Design Program a simple version of the game Nim, implementing at least the provi
ID: 3826149 • Letter: D
Question
Design
Program a simple version of the game Nim, implementing at least the provided methods. You must extend some game-related functionality beyond the provided base guidelines.
The goal of the final project is to use what we have learned over the semester to implement your version of the game Nim.
From Wikipedia:
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps.
On each turn, a player must remove at least one object, and may remove any number of objects provided
they all come from the same heap.
This is one variation of the game, though many exist. You do not have to implement the above version. 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 -- whom alternate turns playing.
There is a single heap/pile, initially containing ten 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 one object on its turn if there is only one thing remaining. Otherwise, it should randomly pick between removing 1 or 2 objects.
Explanation / Answer
Here is the code 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;
}
}