Advertisement
Guest User

Untitled

a guest
Jun 14th, 2014
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. using namespace std;
  2. #include <iostream>
  3. #include <vector>
  4. #include <fstream>
  5. #include <string>
  6. #include <ctime>
  7.  
  8. const string data = "Data.txt";
  9. void ReadData(vector<string> & BigList);
  10. void PrintVector(const vector<string> & V);
  11. void GenerateChallenge(vector<string> & SmallList, const vector<string> & BigList, int wordLn);
  12. int CheckAnswer(const string & word, const string & guess);
  13. void StartGame(const string & word);
  14.  
  15. int main()
  16. {
  17.     srand(time(NULL));
  18.     vector<string> BigList, SmallList;
  19.     string word;
  20.     int diff;
  21.     ReadData(BigList);
  22.     cout << "Difficulty (1-5)? ";
  23.     cin >> diff;
  24.  
  25.     GenerateChallenge(SmallList, BigList, diff*3);
  26.     PrintVector(SmallList);
  27.     word = SmallList[rand() % SmallList.size()];
  28.     StartGame(word);
  29.     return 0;
  30. }
  31.  
  32. void StartGame(const string & word) {
  33.     int attempts = 4;
  34.     string guess;
  35.  
  36.     while (attempts != 0) {
  37.         cout << "Guess (" << attempts << " left) ? ";
  38.         cin >> guess;
  39.         cout << CheckAnswer(word, guess) << "/" << word.length() << " correct" << endl;
  40.         attempts--;
  41.         if (CheckAnswer(word, guess) == word.length()) {
  42.             cout << "You win!" << endl;
  43.             break;
  44.         }
  45.     }
  46. }
  47.  
  48. void ReadData(vector<string> & BigList) {
  49.     ifstream fd(data);
  50.     string word;
  51.     while (!fd.eof()) {
  52.         fd >> word;
  53.         BigList.push_back(word);
  54.     }
  55.     fd.close();
  56. }
  57.  
  58. bool Exists(const vector<string> & SmallList, const string & word) {
  59.     for (size_t i = 0; i < SmallList.size();  i++)
  60.         if (SmallList[i] == word)
  61.             return true;
  62.     return false;
  63. }
  64.  
  65. void GenerateChallenge(vector<string> & SmallList, const vector<string> & BigList, int wordLn) {
  66.     unsigned long rndNumber;
  67.     while (SmallList.size() != 7) {
  68.  
  69.         int divideRnd = rand() % (BigList.size() / RAND_MAX + 1);
  70.         if (divideRnd == BigList.size() / RAND_MAX)
  71.             rndNumber = (rand() % (BigList.size() - divideRnd * RAND_MAX)) + (divideRnd * RAND_MAX);
  72.         else
  73.             rndNumber = rand() + divideRnd * RAND_MAX;
  74.  
  75.         for (unsigned long i = rndNumber; i < BigList.size(); i++) {
  76.             if (BigList[i].length() == wordLn && !Exists(SmallList, BigList[i])) {
  77.                 SmallList.push_back(BigList[i]);
  78.                 break;
  79.             }
  80.         }
  81.     }
  82. }
  83.  
  84. int CheckAnswer(const string & word, const string & guess) {
  85.     int count = 0;
  86.     for (size_t i = 0; i < word.length(); i++)
  87.         if (toupper(word[i]) == toupper(guess[i]))
  88.             count++;
  89.  
  90.     return count;
  91. }
  92.  
  93. void PrintVector(const vector<string> & V) {
  94.     for (size_t i = 0; i < V.size(); i++) {
  95.         cout << V[i] << endl;
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement