Assignemnt Project #2 and Nim
Code
///Name: Daniel Tiffany-Appleton
///Period: 7
///Program Name: Nim
///File Name: Nim
///Date Finished: 1/29/16
import java.util.Scanner;
public class Nim
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String playerOne, playerTwo, chose;
int pileA, pileB, pileC, playerTake, turns;
pileA = 3;
pileB = 4;
pileC = 5;
turns = 1;
System.out.print("Player 1, enter your name: ");
playerOne = keyboard.next();
System.out.print("Player 2, enter your name: ");
playerTwo = keyboard.next();
do
{
System.out.print("\nA: " + pileA + " B:" + pileB + " C: " + pileC);
if ( turns % 2 == 1 )
{
while(turns % 2 == 1 )
{
System.out.print( "\n" + playerOne + ", choose a pile: ");
chose = keyboard.next();
if(chose.equals("A") && pileA != 0)
{
System.out.print( "How many to remove from pile A: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileA )
{
pileA = pileA - playerTake;
}
else if( playerTake <= 0 )
{
while( playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileA = pileA - playerTake;
}
else if( playerTake > pileA )
{
while( playerTake > pileA )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileA = pileA - playerTake;
}
}
else if(chose.equals("B") && pileB != 0)
{
System.out.print( "How many to remove from pile B: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileB )
{
pileB = pileB - playerTake;
}
else if( playerTake <= 0 )
{
while(playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileB = pileB - playerTake;
}
else if( playerTake > pileB )
{
while(playerTake > pileB )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileB = pileB - playerTake;
}
}
else if(chose.equals("C") && pileC != 0)
{
System.out.print( "How many to remove from pile C: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileC )
{
pileC = pileC - playerTake;
}
else if( playerTake <= 0 )
{
while(playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileC = pileC - playerTake;
}
else if( playerTake > pileC )
{
while(playerTake > pileC )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileC = pileC - playerTake;
}
}
turns++;
}
}
else if ( turns % 2 == 0 )
{
while(turns % 2 == 0 )
{
System.out.print( "\n" + playerTwo + ", choose a pile: ");
chose = keyboard.next();
if(chose.equals("A") && pileA != 0)
{
System.out.print( "How many to remove from pile A: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileA )
{
pileA = pileA - playerTake;
}
else if( playerTake <= 0 )
{
while(playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileA = pileA - playerTake;
}
else if( playerTake > pileA )
{
while( playerTake > pileA )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileA = pileA - playerTake;
}
}
else if(chose.equals("B") && pileB != 0)
{
System.out.print( "How many to remove from pile B: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileB )
{
pileB = pileB - playerTake;
}
else if( playerTake <= 0 )
{
while( playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileB = pileB - playerTake;
}
else if( playerTake > pileB )
{
while( playerTake > pileB )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileB = pileB - playerTake;
}
}
else if(chose.equals("C") && pileC != 0)
{
System.out.print( "How many to remove from pile C: ");
playerTake = keyboard.nextInt();
if( playerTake > 0 && playerTake <= pileC )
{
pileC = pileC - playerTake;
}
else if( playerTake <= 0 )
{
while( playerTake <= 0 )
{
System.out.print( "Number must be greater than zero! Try Again: ");
playerTake = keyboard.nextInt();
}
pileC = pileC - playerTake;
}
else if( playerTake > pileC )
{
while( playerTake > pileC )
{
System.out.print( "Pile doesn't have that much! Try Again: ");
playerTake = keyboard.nextInt();
}
pileC = pileC - playerTake;
}
}
turns++;
}
}
}while(pileA != 0|| pileB != 0 || pileC != 0);
if( turns % 2 == 0 )
{
System.out.println( "\n" + playerTwo + " wins!");
}
else if ( turns % 2 == 1 )
{
System.out.println( "\n" + playerOne + " wins!");
}
}
}
Picture of the output