Nim game on java?????
I am suppoed to:
Write a program that allows two people to play Nim. The
program should start by creating four piles of sticks, each
with a number of sticks that varies from 4 to 8. Display
the piles on the screen. The program should then prompt
the first player to remove a number of sticks from a pile.
Once that player has provided valid input, the program
should then adjust the piles and display the new
configuration. This process should continue until one
player has won the game, at which point the computer should
display a congratulatory message to the winner. At the end
of the game, the computer should start a new game if the
users wish to do so. Once the players state that they are
done, the computer should print the games won by each
player.
And this is what i got so far anyone wanna help me finsih it it would be so helpful :)
class Nimgame
{
public static void main (String[]args)
{
playerName();
pilestack();
ChoosePile();
printPileStick();
}
public static void playerName()
{
System.out.print ("P1 Enter your name: ");
char P1 = In.readChar();
System.out.print ("P2 Enter your name: ");
char P2 = In.readChar();
}
public static int pilestack ()
{
System.out.println ( " " );
int a = 1;
for ( a = 1; a<=4; a++)
{
System.out.println (" ");
int pile = (int)(Math.random()* 4 + 4);
System.out.println("pile " + a + " have " + pile + " sticks");
}
return a;
}
public static void ChoosePile()
{
System.out.println(" ");
System.out.println("P1 CHOOSE YOUR PILE");
System.out.println(" ");
int ChoosepileP1 = In.readInt();
System.out.println(" ");
System.out.println ("P1 CHOOSE YOU NUMBER OF STICKS(MAX OF 3)");
System.out.println(" ");
int ChooseStickP1 = In.readInt();
System.out.println(" ");
System.out.println("P2 CHOOSE YOUR PILE");
System.out.println(" ");
int ChoosepileP2 = In.readInt();
System.out.println(" ");
System.out.println ("P2 CHOOSE YOU NUMBER OF STICKS (MAX OF 3)");
System.out.println(" ");
int ChooseStickP2 = In.readInt();
System.out.println(" ");
}
}
I had to change up a bunch of code cause of all the errors I got. But over all I found out the errors in 1 minute. The scanner didn't have the right scripts. Here it is:
import java.util.Scanner;
class Nim
{
static Scanner In = new Scanner (System.in);
static String p1name;
static String p2name;
static int[] pilesOfSticks = new int [4];
static int p1wins = 0;
static int p2wins = 0;
static int turnNumber = 0;
public static void main (String[] args)
{
getPlayerNames ();
do
{
initGame ();
boolean player1turn = true;
do
{
player1turn = (++turnNumber % 2) == 1;
takeTurn (player1turn);
}
while (!gameIsDone ());
// If player 1 took the last stick, player 2 wins (and vice versa)
if (player1turn)
{
System.out.println ("Player 2 (" + p2name + ") wins");
p2wins++;
}
else
{
System.out.println ("Player 1 (" + p1name + ") wins");
p1wins++;
}
}
while (wantToKeepPlaying ());
System.out.println ("After playing the win tally is:");
System.out.println (p1name + " (player 1): " + p1wins + " wins.");
System.out.println (p2name + " (player 2): " + p2wins + " wins.");
}
public static void getPlayerNames ()
{
System.out.print ("P1 Enter your name: ");
p1name = In.nextLine ();
System.out.print ("P2 Enter your name: ");
p2name = In.nextLine ();
}
public static void initGame ()
{
for (int i = 0 ; i < pilesOfSticks.length ; ++i)
{
pilesOfSticks [i] = (int) (Math.random () * 5 + 4);
}
}
public static boolean wantToKeepPlaying ()
{
do
{
System.out.print ("Play another (Y/N)?");
String s = In.nextLine ();
if ("N".equalsIgnoreCase (s))
return false;
if ("Y".equalsIgnoreCase (s))
return true;
System.out.println ("Invalid answer!");
}
while (true);
}
public static boolean gameIsDone ()
{
for (int i = 0 ; i < pilesOfSticks.length ; ++i)
{
if (pilesOfSticks [i] != 0)
return false;
}
return true;
}
public static void takeTurn (boolean player1turn)
{
System.out.println ((player1turn ? p1name:
p2name) + ": your turn...");
printBoard ();
int pileNo = getPileNo (player1turn);
int nSticks = getNumSticks (player1turn, pileNo);
pilesOfSticks [pileNo] -= nSticks;
}
public static void printBoard ()
{
for (int i = 0 ; i < pilesOfSticks.length ; ++i)
{
System.out.println (" Pile " + (i + 1) + " has " + pilesOfSticks [i] + " sticks.");
}
}
public static int getPileNo (boolean player1turn)
{
int pileNo = -1;
do
{
System.out.print ((player1turn ? p1name:
p2name) + ", choose pile: ");
String s = In.nextLine ();
try
{
pileNo = Integer.parseInt (s);
}
catch (Throwable th)
{
System.out.println ("\"" + s + "\" is not a valid pile number (1 to " + pilesOfSticks.length + ")");
continue;
}
if ((pileNo < 1) || (pileNo > pilesOfSticks.length))
{
System.out.println ("\"" + s + "\" is not a valid pile number (1 to " + pilesOfSticks.length + ")");
continue;
}
if (pilesOfSticks [pileNo - 1] <= 0)
{
System.out.println ("\"" + s + "\" does not have any sticks to take.");
continue;
}
// This method uses 1..4, but Java uses zero-based indexing (0..3).
// We subtract one so all code other than this method has the array index.
return pileNo - 1;
}
while (true);
}
public static int getNumSticks (boolean player1turn, int pileNo)
{
int nSticks = -1;
int maxSticks = 3;
if (maxSticks > pilesOfSticks [pileNo])
maxSticks = pilesOfSticks [pileNo];
do
{
System.out.print ((player1turn ? p1name:
p2name) + ", remove how many from pile " + (pileNo + 1) + " (1-" + maxSticks + "): ");
String s = In.nextLine ();
try
{
nSticks = Integer.parseInt (s);
}
catch (Throwable th)
{
System.out.println ("\"" + s + "\" is not a valid integer.");
continue;
}
if ((nSticks < 1) || (nSticks > maxSticks))
{
System.out.println ("\"" + s + "\" is not a valid number of sticks to take (1-" + maxSticks + ")");
continue;
}
return nSticks;
}
while (true);
}
}