Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <ctime>
- class Gallow {
- private:
- int lives = 12;
- char answer = 0;
- std::string word;
- std::string hiddenWord;
- int succesCounter = 0;
- int counter = 0;
- inline bool ifCorrect() const {
- for (int i = 0; i < word.length(); i++) {
- if (word[i] == answer)
- return true;
- }
- return false;
- }
- public:
- Gallow() = default;
- std::string loadRandFile(std::string path) {
- int numLines = 9;
- std::ifstream fin(path);
- int lineNum = 0;
- int randLine = rand() % numLines;
- while (getline(fin, word)) {
- if (lineNum++ == randLine) break;
- }
- return word;
- }
- inline void setAnswer() {
- std::cout << "Enter the letter" << std::endl;
- std::cout << ">>> ";
- std::cin >> answer;
- for (int i = 0; i < word.length(); i++) {
- if (word[i] == answer) {
- hiddenWord[i] = answer;
- succesCounter++;
- }
- }
- if (succesCounter > 0) {
- std::cout << "Correct letter [+]!" << std::endl;
- std::cin.ignore();
- system("clear");
- std::cout << "Current word: ";
- showHiddenWord();
- std::cout << std::endl;
- succesCounter = 0;
- std::cout << "Count of your lives: " << lives << std::endl;
- } else {
- std::cout << "Incorrect letter [-]!" << std::endl;
- std::cin.ignore();
- system("clear");
- std::cout << "Current word: ";
- showHiddenWord();
- std::cout << std::endl;
- lives--;
- std::cout << "Count of your lives: " << lives << std::endl;
- if (lives <= 0) {
- std::cout << "You died!" << std::endl;
- std::exit(1);
- }
- }
- }
- inline void setWord() {
- std::cout << "Enter the word" << std::endl;
- std::cout << ">>> ";
- loadRandFile("game.txt");
- std::cin >> word;
- }
- inline void showHiddenWord() {
- if (!(counter > 0)) {
- for (int i = 0; i < word.length(); i++) {
- hiddenWord.insert(hiddenWord.begin(), '*');
- }
- }
- std::cout << hiddenWord << std::endl;
- counter++;
- }
- inline bool isWordCorrect() const {
- if (hiddenWord == word)
- return true;
- else
- return false;
- }
- inline int getLifes() const { return lives; }
- };
- int main() {
- srand(time(NULL));
- Gallow *gallow = new Gallow;
- // gallow->loadRandFile("game.txt");
- gallow->setWord();
- std::cin.ignore();
- system("clear");
- gallow->showHiddenWord();
- while (!gallow->isWordCorrect()) {
- gallow->setAnswer();
- }
- std::cout << "You win!" << std::endl;
- delete gallow;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement