Project #2 and Nim
Code
/// Name: Brian Phillips
/// Period: 5
/// Program name: Nim
/// File Name: Nim.java
/// Date Finished: 3/4/2016
import java.util.Scanner;
public class Nim
{
public static void main (String [] args)
{
String name1,name2,pile;
int a=5,b=5,c=5,rem=0;
Scanner stuff= new Scanner(System.in);
System.out.println("Player 1 enter your name: ");
name1= stuff.next();
System.out.println("Player 2 enter your name: ");
name2= stuff.next();
do
{
System.out.println("\t a: "+a+"\t b: "+b+"\t c: "+c);
System.out.println(name1+", choose a pile: ");
pile= stuff.next();
System.out.println("How many to remove frome pile "+pile+": ");
rem= stuff.nextInt();
if (pile.equals("a"))
{
a=a-rem;
}
else if (pile.equals("b"))
{
b=b-rem;
}
else if (pile.equals("c"))
{
c=c-rem;
}
if (a+b+c>0)
{
System.out.println("\t a: "+a+"\t b: "+b+"\t c: "+c);
System.out.println(name2+", choose a pile: ");
pile= stuff.next();
System.out.println("How many to remove frome pile "+pile+": ");
rem= stuff.nextInt();
if (pile.equals("a"))
a=a-rem;
else if (pile.equals("b"))
b=b-rem;
else if (pile.equals("c"))
c=c-rem;
if (a+b+c==0)
{
System.out.println(name2+" loses!");
System.out.println(name1+" wins!");
}
}
else
{
System.out.println(name1+" loses!");
System.out.println(name2+" wins!");
}
}while (a+b+c>0);
}
}
Picture of the output