CS139 Algorithm Development |
Purpose: From the Horstmann textbook. "You
will write a program in which the computer plays against a human
opponent.
Generate a random integer between 10 and 100 to denote the initial size
of the pile.
Generate a random integer between 0 and 1 to decide whether the
computer or the human takes the first turn. Generate a random integer
between 0 and 1 to decide
whether the computer plays smart or stupid. In stupid mode the computer
simply takes a random legal value (between 1 and n/2) from the pile
whenever it has a turn.
In smart mode the computer takes off enough marbles to make the size of
the pile a power of two minus 1—that is, 3, 7, 15, 31, or 63.
That is always a legal move,
except when the size of the pile is currently one less than a power of
two. In that case, the computer makes a random legal move. "
Objectives - At the conclusion of this exercise the student will demonstrate that they can:
Deadlines
You have covered the material in Chapter 4 & 5 of Gaddis.
Program
Behavior Reference |
Output printf statement | parameter 1 | parameter 2 | parameter 3 |
1 | "Welcome to the game of Nim\n\n" | |||
3 | "What is your name: " | |||
3, 4 | "\n" - After reading in the value. | |||
4 | "Enter a large integer to 'start' the process: " | |||
4 - Error | "This is not an integer. Try again.\n" | |||
Toolkit - change All error messages should echo the bad value as this |
"Bad value %s\n" | The bad value read from the keyboard. This would be directly followed by the error message. | ||
6c | "The marbles in the pile are %d.\n %s starts.\n" | The number of marbles generated | Either the name of the human player or "Computer" | |
6ei | "%s, how many marbles do you want? (1-%d) " | The name of the human player | The maximum number of marbles the person can choose | |
6ei - error - non integer | "This is not an integer. Try again.\n" | |||
6ei - Error - not in range | "%d is not in the range of 1 - %d. Try again.\n" | The number the human entered | The maximum number they can enter. | |
6f | "%s: took %d\t%s: took %d\t%d remain\n" | The name of the first player, number of marbles | The name of the second player, number of marbles | The number of marbles remaining |
6h | "1 marble left. %s loses.\n" | The name of the player that will have to take the last marble | ||
6i | "That was fun. Play again? " | |||
6i ii | "Bye\n" | |||
After all input | "\n" |
This work must conform to the JMU Honor Code and the specific requirements of this class. NO help may be provided by any student to another student. Authorized help is limited to your textbook, the TAs for any CS139 or CS239 section, and the professor for your section. See collaboration policy.