Lab 1: Game of Nim

Objectives

Run getcs to set up the directory for this lab: Type ``getcs 160 1'' at the Unix prompt.

The game of Nim

Nim is a simple two-player game based on removing stones. The game begins with two piles of stones, numbered 0 and 1. Players alternate turns. Each turn, a player chooses to remove one, two, or three stones from some single pile. The player who removes the last stone wins the game.

Your job is to write a Java class Nim that plays a game of Nim against the user. Don't bother writing the program to play the game well - it just needs to play legally.

Program behavior

Stones for pile 0? 4
Stones for pile 1? 2

Pile 0: 4
Pile 1: 2
Which pile? 0
How many stones? 3

I take 1 from pile 1

Pile 0: 1
Pile 1: 1
Which pile? 0
How many stones? 1

I take 1 from pile 1

I win!

I've won 1, you've won 0.
Play again (0=no, 1=yes)? 1

Stones for pile 0? 2
Stones for pile 1? 0

Pile 0: 2
Pile 1: 0
Which pile? 0
How many stones? 2

You win.

I've won 1, you've won 1.
Play again (0=no, 1=yes)? 0

Thanks for playing!

Figure 1.1: Sample run of program.

Figure 1.1 contains a sample run of the program. Your program may choose different moves than this program, but the format of the output should be identical.

The first thing your program should do is to ask the human how many stones should be in each of the two piles.

In your program, the human player moves first. When it is the human's turn, the program should ask the user for two numbers: The first represents the pile the human chooses, and the second is the number of stones to remove. When it is your program's turn, your program can choose any move you find it convenient to program.

When the game has come to its conclusion, the program should output whether the human or the computer won. It then should display the cumulative record and ask the user whether to play again. (And if the user selects to play again, your program should.)

Your program should verify that each of the user's moves is valid. (That is, the pile must be legal, the user must remove between 1 and 3 stones, and the user must not remove more stones than the pile holds.) If the user's move is illegal, your program should repeatedly print a sensible error message and get a new input, until the user selects a legal move.

Your program need not verify the user's inputs at other times (when selecting the initial number of stones in each pile, or in choosing whether to repeat a game).

Program structure

Your program must include the following two methods.

static boolean playGame()
Plays a single game with the user, returning true if the program wins and false if the user wins.

static void main(String[] args)
Calls playGame(), displaying the game's result and the cumulative results, and repeats the process if the user asks to play again. The args parameter is simply ignored.

Normally, I would encourage you to add additional methods if you think it would lead to better program structure. For this program, however, I recommend against it, as this will likely require you to do things that we haven't covered yet.

Part of your lab grade will be based on your program's coding style - that is, how neat and understandable your program is. This includes descriptive names for variables, methods, and classes; consistent and good indentation; a judicious use of white space; and the occasional comment explaining what's going on within the program. (Generally, comments should explain overall structure, not individual statements. A rough guideline is that every 8--15 lines you should have a comment explaining the overall purpose of that set of lines. Also, every method should have a comment explaining its purpose.)