Homework Assignment 5

A Simple Game: Nim

Computer Science II
Object-Oriented Programming

Task 1 Due: Tuesday, February 22, at 8:00 AM
Project Due: Thursday, March 3, at 8:00 AM


This assignment asks you to design your own object-oriented program and build it "from scratch". Your task is to write a program that plays the game of Nim. For now, we will use a text-based interface, though soon in the course we will be able to implement a nice graphical front end to the program.


  1. Decompose the project requirements into smaller stories.

    Study the rules of the game and the project requirements defined in the specification below. This specification is very simple, listing only a half dozen required behaviors. But some of those behaviors are much too big to implement directly, for example, "Play a game between the program and the user."

    From the specification, draft a few scenarios in which two players play the game. Decompose each scenario down into a set of small behaviors that you might be able to implement one at a time.

    Create a list of these small behaviors, in the order you think you might want to implement them.

  2. Design and implement a program to play the game of Nim.

    Use your list of behaviors to design and write your program. Then write Java classes to implement your design. You may create whatever classes you think best model the problem.

  3. Prepare a "read me" file for your program.

    Create a file named readme.txt that describes the design of your Nim program. It should briefly describe each class, the relationships among the classes, and how to use your program.

We will evaluate your program first of all on whether and how well it provides the specified behaviors. Keep in mind: a program that compiles and runs but is incomplete is worth more than a "more complete" program that does not compile or that does not run correctly.

Secondly, we will evaluate your program on the quality of the design and implementation. How well are the responsibilities apportioned to independent objects that collaborate to solve the problem? How flexible is the design in the face of reasonable changes to the specification?

Your code and your "read me" file are the only ways you have to communicate your design and implementation, so try to use them to communicate well.

Programming Advice

As always, write your program in the style we have been using all semester. First, write a test that expresses what you would like your code to do. Then write the code to do it. Finally, refactor your code to improve the design. Use the GenerateTest program to create your test classes.

Take small steps, and your tests will give you feedback as soon as possible. For each scenario you create, you should be able to write several tests. You almost cannot take a step that is *too* small!


(Task 1) By the due date and time, submit the following file:

stories.txt must be a plain text file listing your project stories in order. Your file may not be a Word file, a Notepad file, or a file in any other format. I should be able to open it in vi and read it in a normal fashion.

Bring your hardcopy to class on the due date.

(Full project) By the due date and time, submit the following files:

Be sure that your submission follows all homework submission requirements.

A Specification for a Program to Play Nim

The game of Nim consists of two players and a pile of stones. The players alternate turns, which consist of picking up one, two, or three stones. The player who picks up the last stone loses the game.

Your program should allow the user to play a number of games of Nim against the computer, displaying results of the play to the user.

Your initial implementation provide only a few simple behaviors:

Later, we will add more interesting behaviors to the program, such as different strategies for the computer player, computer-versus-computer and human-versus-human play, a graphical interaction, and so on. The program you write for this assignment should not do all of these things, but if you design your program in a way that takes advantage of interacting objects then you will find it is easier to add them later.

Your computer player can choose its move in any way you wish, even randomly. (It turns out that, in Nim, one player always has a perfect strategy available, depending on the number of stones that can be picked up on a turn. This means that the player can always win, no matter what her opponent does. If you know or discover this strategy, then you can implement it if you want. But you don't have to!)

Eugene Wallingford ..... wallingf@cs.uni.edu .... February 16, 2005