2007 Game State

<< 2007StudentAnswerSheet | APQuestionsTrailIndex | 2006TimeIntervalAppointment >>

Question from College Board

GameState

import java.util.ArrayList;
public interface GameState 
{ 
  /** @return true if game over 
   *          false otherwise
   */ 
  boolean isGameOver(); 


  /** precondition isGameOver() returns true 
   *  @return the winner, or null if no winner
   */ 
  Player getWinner(); 


  /** precondition isGameOver() returns false 
   *  @return the player who has the next move
   */ 
  Player getCurrentPlayer(); 


  /** @return a list of valid moves 
   */ 
  ArrayList<String> getCurrentMoves(); 


  /** updates the game state
   *  @param move is the move to be made
   */ 
  void makeMove(String move); 


  /** @return a string version of the current GameState 
   */ 
  String toString(); 

} 

Player

public class Player 
{ 
  private String name;   //name of player 


  public Player(String aName) 
  {  name = aName;  } 


  public String getName() 
  {  return name;  } 


  /** This chooses the first valid move
   *  Override this method to define players with other strategies
   *  @param state current state of the game
   *  @return a string representing the move chosen
   *          "no move" if there are no vaid moves
   */ 
  public String getNextMove(GameState state) 
  {  
     return state.getCurrentMoves().get(0);//answer to part (a)
  }  
} 

RandomPlayer

import java.util.ArrayList;
class RandomPlayer 
{
  //part a here
}

HumanPlayer

import java.util.ArrayList;
import java.util.Scanner;
class HumanNimPlayer extends Player
{
  public HumanNimPlayer(String aName)
  {
    super(aName);
  }

  public String getNextMove(GameState state)
  {
    ArrayList<String> moves = state.getCurrentMoves();
    int max=moves.size();
    System.out.print("How many to remove? (1 to "+max+"):");
    Scanner kb=new Scanner(System.in);
    int choice="0123".indexOf(kb.nextLine());

    while (choice<1) {
        System.out.print("How many to remove? (1 to "+max+"):");
        choice="0123".indexOf(kb.nextLine());
    }
    if (choice==1) return "take1";
    if (choice==2) return "take2";
    return "take3";
}

}

GameDriver

public class GameDriver 
{ 
  private GameState state;  //the current state of the game
  public GameDriver(GameState initial) 
  {  state = initial;  } 


  /** Plays an entire game  
   */ 
 public void play() /
  {
    //part b here
  }

} 

NimGame

import java.util.*;

/**
 * 
 * There are 21 sticks in one pile.
 * Players alternate turns removing sticks.
 * The Winner is the one who removes the last stick.
 * Players may remove 1, 2, or 3 sticks when it is their turn.
 * 
 * @author Chris Thiel
 * @version March 20 2008
 */
public class Nim21 implements GameState
{
	private ArrayList <String> moves;
	private boolean over;
	private Player one;
	private Player two;
	private int count;
	private int turn;
	public Nim21()
	{
		moves = new ArrayList ();
		moves.add("take1");
		moves.add("take2");
		moves.add("take3");

		one = new ExpertNimPlayer("Expert");
		two = new HumanNimPlayer("You ");
		over = false;
		count = 21;
		turn =1;
	}
	public boolean isGameOver()
	{
		return count<1;
	}
	public Player getWinner()
	{
		if (turn%2==0) {
		    return one;
		 }else {
		    return two;
		 }
	}
	public Player getCurrentPlayer()
	{
		if (turn%2==0) {
		    return two;
		 }else {
		    return one;
		 }
	}
	public ArrayList <String> getCurrentMoves()
	{
		if (count==2) moves.remove(2);//cannot take 3
		if (count==1) moves.remove(1);//cannot take 2stat
	    return moves;
	}
	/**
	 * updates the gameState and prints it
	 */
	public void makeMove(String move)
	{
		if (move.equals("take1")) count--;
		if (move.equals("take2")) count-=2;
		if (move.equals("take3")) count-=3;
		if(count == 0)
			over = true;
	    turn++;
		System.out.println(count+" sticks remain");
	}
	public String toString()
	{
		return count+" sticks remain in one pile.\n
                     Players alternate turns removing sticks.\n
                     The Winner is the one who removes the last stick.\n
                      Players may remove 1, 2, or 3 sticks when it is their turn.";
	}
}