Assignment Project

Code

        /// Name: Georgi Atanasov
        /// Period: 5
        /// Program Name: Nim
        /// File Name: Nim.java
        /// Date Finished: 3/2/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, enter your name: ");
        player1 = keyboard.next();
        
        System.out.print("Player 2, 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 + ", choose a pile: ");
            pileChoice = keyboard.next();

            while ((pileChoice.equals("A") && pileACount == 0) || (pileChoice.equals("B") && pileBCount == 0) || (pileChoice.equals("C") && pileCCount == 0))
            {
                System.out.print("Nice try, " + player1 + ". That 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();
                    }
                }

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

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

                pileCCount = pileCCount - removeNumber;
            }

            lastPlayer--;
            
            if ( pileACount != 0 || pileBCount != 0 || pileCCount != 0 )
            {
                System.out.println();
                System.out.println("A: " + pileACount + "   B: " + pileBCount + "  C: " + pileCCount);
                System.out.println();
                
                System.out.print(player2 + ", choose a pile: ");
                pileChoice = keyboard.next();
                
                while ((pileChoice.equals("A") && pileACount == 0) || (pileChoice.equals("B") && pileBCount == 0) || (pileChoice.equals("C") && pileCCount == 0))
                {
                    System.out.print("Nice try, " + player2 + ". That 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();
                        }
                    }
                    
                    pileBCount = pileBCount - removeNumber;
                }
                else if (pileChoice.equals("C"))
                {
                    System.out.print("How many to remove from pile " + pileChoice + ": ");
                    removeNumber = keyboard.nextInt();
                    System.out.println();
                    
                    while (removeNumber <= 0 || removeNumber > pileCCount)
                    {
                        if (removeNumber <= 0)
                        {
                            System.out.print("You must choose at least 1. How many? ");
                            removeNumber = keyboard.nextInt();
                            System.out.println();
                        }
                        else if (removeNumber > pileCCount)
                        {
                            System.out.print("Pile " + pileChoice + " doesn't have that many. Try again: ");
                            removeNumber = keyboard.nextInt();
                            System.out.println();
                        }
                    }
                    
                    pileCCount = pileCCount - removeNumber;
                }
                
                lastPlayer++;
            }
        }
        
        System.out.println("A: " + pileACount + "   B: " + pileBCount + "  C: " + pileCCount);
        System.out.println();
        
        if (lastPlayer == 1)
            System.out.println(player2 + ", there are no counters left, so you win!");
        else if (lastPlayer == 2)
            System.out.println(player1 + ", there are no counters left, so you win!");
    }
}
    

Picture of the output

Assignment Project