Advertisement
Guest User

hungman header

a guest
Jan 28th, 2013
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #ifndef HEADER_H_INCLUDED
  2. #define HEADER_H_INCLUDED
  3. #include <fstream>
  4. #include<string>
  5. #include<vector>
  6. using namespace std;
  7.  
  8.  
  9. int getRandomNumber(int level,int lines[])
  10. {
  11.     int low, high;
  12.     if(level == 1)
  13.     {
  14.         low = 2;
  15.         high = lines[1];
  16.     }
  17.     else if(level == 2)
  18.     {
  19.         low = lines[1]+1;
  20.         high = lines[2];
  21.     }
  22.     else if(level == 3)
  23.     {
  24.         low = lines[2]+1;
  25.         high = lines[3];
  26.     }
  27.     srand ( time(NULL) );
  28.     int line = rand() % (high - low) + low;
  29.     return line;
  30. }
  31.  
  32. string getWord(const char fileName[], int level, int lines[])
  33. {
  34.     ifstream fd(fileName);
  35.     string temp;
  36.     for(int i = 1; i<=getRandomNumber(level, lines); i++)
  37.     {
  38.         getline(fd,temp);
  39.     }
  40.  
  41.     return temp;
  42. }
  43.  
  44.  
  45. void fileRead(int lines[], const char fileName[])
  46. {
  47.     ifstream fin(fileName);
  48.     for(int i = 0; i<4; i++)
  49.     {
  50.         fin >> lines[i];
  51.     }
  52. }
  53.  
  54. int selectLevel()
  55. {
  56.     int temp;
  57.     cout << "Iveskite norima sunkumo lygi(1-3): " << endl;
  58.     cin >> temp;
  59.     if(temp <1 || temp >3) selectLevel();
  60.     else return temp;
  61. }
  62.  
  63. char enterGuess(char &guess)
  64. {
  65.     string temp;
  66.     cout << "Enter your guess: " << endl;
  67.     getline(cin, temp);
  68.     if(temp.size()>1)
  69.     {
  70.         cout << "Error! Tip: Enter only 1 letter" << endl;
  71.         enterGuess(guess);
  72.     }
  73.     else guess = temp[0];
  74. }
  75.  
  76. bool isCorrect(string word, char guess, string &text)
  77. {
  78.     bool temp = false;
  79.     for(int i = 0; i <word.size(); i++)
  80.     {
  81.         if(word[i] == guess){
  82.                 text[i] = guess;
  83.                 temp = true;
  84.         }
  85.     }
  86.     if(temp == true) return true;
  87.     return false;
  88. }
  89.  
  90. void drawGuesses(char guesses[], int gIndex)
  91. {
  92.     for(int i = 0; i <=gIndex;i++)
  93.     {
  94.         if(guesses[i] >= 97 && guesses[i] <=122){
  95.         cout << guesses[i];
  96.         }
  97.     }
  98. }
  99. void draw(int &lives, string word, char guesses[], int &gIndex, string &text)
  100. {
  101.     system("cls");
  102.     cout << text << endl;
  103.     cout << "\t Lives: " << lives << endl;
  104.     cout << "\t Guesses: ";
  105.     drawGuesses(guesses, gIndex);
  106.     cout << endl;
  107.     char guess;
  108.     enterGuess(guess);
  109.     isCorrect(word, guess, text);
  110.     if(!isCorrect(word,guess,text)) lives--;
  111.     guesses[gIndex] = guess;
  112.     gIndex++;
  113. }
  114. bool isSessionOver(int lives, string text, bool &pergale)
  115. {
  116.     if(lives == 0){
  117.        pergale = false;
  118.        return true;
  119.     }
  120.     else if(true){
  121.         for(int i = 0; i<text.size();i++)
  122.         {
  123.             if(text[i] == '_') return false;
  124.        }
  125.        pergale= true;
  126.        return true;
  127.     }
  128.     else return false;
  129.     }
  130.  
  131.     void endGame(bool &gameOver)
  132.     {
  133.         char choice;
  134.         cin >> choice;
  135.         if(choice == 'Y' || choice =='y')
  136.         {
  137.             gameOver = false;
  138.         }
  139.         else if(choice == 'N' || choice =='n')
  140.         {
  141.             gameOver = true;
  142.         }
  143.         else{
  144.             cout << "Error! Please try entering it again." << endl;
  145.             endGame(gameOver);
  146.         }
  147.     }
  148. #endif // HEADER_H_INCLUDED
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement