Project #2: Nim

Code

    /// Name: Kelsey Lieberman
    /// Period 5
    /// Program Name: Nim
    /// File Name: Nim.java
    /// Date Finished: 2/10/2016
    
import java.util.Scanner;

public class Nim
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        
        int A = 3, B = 4, C = 5, amt, p1Turns = 0, p2Turns = 0, x;
        String p1, p2, choice, winner;
        
        System.out.print("\nPlayer 1, enter your name: ");
        p1 = keyboard.nextLine();
        System.out.print("Player 2, enter your name: ");
        p2 = keyboard.nextLine();
        
        do
        {
            if  ((A + B + C) != 0)
            {
                do
                {
                    x = 1;
                    System.out.println("\nA: " + A + "\tB: " + B + "\tC: " + C + "\n");
                    System.out.print(p1 +", choose a pile: ");
                    choice = keyboard.next();
                    System.out.print("How many to remove from pile " + choice + ": ");
                    amt = keyboard.nextInt();
                        if (choice.equalsIgnoreCase("A") && A != 0 && amt > 0 && amt <= A)
                            A = A - amt;
                        else if (choice.equalsIgnoreCase("B") && B != 0 && amt > 0 && amt <= B)
                            B = B - amt;
                        else if (choice.equalsIgnoreCase("C") && C != 0 && amt > 0 && amt <= C)
                            C = C - amt;
                        else
                        {
                            System.out.println("ERROR... No Cheating! Here are some rules:\n\t-Choose a valid pile (A-C); (you picked pile " + choice + ").\n\t-Choose a pile that is not empty.\n\t-Remove a valid number from a pile.\n\t-You can't remove more than exists in a pile.\n\t-You can't remove 0 from a pile.");
                            x = 69;
                        }
                }while (x != 1);
                
                p1Turns++;
            }
            
            if  ((A + B + C) != 0)
            {
                do 
                {
                    x = 1;
                    System.out.println("\nA: " + A + "\tB: " + B + "\tC: " + C + "\n");
                    System.out.print(p2 +", choose a pile: ");
                    choice = keyboard.next();
                    System.out.print("How many to remove from pile " + choice + ": ");
                    amt = keyboard.nextInt();
                        if (choice.equalsIgnoreCase("A") && A != 0 && amt > 0 && amt <= A)
                            A = A - amt;
                        else if (choice.equalsIgnoreCase("B") && B != 0 && amt > 0 && amt <= B)
                            B = B - amt;
                        else if (choice.equalsIgnoreCase("C") && C != 0 && amt > 0 && amt <= C)
                            C = C - amt;
                        else
                        {
                            System.out.println("ERROR... No Cheating! Here are some rules:\n\t-Choose a valid pile (A-C); (you picked pile " + choice + ").\n\t-Choose a pile that is not empty.\n\t-Remove a valid number from a pile.\n\t-You can't remove more than exists in a pile.\n\t-You can't remove 0 from a pile.");
                            x = 69;
                        }
                }while (x != 1);
                
                p2Turns++;
            }
            
        }while ((A + B + C) != 0);
        
        if (p1Turns == p2Turns)
            winner = p1;
        else if (p1Turns > p2Turns)
            winner = p2;
        else 
            winner = "Error";
        
        System.out.println("\n\nThere are no more counters left. Congratulations, " + winner + " you are the winner!!!");
    }
}
    

Picture of the output

  • Project 2
  • Project 2
  • Project 2