Project #2 Nim

Code

    /// Name: Koosha Kimelman
    /// Period: 7
    /// Program Name: Nim
    /// File Name: Nim.java
    /// Date Completed: 12/7/15
    
    import java.util.Scanner;
    
    public class Nim {
    
        public static void main(String[] args) {
        
            Scanner keyboard = new Scanner(System.in);
            
            String n1, n2, sc;
            int p1, p2, p3, pc, n;
            
            n = 0;
            p1 = 3;
            p2 = 4;
            p3 = 5;
            
            System.out.print("Player 1, enter your first name: ");
            n1 = keyboard.next();
            
            System.out.print("Player 2, enter your first name: ");
            n2 = keyboard.next();
            
            System.out.println("\n\nA: " + p1 + "   B: " + p2 + "   C: " + p3);
            
            while (p1 + p2 + p3 != 0) {
            
                if (n % 2 == 0) {
            
                    System.out.print("\n" + n1 + ", choose a pile: ");
                    sc = keyboard.next();
            
                    if (!sc.equals("A") && !sc.equals("B") && !sc.equals("C")) {
                        System.out.println("You didn't choose one of the three piles. Try again.");
                        n--;
                    }
            
                    else {
                        System.out.print("How many to remove from pile " + sc + ": ");
                        pc = keyboard.nextInt();
            
                        if (sc.equals("A")) {
                            p1 = p1 - pc;
                            if (p1 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p1 = p1 + pc;
                                n--;
                            }
                        }
                        else if (sc.equals("B")) {
                            p2 = p2 - pc;
                            if (p2 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p2 = p2 + pc;
                                n--;
                            }
                        }
                        else if (sc.equals("C")) {
                            p3 = p3 - pc;
                            if (p3 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p3 = p3 + pc;
                                n--;
                            }   
                        }
                    }
                    n++;
                }
            
                else if (n % 2 == 1) {
            
                    System.out.print("\n" + n2 + ", choose a pile: ");
                    sc = keyboard.next();
            
                    if (!sc.equals("A") && !sc.equals("B") && !sc.equals("C")) {
                        System.out.println("You didn't choose one of the three piles. Try again.");
                        n--;
                    }
            
                    else {
                        System.out.print("How many to remove from pile " + sc + ": ");
                        pc = keyboard.nextInt();
            
                        if (sc.equals("A")) {
                            p1 = p1 - pc;
                            if (p1 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p1 = p1 + pc;
                                n--;
                            }
                        }
                        else if (sc.equals("B")) {
                            p2 = p2 - pc;
                            if (p2 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p2 = p2 + pc;
                                n--;
                            }
                        }
                        else if (sc.equals("C")) {
                            p3 = p3 - pc;
                            if (p3 < 0 || pc <= 0) {
                                System.out.println("You are trying to cheat, try again.");
                                p3 = p3 + pc;
                                n--;
                            }
                        }
                    }
                    n++;
                }
                
            System.out.println("\nA: " + p1 + "   B: " + p2 + "   C: " + p3);
                
            }
                
            if (n % 2 == 0)
                System.out.println("\n" + n1 + " wins!");
            else if (n % 2 == 1)
                System.out.println("\n" + n2 + " wins!");
        }
    }
    

Picture of the output

Project 2