core
Class Game

java.lang.Object
  extended by core.Game

public class Game
extends Object

A mutable class containing all the information about a game. This keeps track of game stuff that generally isn't game specific (like time, move history etc.)

Specification Fields

Constructor Summary
Game(Player player1, int player1Time, Player player2, int player2Time)
          Creates a new Game instance with Players and starting times.
Game(Player player1, int player1Time, Player player2, int player2Time, String powerups)
           
Game(Player player1, Player player2)
          Creates a new Game instance with Players and no times.
 
Method Summary
 Player getCurrentPlayer()
          Gives the current player to move.
 GameState getGameState()
          Returns a mutable copy of the gamestate (changing it will change the game).
 List<Move> getMoveHistory()
          History of moves that have been made so far, from first to last.
 Player getPlayerByColor(Color color)
          A mapping from colors to players
 List<Player> getPlayers()
          Gives an immutable list of the players, with first player first
 int getTimeLeftForPlayer(Player player)
          A mapping from a player to the time left for that player
 Color getWinner()
          Gives the winner if there is one.
 GameOver getWinReason()
          Gives the reason for winning if there is one.
 String getXML()
          Returns the XML for the game as a string.
static Game loadGame(Player player1, Player player2, File xmlFile)
          Loads a game given an xml file and the initial players.
static Game loadGame(Player player1, Player player2, String xmlString)
          Loads a game given an xml string and the initial players.
 void makeMove(Move move)
          Makes a move (if it is valid), also saving it the move history (if it is not a pass).
 void setTime(Player player, int t)
          Sets the time for a player.
 void subtractTimeLeftForPlayer(Player player, int amount)
          Subtracts amount from current time of the player (calls setTime, so updated winner if needed).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Game

public Game(Player player1,
            Player player2)
Creates a new Game instance with Players and no times.

Parameters:
player1 - the player that goes first, must have Color WHITE
player2 - the player that goes second, must have Color BLACK
Requires:
players not null

Game

public Game(Player player1,
            int player1Time,
            Player player2,
            int player2Time)
Creates a new Game instance with Players and starting times.

Parameters:
player1 - the player that goes first, must have Color WHITE
player1Time - time for player1
player2 - the player that goes second, must have Color BLACK
player2Time - time for player2
Requires:
players not null

Game

public Game(Player player1,
            int player1Time,
            Player player2,
            int player2Time,
            String powerups)
Method Detail

loadGame

public static Game loadGame(Player player1,
                            Player player2,
                            File xmlFile)
                     throws GameLoadException
Loads a game given an xml file and the initial players. See the xml format in http://web.mit.edu/6.170/www/assignments/antichess/antichess.html#game-file-example

Parameters:
player1 - white player
player2 - black player
xmlFile - file with file format linked to above
Throws:
GameLoadException - contains a user intended error message
Requires:
no arguments null
Returns:
loaded game

loadGame

public static Game loadGame(Player player1,
                            Player player2,
                            String xmlString)
                     throws GameLoadException
Loads a game given an xml string and the initial players. See the xml format in http://web.mit.edu/6.170/www/assignments/antichess/antichess.html#game-file-example

Parameters:
player1 - white player
player2 - black player
xmlString - string with format linked to above
Throws:
GameLoadException - contains a user intended error message
Requires:
no arguments null
Returns:
loaded game

getXML

public String getXML()
Returns the XML for the game as a string. See http://web.mit.edu/6.170/www/assignments/antichess/antichess.html#game-file-format for the format.

Returns:
the xml string

makeMove

public void makeMove(Move move)
Makes a move (if it is valid), also saving it the move history (if it is not a pass).
Note that you need to decrement the time separately (beforehand), in order to work with the tui spec.

Parameters:
move - the move to make
Throws:
RuntimeException - if the move is invalid
Requires:
move not null, time for the player is decremented beforehand, the move is valid
Effects:
makes a move, updating all relavent fields
Modifies:
game_state, current_player, xml + possibly: move_history, winner, win_reason

getGameState

public GameState getGameState()
Returns a mutable copy of the gamestate (changing it will change the game).

Returns:
the current gamestate (not immutable)

getMoveHistory

public List<Move> getMoveHistory()
History of moves that have been made so far, from first to last.

Returns:
the list if moves

getWinner

public Color getWinner()
Gives the winner if there is one.

Returns:
the color (enum) of the winner (NONE if no winner)

getWinReason

public GameOver getWinReason()
Gives the reason for winning if there is one.

Returns:
the reason for winning (as a GameOver enum), or NONE if no one has won.

setTime

public void setTime(Player player,
                    int t)
Sets the time for a player. If the game isn't timed, does nothing. If the game is timed and the player runs out of time, sets the other player as the winner.

Parameters:
player - The player whose time to change
t - The new time
Requires:
final time is > 0 or == -1, player is one of the player
Effects:
sets the new time for the player and sets the winner if there is one.
Modifies:
time_for_players, winner, win_reason, xml

subtractTimeLeftForPlayer

public void subtractTimeLeftForPlayer(Player player,
                                      int amount)
Subtracts amount from current time of the player (calls setTime, so updated winner if needed).

Parameters:
player - The player whose time to change
amount - The new time
Requires:
amount >= 0, player is one of the player
Effects:
sets the decreased time for the player and sets the winner if there is one.
Modifies:
time_for_players, winner, win_reason, xml

getTimeLeftForPlayer

public int getTimeLeftForPlayer(Player player)
A mapping from a player to the time left for that player

Parameters:
player - player to get the time of
Requires:
player not null
Returns:
int time for that player

getPlayerByColor

public Player getPlayerByColor(Color color)
A mapping from colors to players

Parameters:
color - the color of the player to get
Requires:
color not null or NONE
Returns:
the player of that color.

getPlayers

public List<Player> getPlayers()
Gives an immutable list of the players, with first player first

Returns:
list of players

getCurrentPlayer

public Player getCurrentPlayer()
Gives the current player to move. (or if the game is over, the player that would move otherwise).

Returns:
player to move