/**************************************************** Write a simple version of hangman, in which the user enters the word he'll "guess", and then the user gets to start guessing letters. After each guess, the word is printed out with *s instead of unguessed letters. ****************************************************/ #include #include using namespace std; // Prints each character of 'word', // but if mask[i] is false, then the ith character // prints an asterisk instead of the actual character. // 'N' is the length of 'word' and the size of the mask array void printMaskedWord(string word, bool *mask, int N); // Check every letter of 'word'. // If j'th letter of word is 'letter', // then set j'th value of 'mask' to true void updateMask(string word, bool *mask, int N, char letter); // Returns true if every element of array 'mask' is true bool checkWin(bool *mask, int N); int main() { // Get word to guess string word; cout << "Enter word to guess: "; cin >> word; // Set the mask array - mask[i] is true if the // character word[i] has been guessed. The mask // must be allocated, and initialized to all false int N = word.length(); bool *mask = new bool[N]; for(int i = 0; i < N; i++) mask[i] = false; while (true) { // Print out word, showing letters guessed so far printMaskedWord(word, mask, N); // Get next letter char letter; cout << "Enter letter: "; cin >> letter; // Mark any letters that match updateMask(word, mask, N, letter); // Did they win? if (checkWin(mask, N)) { printMaskedWord(word, mask, N); cout << "You win!"; break; } } return 0; } // Prints each character of 'word', // but if mask[i] is false, then the ith character // prints an asterisk instead of the actual character. // 'N' is the length of 'word' and the size of the mask array void printMaskedWord(string word, bool *mask, int N) { for (int i=0; i