CS 201 - Assignment 1

Due: Monday, September 8th, by 11:59 PM

Preliminary: not an official assignment description.

In this assignment, you will write a program to allow two players to play a game of Nim.

A game of Nim consists of one or more heaps of stones (or other objects). Players take turns taking 1 or more objects from one of the heaps. The last player to take a stone loses. So, the object of the game is to force the other player to take the last stone.

Example Session

Here is an example session (user input in bold):

How many heaps? 3
Size of heap 1? 3
Size of heap 2? 4
Size of heap 3? 5

Player 1's turn
Heaps: 1:3 2:4 3:5
Pick from which heap? 3
How many? 5

Player 2's turn
Heaps: 1:3 2:4 3:0
Pick from which heap? 2
How many? 4

Player 1's turn
Heaps: 1:3 2:0 3:0
Pick from which heap? 1
How many? 1

Player 2's turn
Heaps: 1:2 2:0 3:0
Pick from which heap? 1
How many? 1

Player 1's turn
Heaps: 1:1 2:0 3:0
Pick from which heap? 1
How many? 1
Player 2 wins

Getting started

Download CS201_Assign1.zip. Import it into your Eclipse workspace (File->Import...->General->Existing projects into workspace->Archive File). You will see a project called (surprise) CS201_Assign1 in the Package Explorer. The package edu.ycp.cs201.nim contains a file Nim.java which contains the main method of the program.

Requirements

At a minimum, your program should allow the game to be played with 1 to 3 heaps. (Supporting any number of heaps is good: see Grading section below.)

Use the example session above as a model for what the program's prompts and output should look like.

• If your program works correctly when given valid user input, it will receive a grade of up to 80.
• If your program works correctly, and checks to ensure that user input is valid (prompting the user to re-enter the value when an invalid input is received), it will receive a grade of up to 90.

I expect you to use good coding style (identation, naming of identifiers, etc.) I will deduct points for programs that use poor style.

The following extra credit features may be implemented:

• The game may be played with any number of heaps. (up to +5 points)
• A "graphical" representation of the heaps is drawn using text characters. (up to +5 points)
• Use classes and objects to represent the game, heaps, players, etc. (up to +10 points)
• A GUI is supported. (up to +10 points)
• The game may be played against a computer opponent. See the Wikipedia article for a discussion of an optimal strategy for playing the game. (up to +10 points)

Do not copy any code from other students, web sites, etc. The program you submit must be your work alone. Failure to follow the Academic Integrity policy specified in the syllabus will be treated as Academic Dishonesty, and could have severe consequences.