Date: Mon, 11 Nov 1996 17:01:26 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Mon, 07 Oct 1996 00:18:37 GMT Content-length: 9909
So I decided to write a detailed document describing how to do the assignment. Much of the code that you will need to use in your program is given to you right here; all you have to do is assemble it into a coherent structure and fill in the details.
Note: The code in this document does not address a small problem: treating both upper- and lower-case letters as the same. Instead, it treats the two versions of each letter as totally different letters. This is not correct behavior for the assignment, though. You are responsible for modifying the program to handle upper- and lower-case letters. If the user guesses the letter 'e' (either upper- or lower-case), your program should "reveal" all letters 'e' in the secret phrase (both upper- and lower-case). However, I recommend that you do this only after you have the rest of the program working.
Now, if the user guesses the letter "o", then the partially-revealed secret phrase will be modified; for each "o" in the secret phrase Howdy Doody!, the corresponding character in the partially-revealed secret phrase is changed from the underscore character to the letter "o".
string random_secret_phrase ();
This prototype tells us that the random_secret_phrase function has no inputs, and returns (outputs) a string. So we call this function by simply putting an empty pair of parentheses after its name:
random_secret_phrase ();However, if we just wrote this line in our program, it would call the function, but throw away its return value! This is definitely not what we want. In general, we must use the return value of a function in some way. For random_secret_phrase, its return value is a string which is randomly selected from a list. The appropriate thing to do with this string is to store it in a variable, so we can refer to that value whenever we need to.
string secret_phrase = random_secret_phrase ();This creates a variable secret_phrase of type string, and initializes it with the value returned by the random_secret_phrase function call.
bool isalpha (char ch);This prototype tells us that the isalpha function takes one input, a character, and returns a boolean value (true or false). If the given character is a letter, it returns true.
string partial_phrase = secret_phrase; for (int i = 0; i < partial_phrase.length(); i++) { if ( isalpha (partial_phrase [i]) ) partial_phrase [i] = '_'; }This code loops over each of the characters in the partially-revealed secret phrase; each character that is a letter is changed to an underscore. Since we know the number of characters in the partially-revealed secret phrase is partial_phrase.length(), we can use a simple counting loop. We use the bracket operator to access each character; the index variable i tells us which character we are working on.
To decide whether a letter has already been guessed, use the function char_in_string. This function has the following prototype:
bool char_in_string (char ch, string str);This tells us that the function has two inputs (one character and one string), and returns a boolean (i.e., true or false) value. If it returns true, then one of the characters of the given string is the character ch.
So, if you call this function, and supply as inputs the guess and the string of previously-guessed letters, it will tell you whether or not the guess has been made before. The function call will look like:
char_in_string (guess, guessed_letters)But, as usual, we need to do something with the return value of this function! Since char_in_string returns a boolean value, we can use the return value as the condition of an if statement:
if ( char_in_string (guess, guessed_letters) )
guessed_letters += guess;Remember that the += is just a shorthand for the following:
guessed_letters = guessed_letters + guess;The character guess is automatically converted to a string, and the + operator concatenates the two strings into a single string.
if ( char_in_string (guess, secret_phrase) )
for (int i = 0; i < secret_phrase.length(); i++) { if (secret_phrase [i] == guess) partial_phrase [i] = secret_phrase [i]; }
num_chances == 0The second condition can be checked by comparing the secret phrase to the partially-revealed secret phrase. If they are the same, then the player has won the game. This can be checked with the boolean expression
secret_phrase == partial_phraseThe game ends when either of these two boolean expressions is true:
num_chances == 0 || secret_phrase == partial_phraseHowever, the game must continue as long as neither of these conditions is true. Thus the condition in our loop would be:
while (num_chances > 0 && secret_phrase != partial_phrase) {