KeeganT

Hangman!

Dec 8th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. using namespace std;
  7.  
  8. string word;
  9.  
  10. void randomWord()
  11. {
  12.     srand(time(NULL));
  13.     int num=rand()%27+1;
  14.     string words[28];
  15.     string text;
  16.     ifstream file("words.txt");
  17.     for(int c=0;file>>text;c++)words[c]=text;
  18.     word=words[num];
  19. }
  20.  
  21. int main()
  22. {
  23.     randomWord();
  24.     int count=0;
  25.     string text;
  26.     string theWord[word.length()];
  27.     string guessedLetters="";
  28.     for(int c=0;c<word.length();c++)theWord[c]+="?";
  29.     int hangman=0;
  30.     while(hangman!=7)
  31.     {
  32.         if(count==0)cout<<"Your word is "<<word.length()<<" letters long!"<<endl;
  33.         if(count==0)for(int c=0;c<word.length();c++)cout<<theWord[c]<<" ";
  34.         else
  35.         {
  36.             for(int c=0;c<word.length();c++)cout<<theWord[c]<<" ";
  37.             cout<<"\t\tGuessed letters: ";
  38.             for(int c=0;c<count;c++)cout<<guessedLetters[c];
  39.         }
  40.         cout<<endl;
  41.         if(count==0)cout<<"If you would like to guess a letter, type in one,\nif you would like to guess the word, type 'guess' ";
  42.         else cout<<"Enter your guess: "<<endl;
  43.         cin>>text;
  44.         while(text.length()>1&&text!="guess")
  45.         {
  46.             cout<<"Invalid input! Please enter a chracter or 'guess': ";
  47.             cin>>text;
  48.         }
  49.         while(guessedLetters.find(text)!=string::npos)
  50.         {
  51.             cout<<"You already guessed this letter! Enter another one: ";
  52.             cin>>text;
  53.         }
  54.         guessedLetters+=text;
  55.         if(text=="guess")
  56.         {
  57.             string guess;
  58.             cout<<"What do you think the word is? ";
  59.             cin>>guess;
  60.             if(guess==word)
  61.             {
  62.                 cout<<"Congratulations! You guessed the word correctly!"<<endl;
  63.                 return 0;
  64.             }
  65.             else
  66.             {
  67.                 cout<<"Sorry! That's incorrect! The word was "<<word<<". Better luck next time."<<endl;
  68.                 return 0;
  69.             }
  70.         }
  71.         if(word.find(text)!=string::npos)
  72.         {
  73.             int num=word.find(text);
  74.             theWord[num]=text;
  75.             int num2=word.rfind(text);
  76.             theWord[num2]=text;
  77.         }
  78.         else
  79.         {
  80.             cout<<"Letter not found!"<<endl;
  81.             hangman++;
  82.         }
  83.         count++;
  84.     }
  85.     cout<<"Sorry! You lost!"<<endl;
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment