Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a java program Nim where a user can play the game of nim against a compute

ID: 642167 • Letter: W

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 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):

Example 2 (Full Game):

Explanation / Answer

Yes, here's the whole thing: class Nim { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println("Hello, and welcome to the game of Nim!"); System.out.print("What is your name: "); String user = console.readLine(); int height = (int)(Math.random() * 90 + 10); PileOfMarbles pile = new PileOfMarbles(height); System.out.println("Game starts with a pile of height: " + pile.report()); int number, currentHeight; while (true) { System.out.println("*** Computer moves."); System.out.println("Pile of marbles of height: " + pile.report()); currentHeight = pile.report(); if (currentHeight == 1) { number = 1; } else { number = (int)(Math.random() * (currentHeight / 2)) + 1; } System.out.println("Computer chooses to remove: " + number + " marbles."); pile.move(number, "Computer"); System.out.println("--------------------------"); System.out.println("*** Now " + user + " has to move."); System.out.println("Pile of marbles of height: " + pile.report()); System.out.print(user + ", please enter number of marbles you want to take: "); number = console.readInt(); pile.move(number, user); System.out.println("--------------------------"); } } } class PileOfMarbles { int height; PileOfMarbles (int height) { this.height = height; } int report() { return this.height; } void move(int number, String user) { System.out.println("***Removing " + number + " marbles from the pile for: " + user); if (number height / 2) && (number != 1))) { System.out.println("***Bad move for " + user + ". " + user + " loses."); System.exit(0); } else { this.height -= number; if (this.height == 0) { System.out.println("***End of game. " + user + " loses."); System.exit(0); } } System.out.println("Pile of marbles is now: " + this.report()); } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote