Question: Write a java program Nim where a user can play the game of nim against a computer opponent. The game of nim is usually played
Write a java program Nim where a user can play the game of nim against a computer opponent. The game of nim is usually played with a stack of coins of arbitrary size. On a player's turn, they may remove from 1-3 coins from the stack. Players take turns until there are no coins left. The last player to pick up a coin loses. Specification: First, determine the size of the stack by selecting a random number in the range [10,20]. Next, ask the user to remove between 1 and 3 coins. If the user selects any other number, you should ask again. Have the computer select a random number between 1 and 3 If at any point the stack goes below 0, print out the loser. You should divide your code into the following (static) methods: playerChoose - Prompts the user to enter a number in the valid range, then returns their choice (an integer). computerChoose - returns a random number between 1 and the current size of the coin stack. printStack - prints the current stack as a series of asterisks. Note: The stack should be printed at each round as a list of asterisks ("*")Example 1 (Invalid Choice): The stack is 2 coins. ** How many to remove? [1-2] 3 Invalid option, choose a number in the range [1-2] 2 Player loses Example 2 (Full Game): The stack is 15 coins. *************** How many to remove? [1-3] 2 ************* Computer chose 1 ************ How many to remove? [1-3] 3 ********* Computer chose 1 ******** How many to remove? [1-3] 3 ***** Computer chose 2 *** How many to remove? [1-3] 2 * Computer chose 1 Computer loses
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
