Nim game on java??? (crimson editor)

1 reply [Last post]
Offline
Last seen: 22 weeks 4 days ago
Joined: 12/11/2011
Posts:

I need help i got this program and i want to someone to run it for me please because this site wont let me do it

class Nimgoome
{
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);

}
}

tim.speed's picture
Offline
Last seen: 17 hours 48 min ago
Joined: 02/24/2010
Posts:
You could build it as an

You could build it as an applet, try creating a Java applet project and look at the default code to print output.

This tutorial here will teach you how to grab input:
http://www.willamette.edu/~gorr/classes/cs231/lectures/chapter5/

And in the ide you click build, then preview to run the applet once you have written the code.

-----------------
Tim Speed
Compilr Developer and CTO