In: Computer Science
Nim Game Java, PegClass
One variation of the game of Nim, described in Chapter 5
Exercise 12, is played with four
piles of stones. Initially, the piles contain 1, 2, 3, and 4
stones. On each turn, a player may
take 1, 2, or 3 stones from a single pile. The player who takes the
last stone loses.
a) Write a program that allows two players to play Nim. Be sure to
allow only legal
moves. The program should be written so that it can easily be
modified for more
or less than four piles. The program output should look similar
to:
Name for player #1: Petra
Name for player #2: Elaine
Board: 1 2 3 4
Petra
Which pile? 3
How many? 2
Board: 1 2 1 4
Elaine
Which pile? 4
How many? 3
Board: 1 2 1 1
You do not need to write the full program but figure out how to display the board as:
XXX|XXX
XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX
XXX|XXX XXX|XXX XXX|XXX XXX|XXX
and as the numbers change
XXX|XXX - represents one stone
changes.
Code for your game program is provided below. Code is explained in code comments from top to to bottom. I have provided the output screenshot as welll.
As For the second Part, Why just figure out !!! Lets Make it happen!!! I have made the code for representing bricks too. and i have explained it in code comments. A brief explanation for that is, i have changed the code of printBoard() method, and used two loops for displaying bricks in each pile. You can view to code and understand it.
Enjoy the game!!!!! If you need any further clarification please ask in comments.
############################################################################
CODE WHERE BOARD IS REPRESENTED BY NUMBERS
import java.util.Scanner; //to take input
public class PegClass {
//function to return sum of the stones. we use it to find if there is only one stone left
public static int boardSum(int[] arr)
{
int sum=0;
for(int i=0;i<arr.length;i++)
sum+=arr[i];
return sum; //return sum
}
//method to print the board
public static void printBoard(int[] board)
{
for(int i=0;i<board.length;i++)
{
System.out.print(board[i]+" "); //printing no of stones in each pile
}
}
//sets all piles to zero stones
public static void setBoardZero(int[]board)
{
for(int i=0;i<board.length;i++)
board[i]=0;
}
//main method
public static void main(String[] args)
{
int board[]=new int[4]; //board fo size 4
String player1="",player2=""; //player names
int pick=-1; //no of stones to be picked
int pile=-1; //to choose pile
Scanner scan=new Scanner(System.in);
int turn=1; //to be used to determine the turn, 1 for player 1 , 2 for player 2
boolean valid=true; //flag to be used to check if input is valid
for(int i=0;i<board.length;i++)
{
board[i]=i+1; //set the stones in piles
}
System.out.print("Name of player #1: ");
player1=scan.nextLine(); //first player name
System.out.print("Name of player #2: ");
player2=scan.nextLine(); //second player name
while(boardSum(board)>1) //loop until there is only one stone left in the board
{
if(turn==1) //player1 turn
{
do //loop to take valid input
{
valid=true; //set flag true
System.out.print("Board: ");
printBoard(board); //print board
System.out.println("\n"+player1); //player1 name
System.out.print("Which Pile? ");
pile=scan.nextInt(); //select pile
if(pile<1 || pile>board.length) //if the pile no is invalid
{
System.out.println("INVALID PILE. CHOOSE AGAIN");
valid=false; //set flag false
continue; //continue to take input again
}
System.out.print("How Many? ");
pick=scan.nextInt(); //select how many stones to pick
if(pick<1 || pick>(board.length-1)) //if stones entered are invalid
{
System.out.println("INVALID INPUT. YOU HAVE TO PICK ATLEAST 1 AND ATMOST "+(board.length-1)+" STONE. CHOOSE AGAIN");
valid=false;
continue; //continue again
}
if(pick>board[pile-1]) //if there are less stones in the pile but user wants to pick more
{
System.out.println("THERE ARE NOT ENOUGH STONES IN THIS PIlE, PLEASE PICK VALID NO OF PILES");
valid=false;
continue; //loop again take input again
}
board[pile-1]=board[pile-1]-pick; //if input is valid then decrease the stones in pile as player has nimmed the stone
turn=2; //set turn for second player
}while(!valid); //loop until there is invalid input
}
else if(turn==2) //turn for palyer2
{
do
{
valid=true;
System.out.print("Board: ");
printBoard(board); //print board
System.out.println("\n"+player2);
System.out.print("Which Pile? ");
pile=scan.nextInt(); //scan pile number
if(pile<1 || pile>board.length) //invalid pile
{
System.out.println("INVALID PILE. CHOOSE AGAIN");
valid=false;
continue; //continue again
}
System.out.print("How Many? ");
pick=scan.nextInt(); // enter no of stones
if(pick<1 || pick>(board.length-1)) //if invalid stones
{
System.out.println("INVALID INPUT. YOU HAVE TO PICK ATLEAST 1 AND ATMOST "+(board.length-1)+" STONE. CHOOSE AGAIN");
valid=false;
continue;
}
if(pick>board[pile-1]) //if invalid stones
{
System.out.println("THERE ARE NOT ENOUGH STONES IN THIS PIlE, PLEASE PICK VALID NO OF PILES");
valid=false;
continue; //continue again
}
board[pile-1]=board[pile-1]-pick; //if input is valid then decrease the stones in pile as player has nimmed the stone
turn=1; //set turn for second player
}while(!valid);
} //else if closed
} //outer while loop closed
System.out.print("Board: ");
printBoard(board); //print the board with only one stone left
if(turn==1) //if last turn is for player1
{
System.out.println(player1+" picks the last stone.");
setBoardZero(board); //set board with zero stones after player1 has picked it
System.out.print("Board: ");
printBoard(board); //print board with zero stones
System.out.println("\nCongratulations!!! "+player2+", You Win."); //player2 wins
}
else //if turn is for player2
{
System.out.println(player2+" picks the last stone.");
setBoardZero(board); //set board with zero stones after player2 has picked last stone
System.out.print("Board: ");
printBoard(board); //print board with zero stones
System.out.println("\nCongratulations!!! "+player1+", You Win."); //player1 wins
}
scan.close(); //close scanner
}
}
OUTPUT
############################################################################
CODE WHERE BOARD IS REPRESENTED BY BRICKS
import java.util.Scanner; //to take input
public class PegClass {
//function to return sum of the stones. we use it to find if there is only one stone left
public static int boardSum(int[] arr)
{
int sum=0;
for(int i=0;i<arr.length;i++)
sum+=arr[i];
return sum; //return sum
}
//method to print the board
public static void printBoard(int[] board)
{
for(int i=0;i<board.length;i++) //loop for pile
{
for(int j=1;j<=board[i];j++) //loop for each stone
System.out.print("XXX|XXX "); //printing stone
System.out.print("\n"); //new line for next pile
}
}
//sets all piles to zero stones
public static void setBoardZero(int[]board)
{
for(int i=0;i<board.length;i++)
board[i]=0;
}
//main method
public static void main(String[] args)
{
int board[]=new int[4]; //board fo size 4
String player1="",player2=""; //player names
int pick=-1; //no of stones to be picked
int pile=-1; //to choose pile
Scanner scan=new Scanner(System.in);
int turn=1; //to be used to determine the turn, 1 for player 1 , 2 for player 2
boolean valid=true; //flag to be used to check if input is valid
for(int i=0;i<board.length;i++)
{
board[i]=i+1; //set the stones in piles
}
System.out.print("Name of player #1: ");
player1=scan.nextLine(); //first player name
System.out.print("Name of player #2: ");
player2=scan.nextLine(); //second player name
while(boardSum(board)>1) //loop until there is only one stone left in the board
{
if(turn==1) //player1 turn
{
do //loop to take valid input
{
valid=true; //set flag true
System.out.print("Board: \n");
printBoard(board); //print board
System.out.println("\n"+player1); //player1 name
System.out.print("Which Pile? ");
pile=scan.nextInt(); //select pile
if(pile<1 || pile>board.length) //if the pile no is invalid
{
System.out.println("INVALID PILE. CHOOSE AGAIN");
valid=false; //set flag false
continue; //continue to take input again
}
System.out.print("How Many? ");
pick=scan.nextInt(); //select how many stones to pick
if(pick<1 || pick>(board.length-1)) //if stones entered are invalid
{
System.out.println("INVALID INPUT. YOU HAVE TO PICK ATLEAST 1 AND ATMOST "+(board.length-1)+" STONE. CHOOSE AGAIN");
valid=false;
continue; //continue again
}
if(pick>board[pile-1]) //if there are less stones in the pile but user wants to pick more
{
System.out.println("THERE ARE NOT ENOUGH STONES IN THIS PIlE, PLEASE PICK VALID NO OF PILES");
valid=false;
continue; //loop again take input again
}
board[pile-1]=board[pile-1]-pick; //if input is valid then decrease the stones in pile as player has nimmed the stone
turn=2; //set turn for second player
}while(!valid); //loop until there is invalid input
}
else if(turn==2) //turn for palyer2
{
do
{
valid=true;
System.out.print("Board: \n");
printBoard(board); //print board
System.out.println("\n"+player2);
System.out.print("Which Pile? ");
pile=scan.nextInt(); //scan pile number
if(pile<1 || pile>board.length) //invalid pile
{
System.out.println("INVALID PILE. CHOOSE AGAIN");
valid=false;
continue; //continue again
}
System.out.print("How Many? ");
pick=scan.nextInt(); // enter no of stones
if(pick<1 || pick>(board.length-1)) //if invalid stones
{
System.out.println("INVALID INPUT. YOU HAVE TO PICK ATLEAST 1 AND ATMOST "+(board.length-1)+" STONE. CHOOSE AGAIN");
valid=false;
continue;
}
if(pick>board[pile-1]) //if invalid stones
{
System.out.println("THERE ARE NOT ENOUGH STONES IN THIS PIlE, PLEASE PICK VALID NO OF PILES");
valid=false;
continue; //continue again
}
board[pile-1]=board[pile-1]-pick; //if input is valid then decrease the stones in pile as player has nimmed the stone
turn=1; //set turn for second player
}while(!valid);
} //else if closed
} //outer while loop closed
System.out.print("Board: \n");
printBoard(board); //print the board with only one stone left
if(turn==1) //if last turn is for player1
{
System.out.println(player1+" picks the last stone.");
setBoardZero(board); //set board with zero stones after player1 has picked it
System.out.print("Board: ");
printBoard(board); //print board with zero stones
System.out.println("\nCongratulations!!! "+player2+", You Win."); //player2 wins
}
else //if turn is for player2
{
System.out.println(player2+" picks the last stone.");
setBoardZero(board); //set board with zero stones after player2 has picked last stone
System.out.print("Board: ");
printBoard(board); //print board with zero stones
System.out.println("\nCongratulations!!! "+player1+", You Win."); //player1 wins
}
scan.close(); //close scanner
}
}
OUTPUT