scrabble
Class Scrabble

java.lang.Object
  extended by scrabble.Scrabble

public class Scrabble
extends java.lang.Object

Maintains the state of the game, checks for legal plays, calculates scores, finds best plays.

Author:
Amanda Stephan, Significantly refactored by Claude Anderson and Matt Boutell April, 2011

Field Summary
static char[][] DEFAULT_BOARD_LAYOUT
          Default board configuration
static int DEFAULT_BOARD_SIZE
          Default board size
static int[] DEFAULT_LETTER_COUNTS
          Array storing the standard number of tiles for each letter A-Z (blank is at the end)
static int DEFAULT_MAX_HAND_SIZE
          Default maximum number of tiles in hand
static int[] DEFAULT_POINT_VALUES
          Array storing the standard letter point values A-Z (blank is at the end).
 
Constructor Summary
Scrabble()
          Perform initializations common to all Scrabble games.
Scrabble(ScrabbleDictionary dictionary)
          Creates a new standard scrabble game with random tile bag
Scrabble(ScrabbleDictionary dictionary, java.util.ArrayList<java.lang.Character> tileBag)
          Create a new scrabble game in which no plays have been made.
Scrabble(ScrabbleDictionary dictionary, java.lang.String fileName)
          Creates a Scrabble object using the data in the given .scrabble file (the state of the game at any given moment).
 
Method Summary
static ScrabbleDictionary createDictionary(java.lang.String fileName)
          Returns a new ScrabbleDictionary created by reading the file specified in the fileName parameter.
 void drawTile()
          Move last tile in tileBag to the player's hand
 char[][] getBoardChars()
           
 char[][] getBoardConfiguration()
           
 int getBoardSize()
           
 GameState getCurrentGameState()
          Accessor method for Scrabble's currentGameState field.
 ScrabbleDictionary getDictionary()
           
 java.lang.String getLastWordPlayed()
           
 int getMaxHandSize()
           
 int getScoreThisTurn()
           
 java.util.ArrayList<java.lang.Character> getTileBag()
           
 java.util.ArrayList<java.lang.Character> getTilesInHand()
           
 int getTotalScore()
           
 java.util.ArrayList<GameState> playGame()
          Will play the entire game and return an ArrayList of GameStates that represent each step of play from the end of the next play until the end of the game.
 GameState playOnce()
          Will play one turn in the Scrabble game and return a new GameState that contains the values that have changed during play.
 GameState playOnceHuman(java.lang.String toPlay, int rowStart, int colStart, boolean vertical)
          Plays toPlay (in the order given), starting at zero-based position (rowStart, colStart).
 void returnTiles(char[] tilesToReturn)
          Takes in a certain number of tiles, x, that should be removed from the users hand and placed back in the tileBag.
 void shuffle()
          Should shuffle this game's tileBag into a new random order.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BOARD_SIZE

public static final int DEFAULT_BOARD_SIZE
Default board size

See Also:
Constant Field Values

DEFAULT_MAX_HAND_SIZE

public static final int DEFAULT_MAX_HAND_SIZE
Default maximum number of tiles in hand

See Also:
Constant Field Values

DEFAULT_BOARD_LAYOUT

public static final char[][] DEFAULT_BOARD_LAYOUT
Default board configuration


DEFAULT_POINT_VALUES

public static final int[] DEFAULT_POINT_VALUES
Array storing the standard letter point values A-Z (blank is at the end).


DEFAULT_LETTER_COUNTS

public static final int[] DEFAULT_LETTER_COUNTS
Array storing the standard number of tiles for each letter A-Z (blank is at the end)

Constructor Detail

Scrabble

public Scrabble()
Perform initializations common to all Scrabble games.


Scrabble

public Scrabble(ScrabbleDictionary dictionary)
         throws java.lang.Exception
Creates a new standard scrabble game with random tile bag

Parameters:
dictionary -
Throws:
java.lang.Exception

Scrabble

