Project #2

Code

///jack newsom
///period 5
///program Nim
///filename Nim.java
///date completed 10/16/15

import java.util.Scanner;

public class Nim
{
    public static void main( String[] args )
    {
        Scanner bot = new Scanner(System.in);
        
        System.out.print("Player 1, enter your name: ");
        String p1 = bot.next();
        System.out.print("Player 2, enter your name: ");
        String p2 = bot.next();
        
        int p1Test = 0 , p2Test = 0, p1PileTest = 0, p2PileTest = 0;
        int a = 3;
        int b = 4;
        int c = 5;
        int total = a + b + c; 
        int gameover = 0;
        String winner, winnerNum = "";
    
        
        while ( gameover != 1 )
        {
            
            System.out.println("A: " + a + "\tB: " + b + "\tC: " + c);
            
            if ( total > 0 && gameover != 1 )
            {
            
                
                System.out.println( p1 + ", choose a pile: ");
                String p1Pile = bot.next();
                boolean boolP1Pile = p1Pile.equals("A");
                boolean boolP1Pile1 = p1Pile.equals("B");
                boolean boolP1Pile2 = p1Pile.equals("C");

                while ( boolP1Pile == false && boolP1Pile1 == false && boolP1Pile2 == false )
                {
                    System.out.println("Whoops, " + p1Pile + " wasn't an available option. Try again.");
                    System.out.print( p1 + ", choose a pile: ");
                    p1Pile = bot.next();
                    boolP1Pile = p1Pile.equals("A");
                    boolP1Pile1 = p1Pile.equals("B");
                    boolP1Pile2 = p1Pile.equals("C");

                }
                
                if ( boolP1Pile == true )
                {
                    p1PileTest = a;
                }
                else if ( boolP1Pile1 == true ) 
                {
                    p1PileTest = b;
                }
                else if ( boolP1Pile2 == true )
                {
                    p1PileTest = c;
                }
                
                while ( p1PileTest == 0 )
                {  
                    System.out.println("Whoops! Pile " + p1Pile + " is empty. Please choose a different pile.");
                    System.out.print( p1 + ", choose a pile: " );
                    p1Pile = bot.next();
                    boolP1Pile = p1Pile.equals("A");
                    boolP1Pile1 = p1Pile.equals("B");
                    boolP1Pile2 = p1Pile.equals("C");

                    while ( boolP1Pile == false && boolP1Pile1 == false && boolP1Pile2 == false )
                    {
                        System.out.println("Whoops, " + p1Pile + " wasn't an available option. Try again.");
                        System.out.print( p1 + ", choose a pile: ");
                        p1Pile = bot.next();
                        boolP1Pile = p1Pile.equals("A");
                        boolP1Pile1 = p1Pile.equals("B");
                        boolP1Pile2 = p1Pile.equals("C");

                    }
                    
                    if ( boolP1Pile == true )
                    {
                        p1PileTest = a;
                    }
                    else if ( boolP1Pile1 == true ) 
                    {
                        p1PileTest = b;
                    }
                    else if ( boolP1Pile2 == true )
                    {
                        p1PileTest = c;
                    }

                }


                System.out.println("How many to remove from pile " + p1Pile + ": ");
                int p1PileRemove = bot.nextInt();
                
                if ( boolP1Pile == true )
                {
                    p1Test = a - p1PileRemove;
                }
                else if ( boolP1Pile1 == true ) 
                {
                    p1Test = b - p1PileRemove;
                }
                else if ( boolP1Pile2 == true )
                {
                    p1Test = c - p1PileRemove;
                }
                
                
                while ( p1Test < 0 )
                {
                    System.out.println("Whoops! The number you selected is invalid (The piles cannot have negative amounts of counters). Try again");
                    System.out.println("How many to remove from pile " + p1Pile + ": ");
                    p1PileRemove = bot.nextInt();
                    
                    if ( boolP1Pile == true )
                    {
                        p1Test = a - p1PileRemove;
                    }
                    else if ( boolP1Pile1 == true ) 
                    {
                        p1Test = b - p1PileRemove;
                    }
                    else if ( boolP1Pile2 == true )
                    {
                        p1Test = c - p1PileRemove;
                    }    
                    
                }
                
                

                if ( boolP1Pile == true )
                {
                    a = a - p1PileRemove;
                }
                else if ( boolP1Pile1 == true ) 
                {
                    b = b - p1PileRemove;
                }
                else if ( boolP1Pile2 == true )
                {
                    c = c - p1PileRemove;
                }



                total = a + b + c; 
                
                                
            }
            else
            {
                gameover++;
                winnerNum = "p2";
            }
            
            System.out.println("A: " + a + "\tB: " + b + "\tC: " + c);
        
            if ( total > 0 && gameover != 1 )
            {
                
                               
                System.out.println( p2 + ", choose a pile: ");
                String p2Pile = bot.next();
                boolean boolP2Pile = p2Pile.equals("A");
                boolean boolP2Pile1 = p2Pile.equals("B");
                boolean boolP2Pile2 = p2Pile.equals("C");

                while ( boolP2Pile == false && boolP2Pile1 == false && boolP2Pile2 == false )
                {
                    System.out.println("Whoops, " + p2Pile + " wasn't an available option. Try again.");
                    System.out.print( p2 + ", choose a pile: ");
                    p2Pile = bot.next();
                    boolP2Pile = p2Pile.equals("A");
                    boolP2Pile1 = p2Pile.equals("B");
                    boolP2Pile2 = p2Pile.equals("C");

                }
                
                if ( boolP2Pile == true )
                    p2PileTest = a;
                else if ( boolP2Pile1 == true)
                    p2PileTest = b;
                else if ( boolP2Pile2 == true )
                    p2PileTest = c;
                    
                while ( p2PileTest == 0 )
                {
                    System.out.println("Whoops, pile " + p2Pile + " is empty. Please choose a different pile.");
                    System.out.print( p2 + ", choose a pile: ");
                    p2Pile = bot.next();
                    boolP2Pile = p2Pile.equals("A");
                    boolP2Pile1 = p2Pile.equals("B");
                    boolP2Pile2 = p2Pile.equals("C");
                    
                    while ( boolP2Pile == false && boolP2Pile1 == false && boolP2Pile2 == false )
                    {
                        System.out.println("Whoops, " + p2Pile + " wasn't an available option. Try again.");
                        System.out.print( p2 + ", choose a pile: ");
                        p2Pile = bot.next();
                        boolP2Pile = p2Pile.equals("A");
                        boolP2Pile1 = p2Pile.equals("B");
                        boolP2Pile2 = p2Pile.equals("C");

                    }
                }
                
                System.out.println("How many to remove from " + p2Pile + ": ");
                int p2PileRemove = bot.nextInt();
                
                if ( boolP2Pile == true )
                    p2Test = a - p2PileRemove;
                else if ( boolP2Pile1 == true )
                    p2Test = b - p2PileRemove;
                else if ( boolP2Pile2 == true )
                    p2Test = c - p2PileRemove;
                    
                while ( p2Test < 0 )
                {
                    System.out.println("Whoops! The number you selected is invalid (The piles cannot have negative amounts of counters). Try again");
                    System.out.println("How many to remove from pile " + p2Pile + ": ");
                    p2PileRemove = bot.nextInt();
                    
                    if ( boolP2Pile == true )
                        p2Test = a - p2PileRemove;
                    else if ( boolP2Pile1 == true )
                        p2Test = b - p2PileRemove;
                    else if ( boolP2Pile2 == true )
                        p2Test = c - p2PileRemove;
                      
                }
                    
                
                

                if ( boolP2Pile == true )
                {
                    a = a - p2PileRemove;
                }
                else if ( boolP2Pile1 == true ) 
                {
                    b = b - p2PileRemove;
                }
                else if ( boolP2Pile2 == true )
                {
                    c = c - p2PileRemove;
                }

                total = a + b + c; 
                
            }
            else
            {
                gameover++;
                winnerNum = "p1";
            }
            
        
        }
        
        boolean bWinnerNum = winnerNum.equals("p1");
        
        if ( bWinnerNum == true )
        {
            winner = p1;
        }
        else
        {
            winner = p2;
        }
            
        
        System.out.println( winner + ", there are no more counters left, so you WIN!");
        
    }
}      
    

Picture of the output

Assignment X