1 write a bool function answerisyes that prompts the user to answer a question the q 5346946

1. Write a bool function, answerIsYes, that prompts the user to answer a question. The question is teh function's only parameter – a constant string refrence paramter. The parameter's default valute is “Is your answer 'Yes'?”. The function returns true if the user enters 'y', 'ye' or 'yes' using any combination of upper and lower case letters. The function returns false otheriwse. REwrite Hangman's main function to use the answerIsYes function appropriately.

2. Modify the playHangman function to display the number of wrong guesses remaining ot the player and the number of right guesses remaining to the player.

3. The playHangman function invokes the findAll function and the mapChar function. One of these functions is necessary but is used poorly and one is unnecessary but is used well. Remove the unnecessary function and use the necessary function better.

4. The function, computeMaximumWrongGuessCount returns the length of the secret word as the maximum wrong guess count. Rewrite the function to give more wrong guesses to very short words and fewer wrong guesses to very long words. For example, the maximum wrong guess count for very short words could be a multiple of the word length; the maximum wrong guess count for very long words could be a fraction of the word length and the maximum wrong guess count for words of moderate length could be the word length.

5. The readGuessFromUser function does not work very well if the user enters a guess that contains more than one character or is not a letter of the alphabet. Modify the function to fix these problems. Your solution should explicity reject characters that are not letters of the alphabet. You must provide your own solution to the problem of guesses with more than one character. Consider doing problem 6 at the same time.

6. The playHangman function and the readGuessFromUser function manipulate the already guessed list inefficiently. The playHangman function adds a guess to the already guessed list twice and the readGuessFromUser function uses the already guessed list to reject guesses that have already been guessed. Rewrite the two functions: the playHangman function will not add a guess to the already guessed list even once and the readGuessFromUser function will add the guess to the alreadly guessed list whenever necessary. Finally, change the readGuessFromUser function from a function that returns a char and that has one constant reference parameter to a void function that has two reference parameters. Consider doing problem 5 at the same time.

7. The getRandomSecretWord function reads the secret word file twice. The first time it counts the number of words in the secret word file and the second time it reads the secret word from the file. Change the program: make it read the words from a file into an array of strings exactly once and then randomly select a word from the array each time the player plays hangman. Replace the getRandomSecretWord function with two functions. One function, readSecretWords, reads the words from a file into an array. The other function, getRandomWord, which randomly selects a secret word from an array. Write the functions to use as few global variables and constants as possible and to use as many parameters as possible. Consider doing problem 8 at the same time.

8. Replace statements that open files with statements that invoke the appropriate openStream functions. Consider doing problem 7 at the same time.

——————————————————

#include

#include

#include

#include

#include

#include

using namespace std;

const char end_of_line = 'n';

const string empty_string = “”;

//hangMan prototypes

int computeMaximumWrongGuessCount(const string&);

string getRandomSecretWord(const string& = “secretwords.txt”);

void playHangman(const string&);

char readGuessFromUser(const string&);

//string prototypes

string fill(int length, char fill_char = '*');

int findAll(const string& source, char target);

int mapChar(char map_char, const string& original, string& edited);

string toupper(const string& original);

string tolower(const string& original);

string trim(const string& original);

//file manipulation protoypes

bool openStream(string& stream_name, ifstream& stream, const string& prompt = “Enter an input file name: “, int try_count = 4);

bool openStream(string& stream_name, ofstream& stream, const string& prompt = “Enter an output file name: “, int try_count = 4);

//random prototypes

void init(unsigned);

void init();

int nextInt();

int nextInt(int);

int nextInt(int, int);

int main()

{

init();//initialize random number generator

cout

string answer;

cin >> answer;

answer = trim(answer);

answer = toupper(answer);

while(answer[0] == 'Y')

{

string random_secret_word = getRandomSecretWord();

playHangman(random_secret_word);

cout

cin >> answer;

answer = trim(answer);

answer = toupper(answer);

}

return 0;

}

string getRandomSecretWord(const string& file_name)

