Question: The game of Nim: Write a java program using only the main method




Question: The game of Nim: Write a java program using only the main method

Write a java program: Using only the main method, no other methods aloud.

The game of Nim. This is a well-known game with a number of variants. The following variant has an interesting winning strategy. 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 the number of remaining marbles. Then the other player takes a turn. The player who takes the last marble loses.

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.


error: Content is protected !!