Project #2 Nim
Code
///Name: Tyler Hart
///Period:5
///Project Name: Nim
///File Name:Nim.java
///3/2/2016
import java.util.Random;
import java.util.Scanner;
public class Nim
{
public static void main (String [] args)
{
Random r = new Random();
Scanner stuff= new Scanner(System.in);
String n1,n2,pile;
boolean turn1=true
int a=r.nextInt(6)+4,b=r.nextInt(6)+4,c=r.nextInt(6)+4,remove=0;
System.out.println("Player 1: ");
n1= stuff.next();
System.out.println("Player 2: ");
n2= stuff.next();
while (a>=0||b>=0||c>=0||a+b+c>0||turn1==true)
{
System.out.println("\t a: "+a+"\t b: "+b+"\t c: "+c);
System.out.println(n1+", 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-remove;
turn1=false
}
else if (pile.equals("b"))
{
b=b-remove;
turn1=false
}
else if (pile.equals("c"))
{
c=c-remove;
turn1=false;
}
else
{
System.out.println(n1+" try again");
}
}
while (a>=0||b>=0||c>=0||a+b+c>0||turn1==false)
{
System.out.println("\t a: "+a+"\t b: "+b+"\t c: "+c);
System.out.println(n2+", 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-remove;
turn1=true
}
else if (pile.equals("b"))
{
b=b-remove;
turn1=true
}
else if (pile.equals("c"))
{
c=c-remove;
turn1=true
}
else
{
System.out.println(n2+" try again");
}
}
if (turn1=true)
{
System.out.println(n1+" loeses!!!");
System.out.println(n2+" wins!!!");
}
else
{
System.out.println(n2+" loeses!!!");
System.out.println(n1+" wins!!!");
}
}
}
Picture of the output