CS120
Programming Assignment #3
Due Friday September 15th at the beginning of lecture.
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:
- (Chapter 3, Exercise 1) Change the output of the program to make it
more interesting. For example, change the winning and losing messages.
- (Chapter 3, Exercise 2) Have the program begin by printing the rules
of the game.
- (Chapter 3, Problem 6) 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.
- (Chapter 3, Exeercise 6) 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, instead it removes a random number of items.
In particular change how the computer moves for 2, 3, and 4 items.
- (Chapter 3, Problem 1) 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" numbers
(1 and 3) with variables. This makes the code easier to read (assuming
the variables are given meaningful names like minimum move and maximum
move) and makes the code easier to change in the future because only the
two new variables need to be changed.
- (Chapter 3, Problem 4) 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 num objects 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 num objects as part
of the loop.)
- (Chapter 3, Exercise 7) 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.