Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5. #include <time.h>
  6. #include <conio.h>
  7.  
  8. using namespace std;
  9.  
  10. string randomWord();
  11.  
  12. int main()
  13. {
  14.     int guess;
  15.     int maxGuess = 10;
  16.  
  17.     char letter;
  18.     int i;
  19.     int found_letters[6];
  20.     bool letter_match;
  21.    
  22.     for(;;)
  23.     {
  24.         guess = 0;
  25.         string word = randomWord();
  26.         for(i=0; i<(sizeof(found_letters)/sizeof(int)); i++)
  27.             found_letters[i] = -1;
  28.    
  29.    
  30.         //replace characters in string with '*'s
  31.         string hiddenword = word;
  32.         for (int i = 0; i < word.length(); i++)
  33.         {
  34.             hiddenword[i] = '*';
  35.         }
  36.  
  37.         while (guess < maxGuess)
  38.         {
  39.             letter_match = false;
  40.             cout << "Guess a letter: ";
  41.             cin >> letter;
  42.  
  43.             /*
  44.             for(i=0,int j=0; i<word.size(); i++)
  45.                 if( word[i] == letter )
  46.                 {
  47.                     letter_match = true;
  48.                     found_letters[j++] = i;
  49.                 }
  50.  
  51.             if(letter_match)
  52.             {
  53.                 cout << "You got a match\n";
  54.                 for(i=0; i<(sizeof(found_letters)/sizeof(int)); i++)
  55.                 {
  56.                     if(found_letters[i] != -1)
  57.                         hiddenword[found_letters[i]] = word[found_letters[i]];
  58.                 }
  59.             }
  60.             else
  61.             {
  62.                 cout << "sorry, no match\n";
  63.                 guess++;
  64.             }
  65.                 cout << hiddenword << endl;
  66.             */
  67.  
  68.  
  69.             int found = word.find(letter);
  70.             if (found != string::npos)
  71.             {
  72.                 hiddenword[found] = word[found];
  73.                 cout << "Congratz, you got a letter!" << endl;
  74.                 cout << hiddenword << endl;
  75.             }
  76.             else
  77.             {
  78.                 cout << "no letter for you.." << endl;
  79.                 cout << hiddenword << endl;
  80.                 guess++;// += 1;
  81.             }*
  82.  
  83.         }
  84.  
  85.         cout << "The game is over!" << endl;
  86.         cout << "The word was: " << word << endl;
  87.  
  88.         cin.get();
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94. int linesInFile()
  95. {
  96.     ifstream myfile ("words.txt");
  97.     string line;
  98.     vector <string> lines;
  99.     while(getline (myfile, line) ) lines.push_back(line);
  100.     return lines.size();
  101. }
  102.  
  103.  
  104. string randomWord()
  105. {
  106.     string* words;
  107.     words = new string[linesInFile()];
  108.  
  109.     //string words[128];
  110.     ifstream myfile ("words.txt");
  111.     while (myfile.good() )
  112.     {
  113.         for(int i = 0; i < 128; i++)
  114.         {
  115.             getline (myfile, words[i]);
  116.         }
  117.     }
  118.     srand(unsigned(time(NULL)));
  119.     return words[(rand() % linesInFile())];
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement