CS 1043Program 3
The game of Nim is a contest between two players.The two players alternately take marbles from a pile of marbles. In each move, a player chooses how many marbles to withdraw from the pile. A player must remove at least one marble, but no more than half of the marbles from the pile during a move. You will write a program where the computer plays “Nim” against a human opponent. The player forced to pickup the last marble losses the game.
Write a class called Nim. The Nim class must have
- One static method called main. You must use the following main method for your program:
public static void main( String [] args )
{
Scanner record = new Scanner( System.in );
String answer = null;
do
{ Nim game = new Nim(); // Start a new game.
game.playGame(); // play the game until someone wins.
System.out.printf( "Do you want to play again? (enter yes or no)\n" );
answer = record.next();
} while ( answer.equalsIgnoreCase( "yes" ) );
} // end main
- Only one constructor. The constructor must be a default constructor. The constructor should do the following:
- Generate a random integer between 10 and 100 to denote the initial number of marbles in the pile.
- Generate a random Boolean to decide if the computer or the human takes the first turn.
- Prompt the user to decide if the program will play in smart mode, stupid mode, or allow the computer to determine the mode randomly.
- There must be at least 3 private instance variables:
- NMarbles_ = number of marbles in the pile.
- SMode_ = a Boolean indicating if the computer is to play in smart mode.
- HTurn_ = a Boolean indicating if it is the Human’s turn to remove marbles from the pile.
- An instance method named playGame()
- The playGame method will have a while or do-while loop to alternate turns between the two players until a winner can be determined.
- An instance method named humanTurn() that will prompt the human player for a valid number of marbles to withdraw from the pile. This method should then remove the marbles from the pile. If the human player enters an invalid number of marbles, the method should continue to prompt for a valid number of marbles.
- An instance method named computerTurn() that will remove marbles from the pile.
- In stupid mode, the computer simply takes a random legal value (between 1 and n/2 marbles) from the pile whenever it has a turn.
- In smart mode, the computer takes off enough marbles to make the size of the pile a power of two minus 1; that is 3, 7, 15, 31 or 63. However, if size of the pile is currently one less than a power of two, the computer will randomly compute a legal random number of marbles to withdraw from the pile.
- An instance method named gameStatus() that will display the current pile size.
- The computer cannot be beaten in smart mode if it has the first move and if the starting pile size is other than 15, 31, or 63.
- A human knowing the correct strategy can beat the computer.
Clearly label your game with output that looks like the following:
Do you want the computer to play in Smart mode?
(Enter: yes, no, or don't care): yes
Current pile size = 59.
The Computer picks 28 marble(s).
Current pile size = 31.
Please pick at least one marble, but no more than 15 marble(s): 8
Current pile size = 23.
The Computer picks 8 marble(s).
Current pile size = 15.
Please pick at least one marble, but no more than 7 marble(s):
Current pile size = 9.
The Computer picks 2 marble(s).
Current pile size = 7.
Please pick at least one marble, but no more than 3 marble(s): 3
Current pile size = 4.
The Computer picks 1 marble(s).
Current pile size = 3.
Please pick at least one marble, but no more than 1 marble(s): 1
Current pile size = 2.
The Computer picks 1 marble(s).
Current pile size = 1.
Please pick at least one marble, but no more than 1 marble(s): 1
Sorry. You lose.
Do you want to play again? (enter yes or no)
Clearly indicate the winner.
When finished, submit your source code program using WebCT by the due date.