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
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 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 and 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 that is or That is always a legal move, except if the size of the pile is currently one less than a power of 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 or 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 getComputerStupidMoveint num return number of marbles to be moved in stupid mode if current marble number is num.
int getComputerSmartMoveint 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 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 and 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 that is or That is always a legal move, except if the size of the pile is currently one less than a power of 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 or 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 getComputerStupidMoveint num
return number of marbles to be moved in stupid mode if current marble number is num.
int getComputerSmartMoveint 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:
There are marbles left.
The computer took marbles
There are marbles left.
Take a number between and :
You took marbles
There are marbles left.
The computer took marbles
There are marbles left.
Take a number between and :
You took marble s
There are marbles left.
The computer took marbles
There are marbles left.
Take a number between and :
You took marbles
There are marbles left.
The computer took marbles
There are marbles left.
You can only take marble:
You took marbles
There is marble left.
Computer took the last marble. Player wins!
jGRASP: operation complete.What does this mean
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
