Project 2, Nim
Code
///Name: Stella Koliavas
///Period: 5
///File Name: Nim.java
///Date Finshed: 3/10/16
import java.util.Scanner;
public class Nim
{
public static void main( String[] args )
{
Scanner siri = new Scanner(System.in);
int a=3, b=6, c=7, takeaway = 0 ;
String one, two, choice;
System.out.print("Player 1: ");
one= siri.next();
System.out.print("Player 2: ");
two= siri.next();
System.out.println("Okay now we're going to play a game. There's three piles and taking
turns each player removes 1 or more from a pile at a time and the player that can't take
anything from a pile loses.");
System.out.println("A."+a+"\t B."+b+"\t C."+c );
while (a !=0 || b!=0 || c!=0)
{
System.out.print( one + " choose a pile > ");
choice = siri.next();
System.out.print( one+" how many do you want to take from the pile? >");
takeaway = siri.nextInt();
if (choice.equals("A"))
{
a = a - takeaway;
}
if (choice.equals("B"))
{
b = b- takeaway;
}
if (choice.equals("C"))
{
c = c- takeaway;
}
System.out.println(" ");
System.out.println("A."+a+"\t B."+b+"\t C."+c );
System.out.print( two + " choose a pile > ");
choice = siri.next();
System.out.print( two+" how many do you want to take from the pile? >");
takeaway = siri.nextInt();
if (choice.equals("A"))
{
a = a - takeaway;
}
if (choice.equals("B"))
{
b = b- takeaway;
}
if (choice.equals("C"))
{
c = c- takeaway;
}
System.out.println("A."+a+"\t B."+b+"\t C."+c );
}
if (a ==0 && b==0 && c==0)
System.out.println("Game Over");
}}
Picture of the output