Assignemnt #P2 Nim
Code
///Name: Prooz Fereydouni
///Period: 7
///Project Name: Nim
///File Name: Nim.java
///Date: 3/3/2016
import java.util.Scanner;
public class Nim
{
public static void main( String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.print("Player 1, enter your name: ");
String name1= kb.next();
System.out.print("Player 2, enter your name: ");
String name2= kb.next();
System.out.println("");
int a=3, b=4, c=5;
String choice="";
while(a!=0 && b!=0 && c!=0)
{
System.out.println("A: " + a + " B: " + b + " C: " + c);
System.out.println("");
do
{
System.out.print("Please choose either A, B, or C: ");
choice = kb.next();
}while(!(choice.equals("a") || choice.equals("A") || choice.equals("b") || choice.equals("B") || choice.equals("c") || choice.equals("C")));
System.out.print("How many to remove from pile " + choice + ": ");
int remove = kb.nextInt();
if(choice.equals("a") || choice.equals("A"))
{
while(remove < 1 || remove > a)
{
System.out.print("You can only remove a number from 0 to " + a + ": ");
remove = kb.nextInt();
}
a = a - remove;
}
else if(choice.equals("b") || choice.equals("B"))
{
while(remove < 1 || remove > b)
{
System.out.print("You can only remove a number from 0 to " + b + ". ");
remove = kb.nextInt();
}
b = b - remove;
}
else if(choice.equals("c") || choice.equals("C"))
{
while(remove < 1 || remove > c)
{
System.out.print("You can only remove a number from 0 to " + c + ". ");
remove = kb.nextInt();
}
c = c - remove;
}
System.out.println("");
}
if(a==0 && b==0 && c==0)
{
System.out.println(name1 + ", You WIN!");
return;
}
System.out.println("A: " + a + " B: " + b + " C: " + c);
System.out.println("");
System.out.print(name2 + ", choose a pile: ");
choice = kb.next();
do
{
System.out.print("Please choose either A, B, or C: ");
choice = kb.next();
}while(!(choice.equals("a") || choice.equals("A") || choice.equals("b") || choice.equals("B") || choice.equals("c") || choice.equals("C")));
System.out.println("How many to remove from pile " + choice + ": ");
int remove = kb.nextInt();
if(choice.equals("a") || choice.equals("A"))
{
while(remove < 1 || remove > a)
{
System.out.print("You can only remove a number from 0 to " + a + ": ");
remove = kb.nextInt();
}
a = a - remove;
}
else if(choice.equals("b") || choice.equals("B"))
{
while(remove < 1 || remove > b)
{
System.out.print("You can only remove a number from 0 to " + b + ": ");
remove = kb.nextInt();
}
b = b - remove;
}
else if(choice.equals("c") || choice.equals("C"))
{
while(remove < 1 || remove > c)
{
System.out.print("You can only remove a number from 0 to " + c + ": ");
remove = kb.nextInt();
}
c = c - remove;
}
System.out.println("");
if(a==0 && b==0 && c==0)
{
System.out.println(name2 + ", You WIN!");
return;
}
}
}
Picture of the output