Other 2007 FR Questions FR other years Be Prepared Home
A-4
Part (a)
class RandomPlayer extends Player
{
  public RandomPlayer(String aName)
  {
    super(aName);
  }

  public String getNextMove(GameState state)
  {
    ArrayList<String> moves = state.getCurrentMoves();
    if (moves.isEmpty())
      return "no move";
    else
      return moves.get((int)(Math.random() * moves.size()));
  }
}

Part (b)
  /** Plays an entire game, as described in the problem description
   */
  public void play()
  {
    System.out.println(state);

    while(!state.isGameOver())
    {
      Player p = state.getCurrentPlayer();
      String move = p.getNextMove(state);
      System.out.println(p.getName() + " ==> " + move);
      state.makeMove(move); 1
    }

    Player winner = state.getWinner();

    if (winner != null)
      System.out.println(winner.getName() + " wins");
    else
      System.out.println("Game ends in a draw");
  }
Notes:
  1. Do not forget to make the move to change the state of the game.

Other 2007 FR Questions | Back to Contents

Copyright © 2007 by Skylight Publishing
support@skylit.com