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.
- Declare variables to hold the user's first and second moves.
- Prompt the user to enter his/her first move, and read in the response.
- Based on the response, the computer should report the current board status.
- The computer should then tell its move, followed by a report of the new board status.
- If the game is not yet over, the computer should then prompt for the user's second
move, and repeat steps 3 and 4.
- The computer should report the winner.
- 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:
- A file prologue at the top of your program file. The prologue must contain the
program title, your name, class and section, the date and the assignment number. The
prologue must also contain a short description of the program.
- Each variable should have a comment stating the purpose of the variable.
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.
Sample Game 1:
What is your move?
The board is X.
I play O.
The board is XO.
What is your next move?
The board is XOX.
I play OOO.
The board is XOXOOO.
I win!
Sample Game 2:
What is your move?
The board is XX.
I play O.
The board is XXO.
What is your next move?
The board is XXOXXX.
You win!
Sample Game 3:
What is your move?
The board is XX.
I play O.
The board is XXO.
What is your next move?
The board is XXOXX.
I play O.
The board is XXOXXO.
I win!
Sample Game 4:
What is your move?
The board is XXX.
I play OOO.
The board is XXXOOO.
I win!
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!