Question: Note: You may test getComputerSmartMove ( ) method in zyBook Nim.java darr q , The Game of Nim Problem Description: Two players alternately take marbles

Note: You may test getComputerSmartMove() method in zyBook
Nim.java darr q,
The Game of Nim
Problem Description:
Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. The other player takes a turn. The player who takes the last marble loses.
Write a program to simulate a game of computer vs. human being. Generate a random number between 10-100 to denote the initial size of the pile. Simulate a coin toss to determine who goes first, the computer or the human. Simulate a coin toss to decide if the computer plays smart or stupid mode. In stupid mode, the computer simply takes a random legal number between 1 and n2 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). That is always a legal move, except if the size of the pile is currently one less than a power of 2. In that case, the computer makes a random legal move.
Note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size happens to be 15,31, or 63. If the human player knows the winning strategy and takes the first turn, he can win against the computer.
Program Requirements:
The program must define and use the following methods,
int getComputerStupidMove(int num)// return number of marbles to be moved in stupid mode if current marble number is num.
int getComputerSmartMove(int num)// return number of marbles to be moved in smart mode if current marble number is num.
boolean computerTurn()// return true if it is computer's turn and false if human player's turn
boolean smartMode()// return true if it is in smart mode and false if in stupid mode
Nim.Java
The Game of Nim
Problem Description:
Two players alternately take marbles from a pile. In each move, a player chooses how many marbles to take. The player must take at least one but at most half of the marbles. The other player takes a turn. The player who takes the last marble loses.
Write a program to simulate a game of computer vs. human being. Generate a random number between 10-100 to denote the initial size of the pile. Simulate a coin toss to determine who goes first, the computer or the human. Simulate a coin toss to decide if the computer plays smart or stupid mode. In stupid mode, the computer simply takes a random legal number between 1 and n2 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). That is always a legal move, except if the size of the pile is currently one less than a power of 2. In that case, the computer makes a random legal move.
Note that the computer cannot be beaten in smart mode when it has the first move, unless the pile size happens to be 15,31, or 63. If the human player knows the winning strategy and takes the first turn, he can win against the computer.
Program Requirements:
The program must define and use the following methods,
int getComputerStupidMove(int num)
// return number of marbles to be moved in stupid mode if current marble number is num.
int getComputerSmartMove(int num)// return number of marbles to be moved in smart mode if current marble number is num.
boolean computerTurn()
// return true if it is computer's turn and false if human player's turn
boolean smartMode()
// return true if it is in smart mode and false if in stupid mode
Sample output:
Sample output:
GRASP exec: java GameOfNim
The computer is in stupid mode.
The computer will go first.
Total marbles: 36
There are 36 marbles left.
The computer took 16 marble(s).
There are 20 marbles left.
Take a number between 10 and 1: 10
You took 10 marble(s).
There are 10 marbles left.
The computer took 1 marble(s).
There are 9 marbles left.
Take a number between 4 and 1: 3
You took 3 marble (s).
There are 6 marbles left.
The computer took 1 marble(s).
There are 5 marbles left.
Take a number between 2 and 1: 2
You took 2 marble(s).
There are 3 marbles left.
The computer took 1 marble(s).
There are 2 marbles left.
You can only take 1 marble: 1
You took 1 marble(s).
There is 1 marble left.
Computer took the last marble. Player wins!
----jGRASP: operation complete.What does this mean
Note: You may test getComputerSmartMove ( )

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!