Project 2 and Nim
Code
/// Name: David Shkolnikov
/// Period: 6
/// Program Name: Project 2
/// File Name: Project2.java
/// Date Finished: 1/29/2015
import java.util.Scanner;
public class Project2
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String player1, player2, player, choice;
int pileA = 3, pileB = 4, pileC = 5, n = 0, number;
System.out.print("Player 1, enter your name: ");
player1 = keyboard.next();
System.out.print("Player 2, enter your name: ");
player2 = keyboard.next();
while ( pileA != 0 || pileB != 0 || pileC != 0)
{
if (n % 2 == 0)
player = player1;
else
player = player2;
n++;
System.out.println("\nA: " + pileA + "\tB: " + pileB + "\tC: " + pileC );
System.out.print("\n" + player + ", choose a pile: ");
choice = keyboard.next();
System.out.println(" ");
if (choice.equals("A"))
{
System.out.print("How many to remove from pile " + choice + ": ");
number = keyboard.nextInt();
pileA = pileA - number;
}
else if (choice.equals("B"))
{
System.out.print("How many to remove from pile " + choice + ": ");
number = keyboard.nextInt();
pileB = pileB - number;
}
else if (choice.equals("C"))
{
System.out.print("How many to remove from pile " + choice + ": ");
number = keyboard.nextInt();
pileC = pileC - number;
}
}
if (n % 2 == 0)
player = player1;
else
player = player2;
System.out.println("\nA: " + pileA + "\tB: " + pileB + "\tC: " + pileC);
System.out.println("\n" + player + ", there are no counters left, so you WIN!");
}
}
Picture of the output