Project #2 and Nim
Code
/// Name: Lauren Baird
/// Period: 7
/// Program Name: Nim
/// File Name: p2.java
/// Date Finished: 12/09/2015
import java.util.Scanner;
public class p2
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
int a = 3;
int b = 4;
int c = 5;
int r;
String pile, p1, p2;
int lp = 2;
System.out.println();
System.out.print( "Player 1, enter your name: " );
p1 = keyboard.next();
System.out.print( "Player 2, enter your name: " );
p2 = keyboard.next();
while ( a != 0 || b != 0 || c != 0 )
{
System.out.println();
System.out.println( "A: " + a + " B: " + b + " C: " + c );
System.out.println();
System.out.print( p1 + ", choose a pile: " );
pile = keyboard.next();
while ( pile.equals("A") && a == 0 || pile.equals("B") && b == 0 || pile.equals("C") && c == 0 )
{
System.out.println();
System.out.println( "There isn't anything in pile " + pile + ", " + p1 + ". Try again." );
System.out.print( "Choose a pile: " );
pile = keyboard.next();
}
if ( pile.equals("A") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
while ( r <= 0 || r > a )
{
if ( r <= 0 )
{
System.out.println();
System.out.println( "You need to remove at least one, " + p1 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > a )
{
System.out.println();
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
a = a - r;
}
if ( pile.equals("B") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
while ( r <= 0 || r > b )
{
if ( r <= 0 )
{
System.out.println( "You need to remove at least one, " + p1 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > b )
{
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
b = b - r;
}
if ( pile.equals("C") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
while ( r <= 0 || r > c )
{
if ( r <= 0 )
{
System.out.println( "You need to remove at least one, " + p1 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > c )
{
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
c = c - r;
}
lp--;
if ( a != 0 || b != 0 || c != 0 )
{
System.out.println();
System.out.println( "A: " + a + " B: " + b + " C: " + c );
System.out.println();
System.out.print( p2 + ", choose a pile: " );
pile = keyboard.next();
while ( pile.equals("A") && a == 0 || pile.equals("B") && b == 0 || pile.equals("C") && c == 0 )
{
System.out.println();
System.out.println( "There isn't anything in pile " + pile + ", " + p2 + ". Try again." );
System.out.print( "Choose a pile: " );
pile = keyboard.next();
}
if ( pile.equals("A") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
while ( r <= 0 || r > a )
{
if ( r <= 0 )
{
System.out.println( "You need to remove at least one, " + p2 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > a )
{
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
a = a - r;
}
if ( pile.equals("B") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
while ( r <= 0 || r > b )
{
if ( r <= 0 )
{
System.out.println( "You need to remove at least one, " + p2 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > b )
{
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
b = b - r;
}
if ( pile.equals("C") )
{
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
while ( r <= 0 || r > c )
{
if ( r <= 0 )
{
System.out.println( "You need to remove at least one, " + p2 + ". Try again." );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
else if ( r > c )
{
System.out.println( "Pile " + pile + " does not have that many. Try again. " );
System.out.print( "How many to remove from pile " + pile + ": " );
r = keyboard.nextInt();
System.out.println();
}
}
c = c - r;
}
lp++;
}
}
if ( lp == 1 )
{
System.out.println();
System.out.println( p2 + " won. " );
}
if ( lp == 2 )
{
System.out.println();
System.out.println( p1 + " won. " );
}
System.out.println();
}
}