Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #define N_KEYS 12
  6. #define KEY_MAX_WIDTH 20
  7. #define FAILS_ALLOWED 7
  8.  
  9.  
  10. char possiblekeys [N_KEYS][KEY_MAX_WIDTH] = {
  11.     "mushroom", "pineapple", "neighborhood", "citizen",
  12.     "programming", "darkness", "fundamental", "encyclopedia",
  13.     "businessman", "restaurant", "observatory", "civilization"
  14. };
  15.  
  16.  
  17. char key [KEY_MAX_WIDTH];
  18.  
  19.  
  20. char outstring [KEY_MAX_WIDTH];
  21.  
  22. int CheckLetter (char letter);
  23.  
  24. main ()
  25. {
  26.   using namespace std;
  27.   char input;
  28.   int valid;
  29.   int fails = FAILS_ALLOWED;
  30.   unsigned int discovered = 0;
  31.   unsigned int n;
  32.  
  33.  
  34.   srand ( time (NULL) );            
  35.   int value = rand()%N_KEYS;        
  36.   strcpy (key,possiblekeys[value]);
  37.  
  38.  
  39.   for (n=0; n<strlen(key); n++) outstring[n]='-';
  40.   outstring[n]='\0';
  41.  
  42.   do {
  43.    
  44.     cout << "\nDiscover the secret key: " << outstring << "\n";
  45.     cout << "Enter a letter (You may fail " << fails << " times): ";
  46.     cin >> input; cin.ignore (100,'\n');
  47.  
  48.    
  49.     valid = CheckLetter (input);
  50.  
  51.    
  52.     if (valid!=0) discovered+=valid;
  53.     else fails--;
  54.  
  55.   } while (discovered < strlen(key) && fails>0);
  56.  
  57.  
  58.  
  59.   if (discovered == strlen(key)) cout << "CORRECT! ";
  60.  
  61.   cout<< "Key was '" << key <<"'.\n";
  62.   return 0;
  63. }
  64.  
  65.  
  66. int CheckLetter (char letter)
  67. {
  68.     unsigned int n;
  69.     int found=0;
  70.     for (n=0; n<strlen(key); n++)
  71.         if (key[n]==letter && outstring[n]=='-')
  72.         {
  73.             found++;
  74.             outstring[n]=key[n];
  75.         }
  76.     return found;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement