import java.util.*; public class Audretsch { int marbles; public void smartMove() { if ((":63:31:15:7:3:").contains(":" + this.marbles + ":")) this.randomMove(); else if (this.marbles > 63) this.marbles = 63; else if (this.marbles > 31) this.marbles = 31; else if (this.marbles > 15) this.marbles = 15; else if (this.marbles > 7) this.marbles = 7; else if (this.marbles > 3) this.marbles = 3; else if (this.marbles > 1) this.marbles = 1; else { System.out.println("I refuse to move."); } System.out.println("Computer moves and pile is now: " + this.marbles); } public void randomMove() { int howMany = (int) (Math.random() * (this.marbles / 2)) + 1; this.marbles -= howMany; System.out.println("Computer moves " + howMany + " and pile is now: " + this.marbles); } public void play() { this.marbles = (int)(100 * Math.random() + 1); System.out.println( this.marbles ); int order = 0; // computer moves String intel = Math.random() > 0.5 ? "smart" : "random"; System.out.println("The computer's moves are " + intel); while (this.marbles > 1) { if (order == 0) { // computer moves if (intel.equals("smart")) smartMove(); else randomMove(); } else { // user moves System.out.print("Move: "); this.marbles -= Integer.parseInt((new Scanner(System.in)).nextLine()); System.out.println("Pile now: " + this.marbles); } order = (order + 1) % 2; } if (order == 0) System.out.println("Computer loses."); else System.out.println("Human loses."); } public static void main(String[] args) { Audretsch game = new Audretsch(); game.play(); } } /*** -bash-4.2$ javac Audretsch.java -bash-4.2$ java Audretsch 78 The computer's moves are smart Computer moves and pile is now: 63 Move: 31 Pile now: 32 Computer moves and pile is now: 31 Move: 10 Pile now: 21 Computer moves and pile is now: 15 Move: 3 Pile now: 12 Computer moves and pile is now: 7 Move: 2 Pile now: 5 Computer moves and pile is now: 3 Move: 1 Pile now: 2 Computer moves and pile is now: 1 Human loses. ---(smart computer, here's another game)--- -bash-4.2$ java Audretsch 44 The computer's moves are random Computer moves 16 and pile is now: 28 Move: 13 Pile now: 15 Computer moves 3 and pile is now: 12 Move: 5 Pile now: 7 Computer moves 1 and pile is now: 6 Move: 3 Pile now: 3 Computer moves 1 and pile is now: 2 Move: 1 Pile now: 1 Computer loses. -bash-4.2$ ---(smart human, here's another game)--- -bash-4.2$ java Audretsch 6 The computer's moves are smart Computer moves and pile is now: 3 Move: 0 Pile now: 3 Computer moves 1 and pile is now: 2 Computer moves and pile is now: 2 Move: 1 Pile now: 1 Computer loses. -bash-4.2$ ---(here I used the fact that the human is allowed to cheat in this code)--- ****/