Project #2 and Nim
Code
/// Name: Ali Kurland
/// Period: 6
/// Program Name: Nim
/// File Name: Nim2.java
/// Date Finished: 11/2/2015
import java.util.Scanner;
public class Nim2
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String name1, name2, player, pile;
int remove, turn, first, end, choice, a = 3, b = 4, c = 5;
choice = 0;
end = 0;
turn = 1;
pile = " ";
player = " ";
System.out.print( "Player 1, enter your name: " );
name1 = keyboard.next();
System.out.print( "Player 2, enter your name: " );
name2 = keyboard.next();
while ( end == 0 )
{
if ( turn % 2 != 0 )
player = name1;
else
player = name2;
System.out.println( "\nA: " + a + "\tB: " + b + "\tC: " + c + "\n" );
System.out.print( player + ", choose a pile: ");
pile = keyboard.next();
if ( pile.equals("A") )
choice = a;
else if ( pile.equals("B") )
choice = b;
else if ( pile.equals("C") )
choice = c;
if ( choice == 0 )
{
System.out.println( " " );
System.out.print( "Nice try, " + player + ". That pile is empty. Choose again: " );
pile = keyboard.next();
}
else
{
System.out.print( "How many to remove from pile " + pile + ": " );
remove = keyboard.nextInt();
while ( remove <= 0 )
{
System.out.println( " " );
System.out.print( "You must choose at least one. How many? " );
remove = keyboard.nextInt();
}
while ( remove > choice )
{
System.out.println( " " );
System.out.print( "Pile " + pile + " doesn't have that many. Try again: " );
remove = keyboard.nextInt();
}
choice = choice - remove;
if ( pile.equals("A") )
a = choice;
else if ( pile.equals("B") )
b =choice;
else if ( pile.equals("C") )
c = choice;
if ( ( a == 0 ) && ( b == 0 ) && ( c == 0 ) )
{
turn ++;
end = 1;
first = 1;
}
else
{
turn ++;
first = 1;
}
}
}
if ( turn % 2 != 0 )
System.out.println( "\n" + name1 + ", there are no counters left, so you WIN!" );
else
System.out.println( "\n" + name2 + ", there are no counters left, so you WIN!" );
}
}
Picture of the output