{

ifstream secret_words;

secret_words.open(file_name.c_str());

assert(secret_words);

int secret_word_counter = 0;

string secret_word;

secret_words >> secret_word;

while(!secret_words.eof())

{

secret_word_counter++;

secret_words >> secret_word;

}

secret_words.close();

secret_words.clear();

int secret_word_number = nextInt(1, secret_word_counter);

secret_words.open(file_name.c_str());

assert(secret_words);

for(int counter = 1; counter

secret_words >> secret_word;

secret_words.close();

return secret_word;

}

void playHangman(const string& original_secret_word)

{

string secret_word = trim(original_secret_word);

int secret_word_length = secret_word.length();

assert(secret_word_length > 0);

secret_word = toupper(secret_word);

string mask = fill(secret_word_length);

cout

int maximum_wrong_guess_count = computeMaximumWrongGuessCount(secret_word);

string already_guessed_list = empty_string;

int wrong_guess_count = 0;

int right_guess_count = 0;

do

{

char guess = readGuessFromUser(already_guessed_list);

int n_occurences = findAll(secret_word, guess);

mapChar(guess, secret_word, mask);

if(n_occurences > 0)

{

right_guess_count = right_guess_count + n_occurences;

already_guessed_list = already_guessed_list + guess;

cout

if(n_occurences == 1)

cout

else

cout

}

else

{

wrong_guess_count = wrong_guess_count + 1;

already_guessed_list = already_guessed_list + guess;

cout

}

}

while(wrong_guess_count

if(right_guess_count == secret_word_length)

cout

else

cout

}

int computeMaximumWrongGuessCount(const string& parameter_word)

{

string word = trim(parameter_word);

int word_length = word.length();

assert(word_length > 0);

return word_length;

}

char readGuessFromUser(const string& already_guessed_list)

{

cout

char guess;

cin >> guess;

guess = toupper(guess);

while(already_guessed_list.find(guess) != -1)

{

cout

cout

cin >> guess;

guess = toupper(guess);

}

return guess;

}

string fill(int length, char fill_char)

{

string original = “”;

for(int pos = 0; pos

original = original + fill_char;

return original;

}

int findAll(const string& source, char target)

{

int target_count = 0;

//int target_pos = source.find(target, 0);

int target_pos = source.find(target);

while(target_pos != -1)

{

target_count++;

target_pos = source.find(target, target_pos + 1);

}

return target_count;

}

string toupper(const string& original)

{

string edited = empty_string;

int length = original.length();

for(int pos = 0; pos

{

char c = original[pos];

c = toupper(c);

edited = edited + c;

}

return edited;

}

string tolower(const string& original)

{

string edited = empty_string;

int length = original.length();

for(int pos = 0; pos

{

char c = original[pos];

c = tolower(c);

edited = edited + c;

}

return edited;

}

string trim(const string& original)

{

int length = original.length();

int left_pos = 0;

while(left_pos

left_pos++;

int right_pos = length – 1;

while(left_pos

right_pos–;

string edited = empty_string;

int edited_length = right_pos – left_pos + 1;

edited = original.substr(left_pos, edited_length);

return edited;

}

int mapChar(char map_char, const string& original, string& edited)

{

int map_count = 0;

int length = min(original.length(), edited.length());

int pos = original.find(map_char);

while(-1

{

edited[pos] = map_char;

pos = original.find(map_char, pos + 1);

map_count = map_count + 1;

}

return map_count;

}

void init()

{

srand(unsigned(time(NULL)));

}

void init(unsigned seed)

{

srand(seed);

}

int nextInt()

{

return rand();

}

int nextInt(int upper_bound)

{

assert(upper_bound > 0);

return rand() % (upper_bound + 1);

}

int nextInt(int lower_bound, int upper_bound)

{

if(lower_bound > upper_bound)

{

int t = lower_bound;

lower_bound = upper_bound;

upper_bound = t;

}

int range = upper_bound – lower_bound + 1;

int next_int = rand() % range + lower_bound;

return next_int;

}

bool openStream(string& stream_name, ifstream& stream, const string& prompt, int max_tries)

{

int tries = 0;

do

{

cout

cin >> stream_name;

stream.open(stream_name.c_str());

tries++;

}

while(!stream.good() && tries

return stream.good();

}

bool openStream(string& stream_name, ofstream& stream, const string& prompt, int max_tries)

{

int tries = 0;

do

{

cout

cin >> stream_name;

stream.open(stream_name.c_str());

tries++;

}

while(!stream.good() && tries

return stream.good();

}

6.

5.

"Get 15% discount on your first 3 orders with us"
Use the following coupon
FIRST15

Order Now