<< Return to Homework Page

Lab 5 : The Game Of Nim

Lab Overview

What is Nim?

The game of NIM is played with a pile of N coins. Two players take turns drawing coins from the pile. A player can take 1 or 2 coins from the pile on his or her turn. The player to take the last coin wins.

This Lab

In this lab we'll be writing a program to help us play the game of Nim. Two human players will actually be playing, but the program will keep track of the pile of coins for us. This means that for each player's turn, the program will ask each player how many coins they wish to take: 1 or 2. It will also make sure that the player requests a valid amount (it has to be 1 or 2 and it has to be less than the current number of coins left in the pile). This also means that the program will have to update the number of coins in the pile whenever a player takes some. Also, the program should notice when the pile of coins has run out and then declare which player won (whoever took the last coin). Finally, as we've done before, we'll prompt the users of the program to see if they want to play again.

What you're doing in this lab will be pretty similar to the last lab. You're going to be looping through, performing an action as long as the user keeps responding with "yes". For that action, you'll have to loop again. This time it will be to let players take coins each turn instead of to calculate sums and averages. To help with the process of making this game, we'll build it up: one method at a time.

Step 1: Getting Coins

The first part of our program will be to get a vaild amount of coins from a user. We'll do this with a method. A method is just a block of code that will take in some values, called parameters, do some work, and finally return a value to whatever called the method. We were introduced to methods in Thursday's lecture. For this method we'll be writing code for the following:

	// Goal:   * Get the user to take a valid # of coins
	//           (1 or 2 AND less than the current pile size)
	//         * Return the valid # of coins that the user chose.
	// Input:  String - name of the player taking coins,
	//         int 	- current # of coins in the pile
	// Output: int	- number of coins to be taken by the player (1 or 2)
	public static int getNumber(String player, int total){
		// YOU fill in code here
	}

Notice that the method takes in two parameters: a String and an int. The String is the name of the player who is currently supposed to take coins from the pile. This is so you can call the player by name when you print messages to them. The int is the number of coins currently in the pile. You'll need this to help you make sure that the user takes a proper number of coins. In the game of Nim a user is only supposed to take 1 or 2 coins at a time. You'll have to keep prompting the user to enter 1 or 2 as their choice of coins to take until they finally give you a valid response.

ALSO, you'll have to make sure that the player doesn't take too many coins. With physical coins, if there's only 1 coin left then the player can only take that last coin. But with our program, if there's only 1 coin left the user still might request to take 2. You'll have to prompt them until they finally choose to take only 1. Note that this also means you'll have to tell the user to take 1 or 2 coins AND tell the user how many coins are left to be taken. Otherwise they won't know what to do!

Finally, once you've gotten the user to give you a valid number of coins, you need to return that number back to whatever called your method. As shown in lecture, this is done with the return key word, such as in this trivial example:

	public static int returnNumber(){
		return 3;
	}

To complete this first method, first see if you can just get it to compile and run. This will require you to put in at least 1 line of code using the return key word to return a value of type int. This is because the method is promising to return an int in it's method header:

	public static int getNumber(...

Try filling in a return line to remove any complaints from the compiler (the red errors). Then try calling your method from within the standard main method that we're used to. It would also help to print out the value returned by your method so you can see it. This could be done like so:

	public static void main(String[] args){
		int test = getNumber("bob", 10);
		System.out.println(test);
	}

The test code above should successfully run your method. After you've got the test code successfully calling your method (an important first step) then go about filling in your code for your method. After I had filled in my code for my version of the method, the output looked something like this:

Step 2: One Player's Turn

With the first method completed and working, we're ready for the next step. Now we're going to write a method that handles a single turn for a single player. Here's what the method header looks like:

	// Goal:  * Tell the user that it's his/her turn,
	//        * Get the user to take a valid # of coins
	//        * Print out a winning message if the user took the last coin in the pile
	//        * Return the new size of the pile (after the user took a valid # of coins)
	// Input:  String - name of the player taking coins,
	//         int 	- current # of coins in the pile
	// Output: int	- new current # of coins in the pile
	public static int nextTurn(String player, int total){
		// YOU fill in code here
	}

This method takes in the name of the player and the total number of coins currently in the pile. The parameters are the same as for our previous method. This time we're doing a bit more, though.

  1. We first have to tell the player that it's their turn (using the player's name might be helpful here).
  2. Next we get the player to select a proper number of coins from the pile.
  3. Then, if the player has taken the pile's last coin, we print out a message telling that player that he/she has won.
  4. Finally, given the number of coins taken by the current player, we return the updated number of coins left in the pile. This is done using the return key word.

There's actually not a lot of work to be done in those 4 steps. As with our first method, first just try to call this method and get it to successfully return a dummy value. Once you know how to use the method, then start thinking about what you need to do, and then as a last step actually write the code. In particular, step 2 is something you've already done before. Instead of rewriting the code, for full credit on this assignment you'll need to make use of the first method you wrote, nextTurn. Try calling this method and letting it do the work for you.

Step 3: Playing Just One Game

By this point we've got a method that will allow one player to play a single turn of Nim. To do this it needed the name of the current player, and the current amount of coins in the pile. The method also returned the new number of coins in the pile after the current player took some. Using that method, we're now going to write a method to play an entire game of Nim (but just one game).

First things first: look at the method header below and try calling it to get it any initial erros out of the way. You'll notice that this method has no parameters (it doesn't take anything in) and it doesn't return anything, indicated by the method header saying void instead of giving a return type.

	// Goal:  * State the main goal (or rules) of Nim.
	//        * Take in the names for 2 players.
	//        * Ask the users how many coins they wish to start the pile with.
	//        * Let the players take turns taking coins until the pile is empty.
	// Input:  Nothing
	//         
	// Output: Nothing
	public static void playNim(){
		// YOU fill in code here
	}

Once you've got the method header code put into your file of code, then start filling it in. You'll need to complete the 4 basic steps indicated in the comments above. Namely, you should state what Nim is all about, take in and store the names of the two players playing the game -- to make the game friendly :) -- and ask the players how many coins we should use to start the pile. Finally, just have the code loop through, letting the players take turns taking coins from the pile, stopping once the pile has become empty. Your code should ensure that the players take valid turns (1 or 2 coins, take no more coins than were currently left in the pile) and should declare who won when the game is over. Note that these requirements shouldn't be hard to satisfy if you make use of the methods you've already finished for this lab.

Step 4: Playing As Long As We Like

In the last step you wrote a method that allows two players to play a complete game of Nim. Now we need just one extra tweak: playing as many games as we like. For this last step you won't write another method and you won't modify any of the 3 methods you previously created. Now you're just going to rewrite the code inside the main method. That's the method that your program always starts from and the method where previously you were having to call your methods in order to test them.

	//	 Goal:  * Loop through as often as the user responds "yes"
	//  	        * If the user respond's "yes", play the game of Nim
	public static void main(String[] args) {
		// YOU fill in code here
	}

For this last part just do as the comments indicate: loop through, playing the game of Nim for 2 players, as long as the users continue to respond with "yes" whenever you ask them if you want to play again. Note that this should require only a little bit of code if you make use of the previous methods you created. Also, to get full credit for this assignment, you will have to call those methods rather than taking code out of them and trying to fit that into the main method.

Program Requirements

What To Turn In