// Chips.java // Sample solution to Lab #1. Written by THC. // Plays the game of chips. Originally there are 99 chips in a pile, but // in fact we allow the players to enter the number. The first player removes // any number of chips from the pile, leaving at least half of them. From then // on, the players alternate removing chips from the pile until it is empty. // The number of chips removed in each turn must be at least 1 and at most // twice the number removed by the other player in the previous turn. // At the end of the game, the player with an even number of chips is the // winner. // This version uses dialog boxes. Since it's not really clear what it // means to "cancel" a dialog box when a choice is required, this program // throws an exception when a "cancel" button is pressed. import javax.swing.JOptionPane; public class Chips { public static void main(String[] args) { int response; do { playAGame(); response = JOptionPane.showConfirmDialog(null, "Play another game?"); } while (response == JOptionPane.YES_OPTION); } private static void playAGame() { Referee official = new Referee(); // Make a new game and initialize it. int move; // Number of chips removed in a turn // The main loop makes moves until the game is over. while (!official.saysGameIsOver()) { String stateMessage = official.describeState(); move = official.getLegalMove(stateMessage); official.updateState(move); } // All done, so announce the winner. official.announceWinner(); } }