Guest User

Untitled

a guest
Jan 4th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <time.h>
  3. using namespace std;
  4.  
  5. const unsigned int maxMistakes = 5;
  6. const unsigned int wordsCount = 50;
  7.  
  8. void startGame();
  9. const char* getWord();
  10. char* createSequence(const char* word, int length);
  11. const unsigned getLength(const char* word);
  12. void printSequence(char* sequence, int length, int mistakes);
  13. void printHistory(char* history);
  14. char toLowerCase(char letter);
  15. const char getLetter();
  16.  
  17. int main() {
  18.     cout << "You are playing hangman. You are guessing one of the 50 U.S. states." << endl;
  19.     while (true) {
  20.         startGame();
  21.         char answer;
  22.         cout << "Do you want to play again?(y/n): ";
  23.         cin >> answer;
  24.         if (answer == 'n') {
  25.             cout << "You left the game!" << endl;
  26.             break;
  27.         }
  28.     }
  29.     return 0;
  30. }
  31.  
  32. void startGame() {
  33.     const char* word = getWord();
  34.     int length = getLength(word);
  35.  
  36.     char* sequence = createSequence(word, length);
  37.     char* history = new char[maxMistakes*length];
  38.     history[0] = NULL;
  39.  
  40.     int mistakes = 0, guesses = 0;
  41.  
  42.     cout << "Guess the word (max " << maxMistakes << " mistakes): " << endl;
  43.     while (true)
  44.     {
  45.         printSequence(sequence, length, mistakes);
  46.         printHistory(history);
  47.  
  48.         //in case you make more mistakes than you are allowed to
  49.         if (mistakes == maxMistakes) {
  50.             cout << "Sorry, you lost. The word was " << word << endl;
  51.             break;
  52.         }
  53.  
  54.         const char letter = getLetter();
  55.  
  56.         //fill the array with the previous guesses
  57.         history[guesses++] = letter;
  58.         history[guesses] = '\0';
  59.  
  60.         bool missed = true, //the letter is not from the word
  61.             guessed = true; //guessed the whole word
  62.  
  63.         for (int i = 0; i < length; i++) {
  64.             if (toLowerCase(word[i]) == letter) {
  65.                 sequence[i] = letter;
  66.                 missed = false; //made a right guess
  67.             }
  68.             if (sequence[i] == '_') {
  69.                 guessed = false; //there are still letters left to guess
  70.             }
  71.         }
  72.  
  73.         if (!missed) {
  74.             cout << "OK" << endl;
  75.         }
  76.         if (missed) {
  77.             cout << "No such letter!" << endl;
  78.             mistakes++;
  79.         }
  80.         if (guessed) {
  81.             cout << "Good job. You guessed the word " << word << "." << endl;
  82.             break;
  83.         }
  84.  
  85.         cout << endl;
  86.     }
  87.  
  88.     delete[] sequence;
  89.     delete[] history;
  90. }
  91.  
  92. void printSequence(char* sequence, int length, int mistakes) {
  93.     cout << "[" << mistakes << "/" << maxMistakes << "] ";
  94.     for (int i = 0; i < length; i++) {
  95.         cout << sequence[i] << " ";
  96.     }
  97. }
  98.  
  99. //prints the previous guesses
  100. void printHistory(char* history) {
  101.     int length = getLength(history);
  102.     cout << "History: ";
  103.     for (int i = 0; i < length; i++) {
  104.         cout << history[i] << " ";
  105.     }
  106.     cout << endl;
  107. }
  108.  
  109. char toLowerCase(char letter) {
  110.     if (letter >= 'A' && letter <= 'Z') {
  111.         return letter + ('a' - 'A');
  112.     }
  113.     return letter;
  114. }
  115.  
  116. //guess a letter from the word
  117. const char getLetter() {
  118.     char letter;
  119.     cout << "Choose a letter: ";
  120.     cin >> letter;
  121.  
  122.     return letter;
  123. }
  124.  
  125. //creates a sequence of underscores with the length of the searched word
  126. char* createSequence(const char* word, int length) {
  127.     char* sequence = new char[length + 1];
  128.     for (int i = 0; i < length; i++) {
  129.         if (word[i] == ' ') {
  130.             sequence[i] = ' '; //in case the string consists of more than one word
  131.         }
  132.         else {
  133.             sequence[i] = '_';
  134.         }
  135.     }
  136.     sequence[length] = '\0';
  137.     return sequence;
  138. }
  139.  
  140. //function that gets the length of a string
  141. const unsigned getLength(const char * word) {
  142.     int length = 0;
  143.     while (*word) {
  144.         length++;
  145.         word++;
  146.     }
  147.     return length;
  148. }
  149.  
  150. //function that gets random word from a list
  151. const char* getWord() {
  152.     const char* words[] = {
  153.         "Alabama", "Alaska" , "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Florida",
  154.         "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland",
  155.         "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire",
  156.         "New Jersey", "New Mexico", "New York","North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island",
  157.         "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington","West Virginia", "Wisconsin", "Wyoming"
  158.     };
  159.  
  160.     srand(time(NULL));
  161.     int random = rand() % wordsCount;
  162.     return words[random];
  163. }
Advertisement
Add Comment
Please, Sign In to add comment