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:
  1. Ask the players if they want to play Naive Nim or Normal Nim.
  2. Ask the players if they want to play in Advanced Mode or Beginner Mode.
  3. Having answered these questions, it should start game play.
  4. 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)
  5. 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).
  6. Now players should take turns picking a pile of stones and then removing some stones from that pile.
  7. After a player has removed stones from a pile, the piles should be displayed again. Then the other player gets a turn.
  8. Players continue to take turns until there are no more stones left.
  9. When there are no more stones the game is over and somebody wins.
  10. The winning player should be announced.
  11. The players should then be asked if they would like to play another game.

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:

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.