Question: You will implement Nim in a Java program in which you play against the computer. You are free to choose the initial number of stones

You will implement Nim in a Java program in which you play against the computer. You are free to choose the initial number of stones in each pile, or ask the user to enter one or more of these values. You should make the first move (i.e., the computer should never play first).

On your turn, the program should print out the current state of the board and ask which pile you would like to remove stones from. For example (note that you must use the pile numbers 1, 2, and 3):

Pile: 1 2 3 Stones: 3 4 5 Which pile would you like to remove stones from?

If the pile number is not valid, the program should continue to ask for a number until a valid one is entered. The program should then ask how many stones you wish to remove from that pile. Suppose you choose to remove stones from pile 2:

How many stones would you like to remove from pile 2?

The program should remove the stones from that pile only if the pile has that many. If the number entered matches any of the following conditions,, the program should continue to ask for a number of stones until a valid input is entered:

  • the move would remove fewer than one stone

  • the move would remove more stones than are available in the pile

On the opponents turn, the program should print the current state of the board, randomly generate selections for the opponent, and then report that to you. For example:

Pile: 1 2 3 Stones: 1 4 5 The computer has taken 3 stones from pile 3.

This process should repeat until only one stone remains in a single pile. The program should then report the final state of the board and whether or not you have won. For example:

Pile: 1 2 3 Stones: 0 0 0 You won!

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 Databases Questions!