CS120
Programming Assignment #2A
Due Sunday February 4th at Midnight.
For this assignment you will be modifying the NIM game from the text. The
code is also posted on the course scehdule.
Make the following changes:
- Have the program begin by printing the rules
of the game.
- Include running commentary in the game - think of
typical sports commentary. Use
if statements to check the number of items remaining and print appropriate
messages. For example, if there are relatively few items left the program
could print a message about someone being about to win. Or if there are lots
of items left the program could print a message about it still being
early in the contest and anyone could win. Include at least three messages
triggered by different numbers of remaining items - you don't need
23 messages.
- Modify the program so that it doesn't lose if
it doesn't have to. For example, if there are exactly 3 items left the program
can win by removing 2 of them, right now it removes a random number of items
and loses half of the time.
In particular change how the computer moves for 2, 3, and 4 items.
- Remove the "magic" numbers from the code.
There's nothing particularly special about the limit of removing 3
objects. Changing the rules to a different number of objects requires changing
the conditions of the loops that control what is a valid move, and the
rand() statement so that the computer picks a number in the correct, new
range. The best way to do this is to replace the current "magic" number
(3) with variables. This makes the code easier to read (assuming
the variables are given meaningful names like maximum
move) and makes the code easier to change in the future because only the
two new variables need to be changed.
- Have the computer print a row of symbols,
like #'s or *'s, to represent the number of objects remaining. For example,
if there are 8 objects left the program should print:
########
This
requires a loop that prints num objects of the symbols in a row.
- Modify the program to play multiple games in a row
without having to restart the program each
time. After each individual game the program should ask the player if they
want to play again. If the player chooses to play again then the program
should loop back to the beginning.
The program should also keep score of how many games the player
won versus how many times the
computer won and print the new results after every game.
As always include a block of comments at the beginning of the programs
(or add to the existing block) that lists your names, section number, date,
and the assignment number. Also include comments on any parts of the
assignment you didn't finish or on any extra work you did.
Include this comment block at the beginning
of every assignment and lab.
Turn in: Print the program and sample output (generated using the
script command) showing that
the program works.
Turn in both
the program and the output.