CSIS 2610: Programming and Problem Solving 
Programming assignment 3: Repetition Statements

Due 3/21

Objectives

This assignment will give you more practice with different types of looping statements.

Requirements

You are to write a program which plays a version of the game nim. The rules for this game are as follows:

o       The game starts with a pile of “sticks”.

o       Each turn, a player must take between 1 and 3 sticks from the pile.

o       The player who takes the last stick from the pile loses.

 

In this case, your program will play the user against the computer. At the beginning of each game the user is to be prompted for the initial number of sticks (more on that below).

Each turn, the following is to happen:

o       Your program should print a number of “vertical lines” representing the number of remaining sticks. For example, if there are 7 sticks left, the following should be printed:

| | | | | | |

o       Your program should prompt the user for how many sticks they want to remove, and decrease the number of sticks by that number.

o       If the number of remaining sticks is zero, the game is over, and the user loses.

o       Otherwise, the computer chooses how many sticks it will remove (see below for the algorithm you are to use), and prints that out to the user. If there are no sticks left, the game is over and the user wins.

The computer should use the following algorithm to determine how many sticks to remove:

o       If there is 1 stick left, remove it (and the user wins).

o       If there are between 2 and 5 sticks left, remove (number of sticks + 1) % 3 + 1 sticks.

o       If there are more than 5 sticks left, remove (number of sticks) % 3 + 1 sticks.

Here are a couple of examples to help you out:

How many sticks do you want to start with (0 to quit): 7

| | | | | | |

How many sticks do you wish to remove: 2

The computer removes 1 sticks.

| | | |

How many sticks do you wish to remove: 3

The computer removes 1 sticks.

You win!!

How many sticks do you want to start with (0 to quit): 9

| | | | | | | | |

How many sticks do you wish to remove: 2

The computer removes 2 sticks.

| | | | |

How many sticks do you wish to remove: 1

The computer removes 3 sticks.

|

How many sticks do you wish to remove: 1

You lose!

Input validation

Your program must also validate its input. Specifically, it must make sure that the number of sticks the user removes is between 1 and 3, and not more than the remaining sticks.

For this program, you are to use a loop to keep prompting the user until they enter a legal number of sticks to remove.

Playing more than one game

Your program should also use a loop to play as many games as the user wants. After each game, the program should print the following:

How many sticks do you want to start with (0 to quit):  

If the user answers 0 (or any negative number), program should end. Otherwise, the program should start a new game with that many sticks.

In addition, the program should keep track of the number of games won and lost by the user, and print those out at the end of the program. For example:

How many sticks do you want to start with (0 to quit): 0

You won 3 games and lost 2 games.

Documentation

The program you turn in should be well documented. Along with everything else, make sure that your loops are documented, describing:

o       What is being done in the loop body.

o       What the loop condition represents – that is, when does the program keep looping/exits the loop.

o       What initialization is being done inside the loop.

In addition, make sure that your nested loops are indented properly. This is an important part of your grade.

What to Turn In