Lab 2: Invader Nim

Due by the end of class

The game Nim is a strategy game played with a number of rows of tallies. On each turn, a player is allowed to remove one or more tallies from a single row. The object of our version of the game is to force the other player to remove the last remaining tally. Our version of Nim is played with three rows, starting with three, five, or seven tallies, respectively.

| | |
| | | | |
| | | | | | |

The goal of this lab is to write a Nim player that will always play a legal move. Your Nim player does not need to be the most strategic player, but the student with the best player will win 1% of extra credit applied to his or her final grade.

Unlike most labs you have done before, you will not need to write an entire program. Instead, you only need to create a class that implements the Player interface.

Specification

Create a project called Lab2 and create a package in your project called nim. Download the following three files and add them to the nim package.

Now, add a new class to the nim package. The name of the class should be your user name followed by Player. For example, my class would be called Wittman1Player. Don't forget that Java convention is to start all class names with an upper case letter.

Moves

In our Java implementation of Nim, a move is represented by objects of the Move class. Move objects contain a row and a count. The row is a variable of type Row which is an enum. If you're unfamiliar with enum types, they represent a fixed number of constant values. In this case, Row objects can be one of the three constants Row.TOP, Row.MIDDLE, and Row.BOTTOM, corresponding to the top, middle, and bottom rows of the game of Nim. The count variable is an int giving the number of tallies that a move intends to remove from the row it specifies.

The Player interface

Consider the Player interface shown below.

package nim;

public interface Player {
	Move makeMove(int top, int middle, int bottom);
	String getName();
}

As you can see, the two public methods that a class that implements Player must provide are makeMove() and getName(). The getName() method is trivial. It should provide a human readable name of the player. Feel free to return whatever you want from this method, provided that the name identifies you, is likely to be unique, and does not contain any objectionable words or concepts.

The real work of this lab is writing the makeMove() method. This method is called on your player object every time it has an opportunity to play. It provides as arguments the current number of tallies on the top, middle, and bottom rows. Then, your player object is required to return a Move object with the move it wants to make based on those remaining tallies.

The only requirement for full points on this lab is that your player object returns a legal Move object, specifying a row and a number of tallies to remove from that row. The number of tallies must be greater than 0 and cannot be more than remain on the specified row.

I recommend writing a main() method in either your player class or some separate class. This method should create a Game object and then call its playGame() method, passing in true so that it produces output to the command line. The Game constructor takes two objects that implement the Player interface and the sizes of the top, middle, and bottom rows, which should be 3, 5, and 7, respectively, to match my test cases. You can create two of your own player objects and have them play against each other, or you can create an extra class or two that also implement Player, perhaps not as cleverly as your primary player does.

After all the labs have been turned in, I will run a tournament pitting all the player classes written by students against each other. The student whose class wins the tournament will earn 1% of extra credit applied to his or her final grade.

Hints

Nim is a solved game, meaning that there is a known strategy for the best play in any given situation. Once you have a functional player class, you're encouraged to look on the Internet for the best strategies and try to implement them.

Note: You may not copy code from the Internet, and you can't use any libraries other than java.util. You are allowed to use the Random class.

Turn In

Turn in your code by uploading the Java file containing your player class from the Lab2\src\nim folder inside your workspace folder to Blackboard. Do not upload the entire project. I only want the .java file that defines your player, and you can only turn in one.

All work must be done individually. Never look at someone else's code. Please refer to the course policies if you have any questions about academic integrity. If you have trouble with the assignment, I am always available for assistance.