Tuesday, July 22, 2014

Nim!

3.11 Project
The game of Nim is played as follows. You have n toothpicks to choose from. Player A can remove 1, 2, or 3 toothpicks. Player B can then remove 1, 2, or 3 toothpicks. This process repeats, alternating between players A and B, until there are no more toothpicks left. The player who removes the last toothpick is the loser. Assume that one of the players is human and the other player is the computer (your program).  Use a random generator to decide who goes first. The player that goes second chooses
the value of n, where n must be bigger than 20 and smaller than 30 (if this is the computer, then use a random generator to determine the value of n). Remember to print the remaining number of toothpicks on the screen after every move. If it is the human’s turn, then ask him or her how many toothpicks he or she wants to remove. After the last toothpick is removed, print “YOU WIN” or “YOU LOSE”.
Example program run (player input in italic) follows.
Welcome to my game of Nim.
This time you are going to go first.
I choose the total number of toothpicks to be 21.
Your Turn. There are 21 toothpicks left. How many toothpicks do you want to remove? 2
My turn. There are 19 toothpicks left. I, the Computer, will remove 3 of them.
Your Turn. There are 16 toothpicks left.
How many toothpicks do you want to remove? 3
My Turn. There are 13 toothpicks left. I, the Computer, will remove 3 of them.
Your Turn. There are 10 toothpicks left. How many toothpicks do you want to remove? 3
My Turn. There are 7 toothpicks left. I, the Computer, will remove 3 of them.
Your Turn. There are 4 toothpicks left. How many toothpicks do you want to remove? 3
My Turn. There is 1 toothpick left. I, the Computer, will remove it.
YOU WIN!
Try to design your program to play as smart as possible.

I vaguely remember doing a similar exercise as a homework assignment in my freshman year of college.  I don't remember how I did it then, or if I even did it correctly.  But today this was fairly easy to program.  In my first version the computer chose numbers randomly.  But then I went back and made it play with a strategy.  So now, if the user is randomly chosen to go first in the beginning, the computer will win no matter what the user inputs.  Which makes it a solved game and not very much fun.

import java.util.*;

public class Arithmetic {
public static void main(String[] args) {
boolean usersTurn;
int numtp, input, x;
Scanner keyboard = new Scanner(System.in);
System.out.println("Welcome to the game of Nim!");
//randomly decide who goes first
Random random = new Random();
   usersTurn = random.nextBoolean();
   
   
   //beginning of game
   if (usersTurn){//if user's turn first
    System.out.println("This time you are going to go first.");
    numtp = 21;//computer chooses number of toothpicks
    System.out.println("I choose the number of toothpicks to be "+numtp+".");
   }
   else{//if computer's turn first
    System.out.println("This time I am going to go first.");
    do{
    System.out.print("Choose how many toothpicks we should start with (21-29): ");
    numtp = keyboard.nextInt();
    }while(!(numtp<30&&numtp>20));//loop if not between 20 and 30
   }
   
   
//middle of game
while(numtp>0){//while there are more toothpicks
if(usersTurn){//if it's the user's turn
do{
System.out.print("Your turn.  There are "+numtp+" toothpicks left.  How many toothpicks do you want to remove?  (1-3): ");
input = keyboard.nextInt();
}while (!(input<=3&&input>=1));//repeat if not 1-3
numtp = numtp - input;//subtract that number from number of toothpicks
usersTurn = false;//next turn
}
else{//if it's the computer's turn
if(numtp==1){//if last toothpick
System.out.println("My turn. There is 1 toothpicks left. I will remove it.");
numtp = 0;
usersTurn = true;
break;//game over
}else if (numtp%4==2)//if 2, 6, 10, 14 etc
x=1;//remove 1
else if (numtp%4==3)//if 3, 7, 11, 15 etc
x=2;//remove 2
else if (numtp%4==0)//if 4, 8, 12, 16 etc
x=3;//remove 3
else//if 5, 9, 13 etc
x = random.nextInt(3)+1;//remove random
System.out.println("My turn. There are "+numtp+" toothpicks left. I will remove "+x+" of them.");
numtp = numtp - x;//subtract toothpicks
usersTurn = true;//next turn
}
}


//end of game
if (usersTurn)//if computer was the last player
System.out.println("You Win!");//user wins
else
System.out.println("I Win!");//user loses
keyboard.close();
}
}



No comments:

Post a Comment