public boolean moveCLI(KeyboardReader kr) {
   int max = 3;
   if (nSticks < 3) max = nSticks;
   kr.prompt("Input a number between 1 and " + max + ": ");
   int num = kr.getKeyboardInteger();
   return takeSticks(num);
 } // moveCLI()
Beispiel #2
0
 @Override
 public void setKeyEventBindings(ConsoleKeyEventBindings bindings) {
   if (in instanceof KeyboardReader) {
     ((KeyboardReader) in).setKeyEventBindings(bindings);
   } else {
     throw new UnsupportedOperationException("key event bindings cannt be set");
   }
 }
 // *** Implement methods of PlayableCLI interface ***
 public void initCLI(KeyboardReader kr) {
   kr.display("\n*** The Rules of One Row Nim ***\n");
   kr.display("(1) A number of sticks between 7 and 21 is chosen.\n");
   kr.display("(2) Two players alternate making moves.\n");
   kr.display("(3) A move consists of subtracting 1, 2,\n");
   kr.display("   or 3 from the current number of sticks.\n");
   kr.display("(4) A player who cannot leave a positive\n");
   kr.display(" number of sticks for the other player loses.\n");
   int num = -1;
   while ((num < 7) || (num > 21)) {
     kr.prompt("Input a number between 7 and 21:");
     num = kr.getKeyboardInteger();
   } // while
   setSticks(num);
   setPlayer(1);
 } // initCLI()
Beispiel #4
0
 @Override
 public void setCompleter(InputCompleter completer) {
   if (in instanceof KeyboardReader) {
     ((KeyboardReader) in).setCompleter(completer);
   }
 }
 public void finishCLI(KeyboardReader kr) {
   kr.display("\nThe game is over and the winner is ");
   kr.display("Player " + getWinner() + ". \nCongratulations!\n");
 } // finishCLI()
 public void displayCLI(KeyboardReader kr) {
   kr.display("\nThere are are now " + nSticks + " sticks.\n");
   kr.display("Player " + getPlayer() + " moves next.\n");
 } // displayCLI()