Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task D: Hangman Now we are ready to make Hangman! Oh and here is a dictionary you can use: dictionary.txt The game-loop is a sequence

Task D: Hangman

Now we are ready to make Hangman! Oh and here is a dictionary you can use:

dictionary.txt

The game-loop is a sequence of processes that run continuously as long as the game is running. The three main processes that occur in the game-loop are input, update, and render. Lucky for you, we have provided the game-loop. Make sure to include the following function in your program:

// game-loop for Hangman void gameLoop() { int difficulty, tries; string word, current; char letter; while (true) { cout << "Welcome to Hangman!" << endl; cout << "0. easy 1. normal 2. hard 3. exit Choose a difficulty: "; cin >> difficulty; while (difficulty < 0 || difficulty > 3) { cout << "Enough horseplay >_< ! 0. easy 1. normal 2. hard 3. exit Choose a difficulty: "; cin >> difficulty; } if (difficulty == 3) { cout << "If you're hangry, go grab a bite! See what I did there?" << endl; break; } word = getRandomWord(); current = maskWord(word); tries = getTries(difficulty); while (tries != 0) { cout << "Life: "; printAttempts(tries, difficulty); cout << endl << "Word: "<< current << endl; cout << "Enter a letter: "; cin >> letter; if (!revealLetter(word, letter, current)) { tries--; } if (current == word) { break; } if (tries == 2) { cout << "The part of speech of the word is "<< getPOS(word) << endl; } if (tries == 1) { cout << "Definition of the word: " << getDefinition(word) << endl; } } if (tries == 0) { cout << "The word is \"" << word << "\". Better luck next time! You're getting the ..ahem.. hang of it." << endl; } else { cout << "Congrats!!!" << endl; } } } 

The game-loop uses some helper functions that are not implemented yet. You will implement most of them. One of the helper function is getRandomWord(). In the game-loop, we are going to choose a random word from the dictionary. Use this function to get a random word from your dictionary:

// MAKE SURE YOU INCLUDE THIS LIBRARY! #include  string getRandomWord() { srand((unsigned) time(NULL)); int index = rand() % g_word_count; return g_words[index]; } 

Now that we have a random word from our dictionary, lets create some other functions that will be used in the game-loop. Add and implement the following functions to your .cpp file:

/* @param : The string with a word from the dictionary @return : string of "_" based on the number of characters in the given `word` @post : Return string of "_" based on the length of the given `word`. For example, if the word is "game", then the function would return "____". In other words, a string of four "_"s. */ string maskWord(string word); /* @param : The integer for the difficulty of the game (0 for easy, 1 for normal, and 2 for hard) @return : The number of tries given the `difficulty` (9 for easy, 7 for normal, and 5 for hard) @post : Return the number of tries based on given difficulty (0-easy: 9 tries, 1-normal: 7 tries, 2-Hard: 5 tries) */ int getTries(int difficulty); /* @param tries : The integer for remaining tries @param difficulty : The integer for the difficulty of the game (0 for easy, 1 for normal, and 2 for hard) @post : prints the number of lives left and number of lives used using "O" and "X". DO NOT PRINT AN ENDLINE For example : calling `printAttemps(2, 1)` would print "OOXXXXX". Based on given `difficulty`, we know the total tries is 7 (from `getTries(1)`). Also, the player has 2 `tries` remaining based on the given parameter. Therefore, the function prints two "O"s to indicate the remaining tries and 5 "X"s to indicate the tries that have been used (7-2=5) */ void printAttempts(int tries, int difficulty); /* @param word : The string word from the dictionary @param letter : The char letter that that will be revealed @param(&) current : The string representing a masked word @return : `true` if the `letter` exists in `word`, otherwise return `false` @post : If the given `letter` exists in `word` reveal the `letter` in `current` masked word and return `true`. Otherwise, return `false` For example : Let's say we have the following main function: int main(){ string w = "g___"; cout << revealLetter("good", 'o', "g___") << endl; cout << w << endl; } The first `cout` will print 1 because the letter 'o' exists in "good". Thus, the function returned `true`. The second `cout` will print "goo_". The variable `w` has been modified by the function to reveal all the `o`s in "good" resulting in "goo_" */ bool revealLetter(string word, char letter, string ¤t) 

Implement and test each function before moving on to the next. Once you have all the functions implemented correctly, try out your game by running gameLoop() from main() function. Submit only one .cpp file to gradescope without the main() function.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Instant Reference

Authors: Gruber, Martin Gruber

2nd Edition

0782125395, 9780782125399

More Books

Students also viewed these Databases questions