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

Need help with a NimGame. I am supposed to create three classes: a player class,

ID: 3832916 • Letter: N

Question

Need help with a NimGame. I am supposed to create three classes: a player class, a pile class, and a NimGame class.

NimGame class:

For the text-based version of Nim you will be creating a version of the game that can be played by typing instructions into the console. When the program starts, the user should be asked if they would like to play a game of Nim. If they answer affirmatively, they should be asked, what type of game that would like to play with the options being to have two human competitors play, one computer AI and one human player, or two computer AIs play against each other.

For any human player, the game should prompt the user for the player’s name. For any computer AI, they user will be given an option to select from the four different levels of AI.

After the players have been selected, the game should prompt the user to choose the number of marbles that will be used. The user should be able to select from a standard game (21 marbles) a, random game (19 – 99) marbles, or a custom game where the user chooses 1 – 100 marbles.

Afterwards, the game will begin with one player randomly being chosen to make the first move. For each turn, the game should indicate whose turn it is, display a graphical representation of the pile in an orderly format, display the number of marbles remaining, and then allow the player to take their turn and display the number of marbles that was taken by that player.

For a human player, the game should prompt them to enter the number of marbles that they wish to remove. The game must check that result to ensure that it is not possible to enter values outside of the range 1 – 3 and that it is also not possible to take more marbles from the pile than there are remaining.

For a computer player, the game will take their turn automatically and the AI will make a move based on their level of intelligence. See the Player class API for additional information.

When the last marble has been taken from the pile, the game is over and the winner’s name will be printed. After this, the user will be asked if they would like to play again. If they respond affirmatively, the game will start again and the player will repeat the process of determining the players and pile size. Otherwise, the program will exit.

The game should include both type and value safe input checking for all user input. That is, the game should not crash if text is entered when expecting a number or allow numbers outside of the correct range to be entered.

Explanation / Answer

Here is the Java version for you:

import java.util.*;
/**
Implements a game of nim, user vs. computer.
The game starts with the user entering the number of elements in the game.
The computer takes 1 or 2 elements.
Then the user takes 1 or 2 elements.
The game continues until there are no elements left.
Whoever takes the last turn wins.
*/

public class NimGame
{
  
private int remaining; //the number of objects remaining in the game.
private Scanner keyboardInput; //for getting user input from the terminal.
  
/**
When run, your main method should allow a user to play a simple version of Nim from the terminal.
You should at least implement the methods provided here as described in the assignment guidelines.
**/
public static void main(String[] args)
{
  
int numOfMarbles;
Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Nim Game!");
while(true)
{
System.out.print("Please enter the number of stones to be used in this game: ");
numOfMarbles = sc.nextInt();
System.out.print("Enter your name: ");
String name = sc.next();
System.out.print("Which player will start the game? Select a number from the following: 1. Computer 2. " + name + " Choice: ");
int turn = sc.nextInt();
while(turn < 1 || turn > 2)
{
System.out.println("This game has only 2 players, You and your computer.");
System.out.print("Select a number from the following: 1. Computer 2. " + name + " Choice: ");
turn = sc.nextInt();
}
  
while(true)
{
int marblesToPick;
if(turn == 2)
{
    marblesToPick = getHumanMove(numOfMarbles, sc);
   System.out.println(name + " takes " + marblesToPick + " stones, there are " + (numOfMarbles-marblesToPick) +" stones remaining.");
}
else
{
    marblesToPick = getComputerMove(numOfMarbles);
System.out.println("Computer takes " + marblesToPick + " stones, there are " + (numOfMarbles-marblesToPick) +" stones remaining.");
}      
  

numOfMarbles -= marblesToPick;
turn = turn % 2 + 1;
if(isWin(numOfMarbles))
   break;
}
if(turn == 1)
   System.out.println(name + " had to take last stone and lost in the game.");
else
   System.out.println("Computer had to take last stone and lost in the game.");  
System.out.println("Thanks for playing. Want to play again? (Y/N): ");
char again = sc.next().charAt(0);
if(again == 'N' || again == 'n')
   return;
}  
}
  
/**
Returns a number of objects for the computer to remove on its turn.
At a minimum, this should remove the last object if only one remains, and
otherwise it should randomly pick between removing one or two objects.
(update this documentation with details about the version you actually implement.)
*/
public static int getComputerMove(int remaining)
{
int marblesToPick;
Random rnd = new Random();
if(remaining > 1)
        marblesToPick = rnd.nextInt(remaining/2)+ 1;
    else
        marblesToPick = 1;     
    return marblesToPick;  
}
  
/**
Returns a number of objects for the player to remove on its turn.
Use the Scanner parameter to get user input, but verify that the user can only select one or two objects to be removed.
(update this documentation with details about the version you actually implement.)
*/
public static int getHumanMove(int remaining, Scanner keyboardInput)
{
System.out.print("Please enter how many (1, 2, or 3) stones you want to take from the pile of " + remaining + " stones: ");
   int marblesToPick = keyboardInput.nextInt();
   if((marblesToPick == 1 && remaining == 1))
       {}
   else  
   while(marblesToPick < 1 || marblesToPick > remaining/2)
    {
       System.out.println("Invalid choice. Try again... ");
       System.out.print("Please enter how many (1, 2, or 3) stones you want to take from the pile of " + remaining + " stones: ");
       marblesToPick = keyboardInput.nextInt();
    }
    return marblesToPick;
}
  
/**
Returns whether or not the game is over, and print the winner to the terminal.
*/
public static boolean isWin(int remaining)
{
return remaining == 0;
}

}