What I have accomplished so far...
Design and implement a Nim class and a NimGame application to allow two human players to play the game of Nim. The game starts with four piles of stones. The first pile has 3 stones, the second pile has 5 stones, the third pile has 7 stones, and the last pile has 9 stones. The players take turns removing stones. On a single turn a player may only take stones from a single pile.
The loser of the game is the player to take the very last stone.
This is what I need help on, I entered this for the time being.. but it's obviously not the right way to do it.
A player may not take more than half the pile, except when it’s the last stone in the pile.
while ( stones >= 4 )
{
//Display error message, and ask user to re enter number.
System.out.print("Invalid number, You can't take more than half the pile");
stones = input.nextInt();
}
Below is all the code I have.
package nimgame;
import java.util.Scanner;
public class NimGame
{
public static int player = 1;
public static int [] _board;
public static void main(String[] args)
{
//Declare variables
int pile = 0; //Which file to pick from
int stones = 0; //How many stones to pick
int counter = 0; //If the game is over or not
//Scanner
Scanner input = new Scanner(System.in);
NimClass myAssistant = new NimClass();
myAssistant.displayBoard(); //Set board up
//Begin loop
while(counter == 0)
{
//Choose pile you want to remove stones from.
System.out.print("Player " + player + " Please enter the pile you wish" +
" to remove from. (1, 2, 3 or 4)\n");
//Get a pile number
pile = input.nextInt();
//Only allow user to enter a valid pile number.
while (pile < 1 || pile > 4)
{
//Display error message, and ask user to re enter number.
System.out.print("Invalid number, Please enter a pile # 1-4 ");
pile = input.nextInt();
}
//Enter # of stones you want to remove.
System.out.print("Player " + player + " Please enter the amount of" +
" stones you wish to remove\n");
stones = input.nextInt();
myAssistant.playerMove(pile, stones);
//Only allow user to take half the pile.
while ( stones >= 4 )
{
//Display error message, and ask user to re enter number.
System.out.print("Invalid number, You can't take more than half the pile");
stones = input.nextInt();
}
//Determine what player is entering the values.
if (player == 1)
player = 2;
else
player = 1;
//Display board / determine the winner of the game.
myAssistant.displayBoard();
myAssistant.determineWinner();
if (myAssistant.determineWinner() != -1)
counter = 1;
}
}
}
package nimgame;
public class NimClass
{
//Board for the game array
public static int [] _board;
public NimClass()
{
//Determing # of stones in each pile at the beginning.
_board = new int [4];
_board[0] = 3; //Pile 1 3 stones
_board[1] = 5; //Pile 2 5 stones
_board[2] = 7; //Pile 3 7 stones
_board[3] = 9; //Pile 4 9 stones
System.out.println("Welcome to the Game of Nim\n" +
"You'll need to first pick a pile\n" +
"Then select how many stones you wish to remove\n");
}
//Display the game board
public void displayBoard()
{
for(int row = 0; row < 4; row++)
{
if(_board[row] == 0)
System.out.print(" 0 ");
else
System.out.print(" " + _board[row] + " ");
}
//Print out the piles
System.out.println("\n Pile 1 Pile 2 Pile 3 Pile 4\n");
}
//Determine the player's moves
public boolean playerMove (int pile, int stones)
{
if ((pile < 1) || (pile > _board.length))
{
return false;
}
if (_board[pile - 1] >= stones)
{
_board[pile - 1] -= stones;
return true;
}
else
{
return false;
}
}
//Determine the winner of the game
public int determineWinner()
{
boolean complete = true;
int winner = -1;
//Checking the piles to see if they're empty.
if((_board[0] == 0) && ( _board[1]== 0) && ( _board[2]==0) && ( _board[3]==0))
winner = NimGame.player; //Determine winner
complete = false;
if(complete == false && winner == 1)
{
//Display the results of player 1 winning.
System.out.println("Player 1 Wins!");
winner = 0;
}
if(complete == false && winner == 2)
{
//Display the results of player 2 winning.
System.out.println("Player 2 Wins!");
winner = 0;
}
return winner;
}
}
Any help would be appreciated!