// Referee.java // Class for the maintaining the state of a game of Chips. // Written by THC for Lab #1. import java.util.Scanner; public class Referee { // Instance variables. private int chips; // Number of chips left private Player currentPlayer, otherPlayer; // The two players private int maxMove; // Max number of chips in next legal move // Initialize the state of the game: // * the names of the players and how many chips each has // * the number of chips in the pile // * max number of chips that may be taken in the first move public Referee() { Scanner input = new Scanner(System.in); // Start by creating the two Player objects. System.out.print("What is the name of the first player? "); currentPlayer = new Player(input.nextLine()); System.out.print("What is the name of the second player? "); otherPlayer = new Player(input.nextLine()); // Make sure that the two players have different names. while (currentPlayer.getName().equalsIgnoreCase(otherPlayer.getName())) { System.out.print("Both players cannot be named " + otherPlayer.getName() + ". Enter a different name: "); otherPlayer = new Player(input.nextLine()); } // Get the initial number of chips in the pile. final int MIN_CHIPS = 3; // Have to start with at least 3 chips System.out.print("How many chips does the initial pile contain? "); chips = input.nextInt(); while (chips < MIN_CHIPS || chips % 2 == 0) { if (chips < MIN_CHIPS) System.out.print("You have to start with at least " + MIN_CHIPS + " chips. Choose another number: "); else System.out.print("You have to start with an odd number of chips. " + "Choose another number: "); chips = input.nextInt(); } // It's easy to determine the maximum number of chips that may be taken // in the first move. maxMove = chips / 2; // Print a blank line to separate this dialog from the rest of the game. System.out.println(); } // Return whether the game is over. public boolean saysGameIsOver() { return chips == 0; } // Print out the state of the game: // * How many chips each player has. // * Whose turn it is. // * How many chips remain. public void describeState() { describePlayers(); System.out.println("It is your turn, " + currentPlayer.getName() + "."); if (chips == 1) System.out.println("There is 1 chip remaining."); else System.out.println("There are " + chips + " chips remaining."); } // Print out how many chips each player has. public void describePlayers() { System.out.println(currentPlayer.describe()); System.out.println(otherPlayer.describe()); } // Return whether a given move is legal and, if it's not legal, tell // the player why. private boolean isLegal(int move) { if (move < 1) { System.out.println("Illegal move: you must take at least one chip."); return false; } else if (move > maxMove) { System.out.print("Illegal move: you may not take more than " + maxMove + " chip"); if (maxMove != 1) System.out.print("s"); System.out.println("."); return false; } else return true; } // Get a legal move from a player, prompting until a legal move is given. public int getLegalMove() { int move; // The player's move Scanner input = new Scanner(System.in); // Tell the player the range of how many chips may be taken. System.out.print("You may take any number of chips from 1 to " + maxMove + ". "); do { System.out.print("How many will you take, " + currentPlayer.getName() + "? "); move = input.nextInt(); } while (!isLegal(move)); // At this point, we have a legal move, so return it. First, put out a blank // line to separate the dialog that just occurred from what will follow. System.out.println(); return move; } // Update the state of the game based on a move. public void updateState(int move) { chips -= move; // Remove the chips taken from the pile // Put the chips on the current player's pile. currentPlayer.addChips(move); // Switch between the two players. Player temp = currentPlayer; currentPlayer = otherPlayer; otherPlayer = temp; // And update the max legal move for the next turn. maxMove = Math.min(chips, 2 * move); } // Determine who won the game, and announce it. public void announceWinner() { System.out.println("* * * * * * * * * * * * * * * * * * * * * * * * * * *"); describePlayers(); // The winner is the player with an even number of chips. String winner; if (currentPlayer.getChips() % 2 == 0) winner = currentPlayer.getName(); else winner = otherPlayer.getName(); System.out.println(winner + " wins!"); } }