Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4. and they received support from authorities, law, or custom

Answered: 1 week ago