Project number 2 and Nim
Code
///Name: Ben Borglin
///Period: 6
///Program Name: Nim
///File Name: Nim.java
///Date Finished: 1/26/16
import java.util.Scanner;
public class Nim
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
String P1, P2, pile, name;
int n, a, b, c, remove;
name = "";
remove = 0;
a = 3;
b = 4;
c = 5;
n = 0;
System.out.print("Player 1, enter your name: ");
P1 = kb.next();
System.out.print("Player 2, enter your name: ");
P2 = kb.next();
while ( a != 0 || b != 0 || c != 0)
{
if (n%2 == 0)
name = P1;
else
name = P2;
n++;
System.out.println(" ");
System.out.println("A: " + a + " B: "+ b +" C: "+ c +" ");
System.out.println(" ");
System.out.print(name + ", choose a pile: ");
pile = kb.next();
if ( pile.equals( "a" ))
{
System.out.print("How many would you like to remove from pile " + pile + ": ");
remove = kb.nextInt();
while (remove <= 0 || remove > a)
{
System.out.println("Sorry, the amount you take must be above zero and less then the amount the pile offers");
System.out.println("How many would you like to take away? ");
remove = kb.nextInt();
}
a = a-remove;
}
else if ( pile.equals( "b" ))
{
System.out.print("How many would you like to remove from pile " + pile + ": ");
remove = kb.nextInt();
while (remove <= 0 || remove > b)
{
System.out.println("Sorry, the amount you take must be above zero and less then the amount the pile offers");
System.out.println("How many would you like to take away? ");
remove = kb.nextInt();
}
b = b-remove;
}
else
{
System.out.print("How many would you like to remove from pile " + pile + ": ");
remove = kb.nextInt();
while (remove <= 0 || remove > c)
{
System.out.println("Sorry, the amount you take must be above zero and less then the amount the pile offers");
System.out.println("How many would you like to take away? ");
remove = kb.nextInt();
}
c = c-remove;
}
}
if ( n%2 == 0)
name = P1;
else
name = P2;
System.out.println(name + " YOU WIN!");
}
}