// Referee.java // Class for the state of a game of Chips. // Written by THC for Lab #1. // This version uses dialog boxes. import javax.swing.JOptionPane; 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() { // Start by creating the two Player objects. String message; message = "What is the name of the first player?"; currentPlayer = new Player(JOptionPane.showInputDialog(message)); message = "What is the name of the second player?"; otherPlayer = new Player(JOptionPane.showInputDialog(message)); // Make sure that the two players have different names. while (currentPlayer.getName().equalsIgnoreCase(otherPlayer.getName())) { message = "Both players cannot be named " + otherPlayer.getName() + ".\nEnter a different name."; otherPlayer = new Player(JOptionPane.showInputDialog(message)); } // Get the initial number of chips in the pile. final int MIN_CHIPS = 3; // Have to start with at least 3 chips message = "How many chips does the initial pile contain?"; chips = Integer.parseInt(JOptionPane.showInputDialog(message)); while (chips < MIN_CHIPS || chips % 2 == 0) { if (chips < MIN_CHIPS) message = "You have to start with at least " + MIN_CHIPS + " chips.\nChoose another number."; else message = "You have to start with an odd number of chips.\n" + "Choose another number."; chips = Integer.parseInt(JOptionPane.showInputDialog(message)); } // 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; } // Return a String containing the state of the game: // * How many chips each player has. // * Whose turn it is. // * How many chips remain. public String describeState() { String message = describePlayers(); message += "It is your turn, " + currentPlayer.getName() + ".\n"; if (chips == 1) message += "There is 1 chip remaining.\n"; else message += "There are " + chips + " chips remaining.\n"; return message; } // Return a String saying how many chips each player has. public String describePlayers() { return currentPlayer.describe() + "\n" + otherPlayer.describe() + "\n"; } // Return whether a given move is legal. private boolean isLegal(int move) { return move >= 1 && move <= maxMove; } private String getIllegalMoveMessage(int move) { String message = ""; if (move < 1) message = "Illegal move: you must take at least one chip.\n"; else if (move > maxMove) { message = "Illegal move: you may not take more than " + maxMove + " chip"; if (maxMove != 1) message += "s"; message += ".\n"; } return message; } // Get a legal move from a player, prompting until a legal move is given. public int getLegalMove(String message) { int move; // The player's move // Tell the player the range of how many chips may be taken. message += "You may take any number of chips from 1 to " + maxMove + ".\n"; do { message += "How many will you take, " + currentPlayer.getName() + "?"; move = Integer.parseInt(JOptionPane.showInputDialog(message)); message = getIllegalMoveMessage(move); } while (!isLegal(move)); // At this point, we have a legal move, so return it. 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() { String message = 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(); message += winner + " wins!"; JOptionPane.showMessageDialog(null, message); } }