CSCI 150, Spring 2003

Home | | Course Schedule | | Assignments | | Lecture Notes

CSCI 150 Homework 2

The Game of Nim

Due Friday, February 7, at the beginning of class.

Introduction
The game of Nim is described in your textbook on pages 7 - 9. Briefly, the game board consists of some number of squares in a line. Each player is allowed to place 1, 2, or 3 markers on the board in turn. Player 1 plays with X's and player 2 plays with O's. The winner is the player who places their marker in the last square. The figure on page 9 of the textbook shows a decision tree for this game when the board has 7 squares. In this project, you will create and implement a decision tree for a 6 square game of Nim. The rules are exactly the same as for the 7 square version, except there are only 6 squares on the board. The winner is the player who places a piece on the 6th square.

Decision Tree
First you will create a decision tree for the 6-square game of Nim. The user makes the first move, placing 1, 2 or 3 X's on the board. If the user plays 1 or 2 X's, the computer should respond with a single O. If the user plays 3 X's, the computer should play three O's (to win and end the game). In the first 2 cases, the user makes a second move of 1, 2, or 3 X's. The computer should then respond with an appropriate move to win the game or report that the user has won. Note that there should be only one condition under which the user wins. Draw the decision tree for this 6-square game of Nim.

Program Description
The program will implement the decision tree you develop above. The user will be prompted for their move, and they should enter 1, 2 or 3 X's. Depending on the user's entry, the program will make a move of 1,2 or 3 O's as shown in your decision tree. When the last (6th) box is filled, the program reports the winner.

Project Specifications
Place all your code in a file named <username>_nim.pas, where refers to your own username. For example, Professor Royden's file would be named croyden_nim.pas.

  1. Declare variables to hold the user's first and second moves.
  2. Prompt the user to enter his/her first move, and read in the response.
  3. Based on the response, the computer should report the current board status.
  4. The computer should then tell its move, followed by a report of the new board status.
  5. If the game is not yet over, the computer should then prompt for the user's second move, and repeat steps 3 and 4.
  6. The computer should report the winner.
  7. Write your code to assure that the text that appears on the monitor looks like that displayed in the section below called Specifications for Output.

Specifications for Documentation and Format.
Fully correct documentation for Project 1 must include:

Specifications for Output.
Directly below, you can see an example of the display that will appear on the monitor screen during the prompting for user input and the computer responses for two sample games. Note that the response after the user's second move differs slightly from the decision tree in the textbook, since the computer reports the final board status before announcing the winner. Characters in the boxes indicate user input.

To Submit Your Finished Project:
1. Hand in your decision tree. Hand this to your instructor in class on the project's due date.

2. Hand in a hard copy of the file <username>_nim.pas. Hand this to your instructor in class on the project's due date.

3. Hand in a printout of 4 sample games like those shown above. Hand this to your instructor in class on the project's due date.

4. Print your name at the top of the cover page and staple it to the top of your hard copy.

5. In addition to the hard copy listed above, email your <username>_nim.pas file to me at croyden@mathcs.holycross.edu. The subject line should read: csci150 hw2 <your_username>.

Get started early and have fun!