126k views
3 votes
Create a class called Hangman. In this class, Create the following private variables: char word[40], progress[40], int word_length. word will be used to store the current word progress will be a dash representation of the the current word. It will be the same length as the current word. If word is set to ‘apple’, then progress should be "‑‑‑‑‑" at the beginning of the game. As the user guesses letters, progress will be updated with the correct guesses. For example, if the user guesses ‘p’ then progress will look like "‑pp‑‑" Create a private function void clear_progress(int length). This function will set progress to contain all dashes of length length. If this function is called as clear_progress(10), this will set progress to "‑‑‑‑‑‑‑‑‑‑". Remember that progress is a character array; don’t forget your null terminator. Create the following protected data members: int matches, char last_guess, string chars_guessed, int wrong_guesses, int remaining. Create a public function void initialize(string start). This function will initialize chars_guessed to a blank string, wrong_guesses to 0. Set remaining to 6. Use strcpy() to set word to the starting word passed as start (You can use start.c_str() to convert it to a character array). Set word_length to the length of word. Call clear_progress in this function.

User Demaris
by
6.9k points

1 Answer

4 votes

Answer:

See explaination

Step-by-step explanation:

#include <cstring>

#include <cstdio>

#include <string>

#include <array>

#include <random>

#include <algorithm>

struct RandomGenerator {

RandomGenerator(const size_t min, const size_t max) : dist(min, max) {}

std::random_device rd;

std::uniform_int_distribution<size_t> dist;

unsigned operator()() { return dist(rd); }

};

struct Gallow {

void Draw() const

%c%c%c\\"

"

bool Increment()

{

switch (++errors) '; break;

case 2: body[2] = '/'; break;

case 1: body[0] = '(', body[1] = ')'; break;

return errors < 6;

}

char body[7] { '\0' };

int errors { 0 };

};

struct Game {

void Draw() const

{

#ifdef _WIN32

std::system("cls");

#else

std::system("clear");

#endif

gallow.Draw();

std::for_each(guess.begin(), guess.end(), [](const char c) { std::printf("%c ", c); });

std::putchar('\\');

}

bool Update()

{

std::printf("Enter a letter: ");

const char letter = std::tolower(std::getchar());

while (std::getchar() != '\\') {}

bool found = false;

for (size_t i = 0; i < word.size(); ++i) {

if (word[i] == letter) {

guess[i] = letter;

found = true;

}

}

const auto end_game = [this](const char* msg) {

Draw();

std::puts(msg);

return false;

};

if (not found and not gallow.Increment())

return end_game("#### you lose! ####");

else if (found and word == guess)

return end_game("#### you win! ####");

return true;

}

RandomGenerator rand_gen { 0, words.size() - 1 };

const std::string word { words[rand_gen()] };

std::string guess { std::string().insert(0, word.size(), '_') };

Gallow gallow;

static const std::array<const std::string, 3> words;

};

const std::array<const std::string, 3> Game::words{{"control", "television", "computer"}};

int main()

{

Game game;

do {

game.Draw();

} while (game.Update());

return EXIT_SUCCESS;

}

User Nicasio
by
7.1k points