// Chips.java // Sample solution to Lab #1 Extra Credit. Written by THC. // Plays the game of Chips For Three. We start with an odd number of chips // that is congruent to 1 (mod 3). Players take turns removing chips. // The first player removes any number of chips from the pile, leaving at least // half of them. From then on, the players rotate turns, 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, we take the remainder when we divide each player's // chips by 2. If one player has different remainder than the other two, that's // the winner. Otherwise (all 3 are odd), we take the remainder when we divide // by 3. We must have two players with equal remainders and one player with // a different remainder (proof below). The player with the different remainder // is the winner. // Proof that when we divide the final chip values by 3, two players have equal // remainders and one player has a different remainder: // First, suppose all 3 have the same remainder. Then we get the following // contradiction: the number of chips that all 3 have sum to something congruent // to 0 (mod 3), but the sum must be congruent to 1 (mod 3). // Now suppose all 3 have different remainders. Then we again get the contradiction // that the sum is congruent to 0 (mod 3), since the remainders are 0 (mod 3), // 1 (mod 3), and 2 (mod 3). // The only remaining possibility is that two players have equal remainders // and one player has a different remainder. import java.util.Scanner; public class Chips { public static void main(String[] args) { String response; Scanner input = new Scanner(System.in); do { playAGame(); System.out.print("\n\nPlay another game? (y/n) "); response = input.next(); } while ((response.toLowerCase()).charAt(0) == 'y'); } 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()) { official.describeState(); move = official.getLegalMove(); official.updateState(move); } // All done, so announce the winner. official.announceWinner(); } }