Assignemnt Project 2: Playing a Card Game of Nim
Code
/// Name: Sean Harrison
/// Period: 7
/// Program name: Nim
/// File Name: Nim.java
/// Date Finished: 2/11/2016
import java.util.Scanner;
public class Nim
{
public static void main( String[] args )
{
Scanner keyboard = new Scanner(System.in);
String Player1, Player2, choice;
int Number, A, B, C, P1, P2, counter, winner, loser;
A = 3;
B = 4;
C = 5;
counter = 0;
P1 = 0;
P2 = 0;
System.out.println( "Player 1 enter your name : " );
Player1 = keyboard.next();
System.out.println( "Player 2 enter your name : " );
Player2 = keyboard.next();
System.out.println( " A: 3 B: 4 C: 5 " );
while ( A + B + C != 0 )
{
System.out.println( Player1 + " which pile would you like to take from " );
choice = keyboard.next();
System.out.print( " How many would you like to remove" );
Number = keyboard.nextInt();
if ( choice.equals("A" ))
{
counter++;
P1++;
A = (A - Number);
}
else if ( choice.equals("B" ))
{
counter++;
P1++;
B = B - Number;
}
else
{
counter++;
P1++;
C = C - Number;
}
System.out.println( Player2 + " which pile would you like to take from " );
choice = keyboard.next();
System.out.println( " How many would you like to remove" );
Number = keyboard.nextInt();
if ( choice.equals( "A" ))
{
counter++;
P2++;
A = A - Number;
}
else if ( choice.equals("B" ))
{
counter++;
P2++;
B = B - Number;
}
else
{
counter++;
P2++;
C = C - Number;
}
}
if ( P1> P2 )
{
System.out.println( Player2 + " , Congrajulations you have won Nim. " );
}
if ( P2 == P1 )
{
System.out.println( Player1 + " , Congrajulations on defeating " + Player2 + " . " );
}
}
}
Picture of the output