CSE 142 Homework 3
Due Tuesday, July 18th, 10:00 PM
Paper turn in Wednesday, July 19th, 5:00 PM
Worksheet and Turn in Report are due the same time as the Paper turn in (Wednesday)
Nim - The Power Returns is your newest assignment at HHGG. The success of your earlier projects caused your appointment
to head the creation of the first real game for HHGG. Slated for release in the third week of July, 2000, Nim holds great promise.
Homework Requirements:
Program -- The game Nim, described below
A Worksheet -- available here
A Turn In Description -- details here
You need to do both the program described below, as well as the
loops worksheet. The worksheet will
help you practice writing complex loops. The worksheet is to
be turned in, on a separate piece of paper, at the same time as
the paper turn in for the program. Lastly, you will also have
to a turn in description. This involves
writing a bit about what you did in this assignment. No late work.
Overview
Nim is a simple two-player game. There are two versions, Naive Nim, and Normal Nim, and you will
be programming both. The game is played as follows: there are 3 piles
of stones, and players take turns removing 1 or more stones from a single
pile. In Naive Nim, the player to take the last stone wins. In Normal
Nim, the player to take the last stone loses. Using your knowledge of
functions, loops, conditionals, and more, you will be implementing Nim.
This program is more complex and complicated than the others, so get started early.
Quick Overview
Specifications
First you will decide what kind of game the users wants to play.
After that, the players can play as many games as they want.
Your program should do the following:
- Ask the players if they want to play Naive
Nim or Normal Nim.
- Ask the players if they want to play in Advanced
Mode or Beginner Mode.
- Having answered these questions, it should start game play.
- First, the game should generate 3 piles
of stones, with a random number of stones
in each pile. Each pile should have at most 25
stones in it. (We'll explain how to do random numbers below)
- The number of stones in a pile should be displayed by using the | character. Each pile will correspond to a row of | characters, with as many | as there are stones in the pile. (The | character can be
found on most keyboards near the Enter key, and often looks like two hyphens -- rotated 90 degrees).
- If the players are in Beginning Mode, then at the end of each row
should be displayed the number of stones in that pile. These numbers
should be aligned properly. This means that they
should appear above each other, a little to the right of the | characters.
- If the players are in Advanced Mode, then no such totals should be given.
- Now players should take turns picking a pile of stones and then removing some stones from that pile.
- This should be done by first prompting a player for a pile to pick from.
- If the user enters an invalid pile number (something besides 1,2,3) or a pile with zero (0) stones in it, they should be prompted again.
- Once they have picked a valid pile, players should be asked how many
stones they want to remove from the pile.
- Players must remove at least one (1) stone
from a pile, and cannot remove more stones than there are in the pile.
- If a player tries to remove an incorrect number of stones (e.g.
a negative number), they should be prompted again.
- After a player has removed stones from a pile, the piles should be displayed again. Then the other player gets a turn.
- Players continue to take turns until there are no more stones left.
- When there are no more stones the game is over and somebody wins.
- If playing Naive Nim, the player who takes the last stone wins.
- If playing Normal Nim, the player who takes the last stone loses.
- The winning player should be announced.
- The players should then be asked if they would like to play another game.
- If they answer yes, then play proceeds starting from step 3 above.
- Otherwise the program ends.
Look at a few sample executions of the game.
Suggestions and Hints
You will definitely need to use functions and conditionals, and probably
functions which return values.
To produce random numbers you need to do
the following:
- Include the stdlib.h and time.h.
- Seed the random function.
- Use the random function.
To include the appropriate libraries, the first 3 lines of code in
your program should be:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
To ensure that each time you run the program you get a different
set of random numbers, the first line of your program after you
declare variables in main() should be:
srand((int)time(NULL));
And to generate a random number, you should call the rand() function.
rand() is a function which generates a random number between 0 and
about 2 billion, so you should use it with modulus.
For example, to get a number between 0 and 11 (inclusive) you would do:
int random_number;
random_number = rand() % 12;
Sample Executable
Download and run the sample PC executable hw3.exe.
See some sample runs of the program.
Getting Started
We aren't providing you with any starter code, but we will give you
a default workspace.
Download the MSVC 6.0 self-extracting archive.
MSVC 5.0 here
For other platforms, the starting workspace looks like this.
Submitting Your Work
Use the turnin page. Don't forget the worksheet as well as the turnin description
You may turn in the program as many times as you wish. Only the
last version is graded, and only the receipt from this last version
should be handed in.