(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 selections 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 you could print a message about someone being about to win. Or if there are lots of items left the program you could print a message about it still being early in the contest and anyone could win. (Chapter 3, Exercise 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 Currently the program removes a random number of items and may cause itself to lose unnecessarily. In particular, change how the computer selects when there are 2, 3, or 4 items remaining. (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 MinumumMove and MaximumMove) 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 the number of objects remaining 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. 1 include two #include
libraries #include using namespace std; int main() actual program // main() starts the // ---------------- Variable declarations int num_objects = 23; int current_player = 1; int move; // ----------- Beginning of the main game loop ------- ------ -- do { if (current_player == 1) { / conditional: if cout > move; input while (move 3 || move > num_objects) { cout > move; } else { else part of conditional do { make sure move is legal move = 1+ rand() & 3; // random computer move } while (move 3 || move > num_objects); cout 0); // ------------ end of the game loop ----- cout