public Scrabble(ScrabbleDictionary dictionary,
                java.util.ArrayList<java.lang.Character> tileBag)
Create a new scrabble game in which no plays have been made.

Parameters:
dictionary - - dictionary to be used in this instance of Scrabble
tileBag - - tiles to be drawn from

Scrabble

public Scrabble(ScrabbleDictionary dictionary,
                java.lang.String fileName)
         throws java.io.IOException
Creates a Scrabble object using the data in the given .scrabble file (the state of the game at any given moment). The format of the .scrabble file must follow the example shown in the specification document.

Parameters:
dictionary - A Scrabble dictionary
fileName - name (must end in '.scrabble')
Throws:
java.io.IOException - if the given file name is not found or does not end in scrabble
Method Detail

drawTile

public void drawTile()
Move last tile in tileBag to the player's hand


getCurrentGameState

public GameState getCurrentGameState()
Accessor method for Scrabble's currentGameState field.

Returns:
the current GameState in the Scrabble class.

playOnce

public GameState playOnce()
Will play one turn in the Scrabble game and return a new GameState that contains the values that have changed during play. See the assignment document for more specifics.

Returns:
a GameState containing the changed values after a single turn.

playGame

public java.util.ArrayList<GameState> playGame()
Will play the entire game and return an ArrayList of GameStates that represent each step of play from the end of the next play until the end of the game.

Returns:
ArrayList of GameState objects representing each step in the game.

playOnceHuman

public GameState playOnceHuman(java.lang.String toPlay,
                               int rowStart,
                               int colStart,
                               boolean vertical)
                        throws java.lang.IllegalArgumentException,
                               java.lang.IndexOutOfBoundsException
Plays toPlay (in the order given), starting at zero-based position (rowStart, colStart). Plays vertically if vertical==true, otherwise plays horizontally. If there is already a letter in a square in which we try to place a tile, skip over it and place that tile in the next available space.

Parameters:
toPlay -
rowStart -
colStart -
vertical -
Returns:
the new state of the game after this play (including replacing player's tiles from the tileBag.
Throws:
java.lang.IllegalArgumentException - if the play tries to make an illegal word.
java.lang.IndexOutOfBoundsException - if the placement is illegal (some tiles would be outside the board, first play does not include the center square, subsequent play does not touch any previously-played letters.

returnTiles

public void returnTiles(char[] tilesToReturn)
Takes in a certain number of tiles, x, that should be removed from the users hand and placed back in the tileBag. The tileBag must then be shuffled and the last x tiles placed into the hand.

Parameters:
tilesToReturn - - tiles to be put back in the tileBag

shuffle

public void shuffle()
Should shuffle this game's tileBag into a new random order.


createDictionary

public static ScrabbleDictionary createDictionary(java.lang.String fileName)
Returns a new ScrabbleDictionary created by reading the file specified in the fileName parameter.

Parameters:
fileName - - the name of the file in which the dictionary is located.
Returns:
a new ScrabbleDictionary

getBoardChars

public char[][] getBoardChars()
Returns:
the characters currently on the board

getBoardConfiguration

public char[][] getBoardConfiguration()
Returns:
the board's current configuration of point squares

getBoardSize

public int getBoardSize()
Returns:
the size of the board (this value must be odd)

getMaxHandSize

public int getMaxHandSize()
Returns:
the maximum number of tiles a user can have in hand at any one time.

getScoreThisTurn

public int getScoreThisTurn()
Returns:
score received on a single turn

getTileBag

public java.util.ArrayList<java.lang.Character> getTileBag()
Returns:
the tileBag containing all letters not yet played

getTilesInHand

public java.util.ArrayList<java.lang.Character> getTilesInHand()
Returns:
an array of characters representing the tiles currently in the user's hand.

getTotalScore

public int getTotalScore()
Returns:
the overall game score

getDictionary

public ScrabbleDictionary getDictionary()
Returns:
the current ScrabbleDictionary

getLastWordPlayed

public java.lang.String getLastWordPlayed()
Returns:
the lastWordPlayed