Question: JAVA Write a Nim code so that the program is broken into functions. At a minimum you should have three functions beside main. One function
JAVA
Write a Nim code so that the program is broken into functions. At a minimum you should have three functions beside main. One function to decide who goes first. One function for the human turn One function for the computer turn. You can write this as two separate functions (regular and expert) or have the function accept a parameter value that lets the function know what mode to play. Each function must have a comment block above it, describing the purpose of the function and what each parameter is used for. Use plent of inline comments and follow naming conventions and use of indentation and white space to make your code readable.
The game of nim: Write a program in which the computer plays against a human opponent. Generate a random integer, 0 or 1 to decide if the computer or the human takes the first turn. Then generate a random number in the range [10,100] to denote the initial size of the number of marbles. Generate another random integer [0,1] to decide whether the computer plays expert or novice. In novice mode, the computer simply takes a random legal number of marbles (between 1 and n/2) from the pile whenever it has a turn. In expert mode the computer takes off enough marbles to make the size of the remaining pile a power of two minus 1, that is 3, 5, 7, 15, 31 or 63. That is always a legal move, except when the size of the pile is currently one less than a power of two. In that case, the computer makes a random legal move. Before the game starts, state how many marbles are in the original pile, who is going first and which mode the computer is using (expert or novice). After each move, state who made the move, how many marbles the player removed and how many marbles remain in the pile. When there is one marble left in the pile, print who won the game. You will note that the computer cannot be beaten in expert mode when it has the first move, unless the initial pile size happens to be 15, 31, or 63. Of course, a human player who has the first turn and knows the winning strategy can win against the computer.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
