At a minimum , however, you should implement Nim such that: There are two player
ID: 3829932 • Letter: A
Question
At a minimum, however, you should implement Nim such that:
There are two players -- one human user (taking input from the terminal) and one computer AI -- who alternate turns playing.
There is a single heap/pile, initially containing 10 objects, which the players may remove either one (1) or two (2) objects on their respective turns.
The player who removes the last item from the heap is declared the winner.
The AI should remove 1 object on its turn if there is only one item remaining, otherwise it should randomly pick between removing 1 or 2 objects
Explanation / Answer
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;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.