Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <array>
- #include <random>
- #define MAX_TRIES 6
- #define EXIT_SUCCESS 0
- #define EXIT_ERROR 84
- #define WORDS 5
- #define GALLOW_0 "_________\n| |\n| \n|\n|\n|\n|\n"
- #define GALLOW_1 "_________\n| |\n| 0\n|\n|\n|\n|\n"
- #define GALLOW_2 "_________\n| |\n| 0\n| |\n|\n|\n|\n"
- #define GALLOW_3 "_________\n| |\n| 0\n| /|\n|\n|\n|\n"
- #define GALLOW_4 "_________\n| |\n| 0\n| /|\\\n|\n|\n|\n"
- #define GALLOW_5 "_________\n| |\n| 0\n| /|\\\n| /\n|\n|\n"
- #define GALLOW_6 "_________\n| |\n| 0\n| /|\\\n| / \\\n|\n|\n"
- using namespace std;
- std::string get_random_word()
- {
- std::mt19937 rng;
- std::uniform_int_distribution<std::mt19937::result_type> random_index(0, WORDS - 1);
- std::array<const char *, WORDS> words = {
- "HELLO",
- "WASSUP",
- "SWEG",
- "IDKKEV",
- "FORSENBAJS"
- };
- rng.seed(std::random_device()());
- return (words.at(random_index(rng)));
- }
- void print_gallow(int status)
- {
- switch (status)
- {
- case 0:
- cout << GALLOW_0;
- break;
- case 1:
- cout << GALLOW_1;
- break;
- case 2:
- cout << GALLOW_2;
- break;
- case 3:
- cout << GALLOW_3;
- break;
- case 4:
- cout << GALLOW_4;
- break;
- case 5:
- cout << GALLOW_5;
- break;
- default:
- cout << GALLOW_6;
- break;
- }
- cout << '\n';
- }
- int main()
- {
- std::string word;
- std::string guessed;
- std::string buffer;
- char last;
- int word_len;
- int tries;
- int i;
- char c;
- bool is_char;
- bool won;
- tries = 0;
- word = get_random_word();
- won = false;
- word_len = word.size();
- while (tries < MAX_TRIES && !won)
- {
- won = true;
- is_char = false;
- while (!is_char)
- {
- cout << "Enter a letter: ";
- cin >> buffer;
- if (buffer.size() == 1)
- {
- last = buffer[0] &= '_';
- is_char = true;
- }
- }
- if (word.find_first_of(last) == string::npos)
- {
- tries++;
- if (tries < MAX_TRIES)
- cout << "Oops! You missed!\nRemaining tries: " << MAX_TRIES - tries << "\n";
- }
- else if (guessed.find_first_of(last) == string::npos)
- guessed += last;
- cout << "Word:";
- i = 0;
- while (i < word_len)
- {
- c = word.at(i);
- if (guessed.find_first_of(c) != string::npos)
- cout << c;
- else
- {
- won = false;
- cout << "_";
- }
- i++;
- }
- cout << '\n';
- cout << "Guessed: ";
- if (guessed.size() == 0)
- cout << "None\n";
- else
- cout << guessed << "\n";
- print_gallow(tries);
- }
- if (tries < MAX_TRIES)
- cout << "Congrats you win!\n";
- else
- cout << "Sorry, you lose!\n";
- return (EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement