Project 2

Code

/// Name: Ryan Volko
/// Period: 7
/// Program Name: Nim
/// File Name: Nim.java
/// Date Finished: 2/26/2016

import java.util.Scanner;

public class Nim
{
	public static void main( String[] args )
	{
		Scanner keyboard = new Scanner(System.in);
		
		int a = 3, b = 4, c = 5, z, y;
		String P1, P2, x;
        x = " ";
        z = 0;
        
        System.out.print("Player 1, enter your name: ");
            P1 = keyboard.next();
            System.out.print("Player 2, enter your name: ");
            P2 = keyboard.next();
        
		while ( a >= 0 && b >= 0 && c >= 0 )
		{
           
            z++;
            System.out.println("A: " + a + "    B: " + b + "    C: " + c);
            
        if ( z % 2 == 1 )
        {
            System.out.print(P1 + ", choose a pile: ");
            x = keyboard.next();
            
            if ( x.equals("A"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                a = a - y;
                while ( a < 0 )
                {
                    a = a + y;
                    System.out.println("Try again");
                    System.out.print(P1 + ", choose a pile: ");
                    x = keyboard.next();
                    if ( x.equals("A"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                a = a - y;
                    }
                    
                }
            }
            else if ( x.equals("B"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                b = b - y;
                while ( b < 0 )
                {
                    b = b + y;
                    System.out.println("Try again");
                    System.out.print(P1 + ", choose a pile: ");
                    x = keyboard.next();
                    if ( x.equals("B"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                b = b - y;
                    }
                    
                }
                
            }
            else if ( x.equals("C"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                c = c - y;
               
            }
            else 
            {
                System.out.println("You didn't enter correctly try again.");
            }
        }
        else if ( z % 2 == 0 )
        {
            System.out.print(P2 + ", choose a pile: ");
            x = keyboard.next();
            
            if ( x.equals("A"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                a = a - y;
                while ( a < 0 )
                {
                    a = a + y;
                    System.out.println("Try again");
                    System.out.print(P2 + ", choose a pile: ");
                    x = keyboard.next();
                    if ( x.equals("A"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                a = a - y;
                    }
                    
                }
            }
            else if ( x.equals("B"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                b = b - y;
                while ( b < 0 )
                {
                    b = b + y;
                    System.out.println("Try again");
                    System.out.print(P2 + ", choose a pile: ");
                    x = keyboard.next();
                    if ( x.equals("B"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                b = b - y;
                    }
                    
                }
            }
            else if ( x.equals("C"))
            {
                System.out.print("How many to remove from pile " + x + ": ");
                y = keyboard.nextInt();
                c = c - y;
                
               
            }
            else 
            {
                System.out.println("You didnt enter correctly try again.");
            }
        }               
	}
        if ( z % 2 == 1 )
        {
            System.out.println(P1 + ". There are no more moves so you won");
        }
        if ( z % 2 == 0 )
        {
            System.out.println(P2 + ". There are no more moves so you won");
        }
}
}
    

Picture of the output

Project 2