Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <time.h>
  4. using namespace std;
  5.  
  6. char toUpper(char c) {
  7.     return (c >= 'a' && c <= 'z' ? 'A' + c - 'a' : c);
  8. }//end of toUpper
  9.  
  10. bool contains(char str[], char c) {
  11.     int i = 0;
  12.     while (str[i] != 0) {
  13.         if (str[i] == c)
  14.             return true;
  15.         i++;
  16.     }//end of while
  17.     return false;
  18. }//end of contains
  19.  
  20. int main() {
  21.     srand(time(0));
  22.     char words[10][11] = { "ABC","BAD","CAT","DAD","FREE","GREAT","HAPPY","ISLAND","JACK" };
  23.     int pickedWord = rand() % 10;
  24.     cout << words[pickedWord] << endl;
  25.     char usedLetters[26] = {};
  26.     char c;
  27.     int attemps = 0;
  28.     while (attemps < 10) {
  29.         if (attemps == 5) {
  30.             int i = 0;
  31.             int missingLetters = 0;
  32.             int userMissed = 0;
  33.             while (words[pickedWord][i]) {
  34.                 char letterInWord = words[pickedWord][i];
  35.                 if (usedLetters[letterInWord - 'A'] != 1)
  36.                     userMissed++;
  37.                 i++;
  38.             }//end of while
  39.             cout << "There are " << userMissed << " letters left in the word.\n";
  40.         }//end of if
  41.         cout << "Enter a letter:";
  42.         cin >> c;
  43.         c = toUpper(c);
  44.         if (usedLetters[c - 'A'] == 1) {
  45.             cout << "You've already tried letter " << c << endl;
  46.             continue;
  47.         }//end of if
  48.         else
  49.             usedLetters[c - 'A'] = 1;
  50.         if (contains(words[pickedWord], c)) {
  51.             cout << "Letter " << c << " is correct!\n";
  52.             bool win = true;
  53.             int i = 0;
  54.             while (words[pickedWord][i]) {
  55.                 char letterInWord = words[pickedWord][i];
  56.                 if (usedLetters[letterInWord - 'A'] != 1) {
  57.                     win = false;
  58.                     break;
  59.                 }//end of if
  60.                 i++;
  61.             }//end of while
  62.             if (win) {
  63.                 cout << "You've won with only " << attemps << " wrong answer! The word was " << words[pickedWord] << endl;
  64.                 return 0;
  65.             }//end of if
  66.         }//end of if
  67.         else {
  68.             attemps++;
  69.             cout << "Incorrect! You have " << 10 - attemps << " tries left!\n";
  70.         }//end of else
  71.     }//end of while
  72.     cout << "You lose! The word was " << words[pickedWord] << "!\n";
  73.     return 0;
  74. }//end of main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement