CS120
Programming Assignment #3
Due on or before Friday September 13 at 11:59 PM.
For this assignment you will be modifying the NIM game from the course textbook.
The code is also posted on the course website in the course schedule.
Make the following changes to the NIM game:
- Change the output of the program to make it
more interesting. For example, change the winning and losing messages.
- Have the program begin by printing the rules
of the game.
- Include running commentary in the game - think of
typical sports commentary. Use
selection statements (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 close to winning.
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.
- 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. Currently the program removes a random number of items and may cause itself to lose unnecessarily.
In particular, change how the computer selects how many items to remove when there are 2, 3, or 4 items remaining.
- Remove the "magic" numbers from the code.
There's nothing particularly special about the limit of removing 3
objects. Changing the rules to permit a different number of objects to be
removed 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" numbers
(1 and 3) with variables. This makes the code easier to read (assuming
the variables are given meaningful names like MinimumMove and MaximumMove) 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. This
requires a loop that prints the correct number of the symbols in a row. A for
loop, described in Section A.3 of Appendix A and in Chapter 5, works
well, although a do-while loop can also be made to work. (A do-while
loop requires a new variable that "counts" from 1 to the number of objects remaining as part of the loop.)
- 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 and start a new game. If the player chooses
not to play again, the program should terminate.
As always, include a block of comments at the beginning of the programs
(or add to the existing block) that lists your name, 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: Turn in your program and sample output (generated using the
script command) showing that the program works.
Turn in both the program and the sample output via Canvas.