Project #2 Nim

Code

    Name: Iaroslav Titov
    Period: 7
    Project Name: NIM
    File Name: NIM.java
    Date: 1/11/2015
    
import java.util.Scanner;
import java.util.Random;

public class NIM
{
    public static void main(String[] args)
    {
        int[] pile = new int[3];
        String[] player = new String[2];
        byte currPlayer,chosenPile = 0;
        Random r = new Random();
        Scanner keyboard = new Scanner(System.in);
        String read = "";
        boolean correct = false;
        int amountToRemove = 0;

        pile[0] = r.nextInt(7) + 1;
        pile[1] = pile[0]+1;
        pile[2] = pile[1]+1;

        try
        {
            System.out.println("Welcome to the NIM game!!!\n");

            currPlayer = 1;

            System.out.print("Player 1: ");
            player[0] = keyboard.next();
            System.out.println();

            System.out.print("Player 2: ");
            player[1] = keyboard.next();
            System.out.println();

            while (pile[0]+pile[1]+pile[2]!=0)
            {
                System.out.println("A: "+pile[0]+"   B: "+pile[1]+"   C: "+pile[2]+"\n");

                correct = false;
                do
                {
                    System.out.print(player[currPlayer] + ", choose a pile: ");
                    read = keyboard.next();

                    correct = true;
                    switch (read)
                    {
                        case "A": chosenPile = 0;
                            break;
                        case "B": chosenPile = 1;
                            break;
                        case "C": chosenPile = 2;
                            break;
                        default: correct = false;
                                 System.out.println("Choose A, B or C!!!");
                            break;
                    }
                    if (pile[chosenPile]==0 && correct)
                    {
                        System.out.println("Chosen pile is empty, choose another one!!!");
                        correct = false;
                    }

                }
                while (!correct);

                do
                {
                    System.out.print("How many to remove from that pile? ");
                    amountToRemove = keyboard.nextInt();

                    correct = true;

                    if (amountToRemove<=0)
                    {
                        System.out.println("You have to enter a positive number!!!");
                        correct = false;
                    }

                    if (pile[chosenPile] < amountToRemove && correct)
                    {
                        System.out.println("You entered too many!!! The max you can remove is "+pile[chosenPile]);
                        correct = false;
                    }

                    if (correct) pile[chosenPile] -= amountToRemove;
                }
                while (!correct);


                if (currPlayer==0) currPlayer = 1;
                else currPlayer = 0;
            }


            System.out.println("You can do a victory dance now, "+player[currPlayer]+"!!!");

            if (currPlayer==0) currPlayer = 1;
            else currPlayer = 0;

            System.out.println("You suck, "+player[currPlayer]+"!!!");



        }
        catch (Exception e)
        {
            System.out.println("WRONG INPUT!!! RESTART THE GAME!!!");
        }

        System.out.println();
    }
}

    

Picture of the output

Assignment p2