Advertisement
ToastyStoemp

Hangman Updated

Dec 18th, 2014
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <fstream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <ctime>
  8. #include <stdlib.h>
  9.  
  10. int main()
  11. {
  12.     srand(time(NULL));
  13.     std::cout << "Loading..." << std::endl;
  14.     std::ifstream stream("words.txt");
  15.     std::vector<std::string> words;
  16.     std::string line;
  17.  
  18.     while (std::getline(stream, line))
  19.     {
  20.         words.push_back(line);
  21.     }
  22.  
  23.     std::string solution = words.at((rand() * rand())%words.size());
  24.     std::string current;
  25.     current.assign(solution.length(), '-');
  26.     std::string entered;
  27.     int lives = 10;
  28.  
  29.     while (!(lives == 0 || solution == current))
  30.     {
  31.         system("cls");
  32.         bool found = 0;
  33.         std::cout << current << std::endl;
  34.         std::cout << "enter a letter, you currently have: " << lives << " lives" << std::endl;
  35.         std::cout<< "Wrong characters: " << entered << std::endl;
  36.         std::string c;
  37.         std::cin >> c;
  38.         std::string old  = current;
  39.         if (c.size() > 1)
  40.         {
  41.             if (c == solution)
  42.             {
  43.                 current = solution;
  44.             }
  45.             else
  46.             {
  47.                 --lives;
  48.             }
  49.         }
  50.         else
  51.         {
  52.             for (int i = 0; i < solution.length(); ++i)
  53.             {
  54.                 char s = solution.at(i);
  55.                 if (s == c[0])
  56.                 {
  57.                     current[i] = s;
  58.                 }
  59.             }
  60.             if (old == current)
  61.             {
  62.                 --lives;
  63.                 entered += c[0];
  64.             }
  65.         }
  66.     }
  67.     if (!(lives == 0))
  68.     {
  69.         std::cout << " You have finished the game, conGratz !!! *fireworks*" << std::endl;
  70.     }
  71.     else
  72.     {
  73.         std::cout << "You loose, BAKA" << std::endl;
  74.         std::cout << "The word you were looking for was: " << solution << std::endl;
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement