Class Game

java.lang.Object
  extended by Game

public class Game
extends java.lang.Object

Game manages the game -- creating Player and Grid objects, and interacting with the human player at each turn. It also keeps track of every move made to enable loading and saving of games.

Author:
willb

Constructor Summary
Game(Player human, Player computer)
          Constructs a new Game with the given Players.
 
Method Summary
 BoardPoint getMove(int i)
          Use getMove when saving games.
 int getMoveCount()
          Use getMoveCount when saving games.
 Player getWinner()
           
 boolean isGameOver()
           
 void placeShipsForPlayer(Player p)
          Repeatedly asks this Player to place ships until all five of this Player's ships are on the board in legal locations.
 void playOneTurn()
          Does the following: The program prints a status report to the screen.
 void reconstituteMove(int i)
          Executes a move without involving a Player.
 void setMove(int i, BoardPoint move)
          Use setMove when loading games, to set up the move state for the ith move in this Game.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Game

public Game(Player human,
            Player computer)
Constructs a new Game with the given Players.

Parameters:
human -
computer -
Method Detail

placeShipsForPlayer

public void placeShipsForPlayer(Player p)
Repeatedly asks this Player to place ships until all five of this Player's ships are on the board in legal locations.

Parameters:
p - a Player

playOneTurn

public void playOneTurn()
Does the following:
  1. The program prints a status report to the screen. The status report is a textual representation of the state of the game, as far as the human player knows. It consists of two boards: one containing the human player’s ships, and one containing the results of “attacks” on the computer player’s board. Therefore, the status report is a pair of 10x10 grids of characters with coordinate annotations. A status report will use the following characters:
    1. spaces indicate the absence of a ship (in the player’s board), or a grid space that has not been attacked (in the results board);
    2. letters from the set {A, B, C, D} indicate ships on the player’s board;
    3. on both the player’s board and the results board, the letter O indicates a missed attack; and
    4. on both the player’s board and the results board, the letter X indicates a successful attack.
  2. The program prompts the human player for an action to undertake. At any turn, the human player may specify “L” to load a game, specify “S” to save a game, specify “Q” to quit, or specify “T” to choose to take a turn, in which case she will be prompted again for a grid space to attack. If the user is loading or saving a game, prompt her for a filename and create a FileLoader or FileSaver as appropriate before saving or reconstituting the game state. If the user is taking a turn and provides invalid input or specifies a space that has already been attacked, the program will prompt the player again.
  3. The program then indicates to the human player whether the grid space she attacked was a hit or a miss. If it was a hit that resulted in destroying a ship, the program will output a message indicating which ship sank. If no ships remain for the computer player, the program will announce that the human player has won the game and ask the user if she wants to play again.
  4. The computer player will then pick a space to attack, using either a “smarter” or a “naive” strategy.
  5. The program then indicates to the human player whether the grid space the computer player attacked was a hit or a miss. If it was a hit that resulted in destroying a ship, the program will output a message indicating which ship sank. If no ships remain for the human player, the program will announce that the computer player has won the game and ask the user if she wants to play again.


isGameOver

public boolean isGameOver()
Returns:
true if one player's ships have all been destroyed

getWinner

public Player getWinner()
Returns:
the Player with ships remaining at the end of the game.

reconstituteMove

public void reconstituteMove(int i)
Executes a move without involving a Player. Only used for loading games from files.

Parameters:
i - the move number to replay; must be less than getMoveCount()

getMove

public BoardPoint getMove(int i)
Use getMove when saving games. Precondition: i < getMoveCount()

Returns:
the ith move of this game

setMove

public void setMove(int i,
                    BoardPoint move)
Use setMove when loading games, to set up the move state for the ith move in this Game.

Parameters:
i - the move number
move - the move

getMoveCount

public int getMoveCount()
Use getMoveCount when saving games.

Returns:
the number of moves in this game