Project Number 2

Code

    /// Name: Mahima Choudhary
    /// Period: 6
    /// Program Name: Nim
    /// File Name: Nim.java
    /// Date Finished: 2/7/2016
    
    import java.util.Scanner;

public class Nim
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        
        String player1, player2, pileChoice;
        int pileACount = 4;
        int pileBCount = 3;
        int pileCCount = 5;
        int removeNumber;
        int lastPlayer = 2;
        
        System.out.print("Player 1, please enter your name: ");
        player1 = keyboard.next();
        
        System.out.print("Player 2, please enter your name: ");
        player2 = keyboard.next();
        System.out.println();
        
        while ( pileACount != 0 || pileBCount != 0 || pileCCount != 0 )
        {
            System.out.println("A: " + pileACount + "   B: " + pileBCount + "  C: " + pileCCount);
            System.out.println();

            System.out.print(player1 + ", pick a pile: ");
            pileChoice = keyboard.next();

            while ((pileChoice.equals("A") && pileACount == 0) || (pileChoice.equals("B") && pileBCount == 0) || (pileChoice.equals("C") && pileCCount == 0))
            {
                System.out.print("Good try, " + player1 + ". The pile is empty. Choose again: ");
                pileChoice = keyboard.next();
            }

            if (pileChoice.equals("A"))
            {
                System.out.print("How many to remove from pile " + pileChoice + ": ");
                removeNumber = keyboard.nextInt();
                System.out.println();

                while (removeNumber <= 0 || removeNumber > pileACount)
                {
                    if (removeNumber <= 0)
                    {
                        System.out.print("You must choose at least 1. How many? ");
                        removeNumber = keyboard.nextInt();
                        System.out.println();
                    }
                    else if (removeNumber > pileACount)
                    {
                        System.out.print("Pile " + pileChoice + " doesn't have that many. Try again: ");
                        removeNumber = keyboard.nextInt();
                        System.out.println();
                    }
                }

                pileACount = pileACount - removeNumber;
            }
            else if (pileChoice.equals("B"))
            {
                System.out.print("How many to remove from pile " + pileChoice + ": ");
                removeNumber = keyboard.nextInt();
                System.out.println();

                while (removeNumber <= 0 || removeNumber > pileBCount)
                {
                    if (removeNumber <= 0)
                    {
                        System.out.print("You must choose at least 1. How many? ");
                        removeNumber = keyboard.nextInt();
                        System.out.println();
                    }
                    else if (removeNumber > pileBCount)
                    {
                        System.out.print("Pile " + pileChoice + " doesn't have that many. Try again: ");
                        removeNumber = keyboard.nextInt();
                        System.out.println();
                    }
                }
    

Picture of the output

Project Number 2