Advertisement
Guest User

Challenge #238 [Intermediate] Fallout Hacking Game

a guest
Jan 1st, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.18 KB | None | 0 0
  1. //Fallout hacking game
  2. //https://www.reddit.com/r/dailyprogrammer/comments/3qjnil/20151028_challenge_238_intermediate_fallout/
  3. //Programmed by Bruno Petrus 1/1/2016
  4.  
  5. #include <iostream>
  6. #include <fstream>
  7. #include <vector>
  8. #include <string>
  9. #include <ctime>
  10. #include <cstdlib>
  11.  
  12. //Number of words
  13. const int NUMBER_OF_WORDS = 10;
  14.  
  15. //TRIES
  16. const int NUMBER_OF_TRIES = 4;
  17.  
  18. //Load words
  19. std::vector<std::string> loadWords(std::string path, int length);
  20.  
  21. //Compare words with input
  22. int compareInputWithWords(std::string correctWord, std::string inputString);
  23.  
  24. //Pseudo-random number generator
  25. std::vector<int> generateNumbers(int max, int size);
  26.  
  27. int main(int argc, char* argv[]) {
  28.     //Feed the random number generator
  29.     srand(static_cast<unsigned int>(time(nullptr)));
  30.  
  31.     //Game loop
  32.     bool quit = false;
  33.     while (!quit) {
  34.  
  35.         //Ask for difficulty
  36.         std::cout << "Difficulty (1,2,3,4,5) ? ";
  37.         int difficulty = 0;
  38.         std::cin >> difficulty;
  39.         while (std::cin.fail() || difficulty > 5 || difficulty < 1) {
  40.             std::cin.clear();
  41.             std::cin.ignore();
  42.             std::cout << "Difficulty (1,2,3,4,5) ? ";
  43.             std::cin >> difficulty;
  44.         }
  45.  
  46.         //Decide the length
  47.         int length = 0;
  48.         switch (difficulty)
  49.         {
  50.         case 1:
  51.             length = 5;
  52.             break;
  53.         case 2:
  54.             length = 7;
  55.             break;
  56.         case 3:
  57.             length = 9;
  58.             break;
  59.         case 4:
  60.             length = 12;
  61.             break;
  62.         case 5:
  63.             length = 14;
  64.             break;
  65.         }
  66.  
  67.         //Load words
  68.         std::vector<std::string> listWithWords = loadWords("enable1.txt", length);
  69.  
  70.         //Choose one
  71.         std::string correctWord = listWithWords[rand() % (listWithWords.size() - 1)];
  72.  
  73.         //Print the options
  74.         for (unsigned int i = 0; i < listWithWords.size(); i++)
  75.             std::cout << listWithWords[i] << std::endl;
  76.  
  77.         //Tries user has
  78.         int remainingTries = NUMBER_OF_TRIES;
  79.         while (remainingTries > 0) {
  80.             std::cout << "Guess (" << remainingTries << " left) ? ";
  81.             std::string input;
  82.             std::cin >> input;
  83.  
  84.             //Convert to lower case
  85.             for (int i = 0; i < input.size(); i++) {
  86.                 input[i] = tolower(input[i]);
  87.             }
  88.  
  89.             int matches = compareInputWithWords(correctWord, input);
  90.             std::cout << matches << "/" << length << " correct\n";
  91.            
  92.             //User wins
  93.             if (matches == length) {
  94.                 std::cout << "You won!\n";
  95.                 break;
  96.             }
  97.  
  98.             remainingTries--;
  99.         }
  100.  
  101.         //Print correct word
  102.         std::cout << "Correct word: " << correctWord << std::endl;
  103.  
  104.         //Restart ?
  105.         std::cout << "\n\nRestart [Y/N]? ";
  106.         char temp;
  107.         std::cin >> temp;
  108.         if (temp == 'Y' || temp == 'y')
  109.             quit = false;
  110.         else
  111.             quit = true;
  112.     }
  113.  
  114.     return 0;
  115. }
  116.  
  117. //Load words
  118. std::vector<std::string> loadWords(std::string path, int length) {
  119.     //Final vector filled with words
  120.     std::vector<std::string> finalList;
  121.  
  122.     //Open file
  123.     std::ifstream file(path);
  124.     if (!file.fail()) {
  125.         //List with every word
  126.         std::vector<std::string> list;
  127.  
  128.         std::string line = "";
  129.         while (std::getline(file, line)) {
  130.             if (line.size() == length) //If the word is exactly the same size as length
  131.                 list.push_back(line);
  132.             line = "";
  133.         }
  134.  
  135.         //Generate random indices
  136.         std::vector<int> indices = generateNumbers(list.size() - 1, NUMBER_OF_WORDS);
  137.  
  138.         for (unsigned int i = 0; i < indices.size(); i++)
  139.             finalList.push_back(list[indices[i]]);
  140.     }
  141.     else {
  142.         std::cout << "Can't open file: " << path;
  143.     }
  144.  
  145.     return finalList;
  146. }
  147.  
  148. //Compare words with input
  149. int compareInputWithWords(std::string correctWord, std::string inputString) {
  150.     //How many letters matched
  151.     int matches = 0;
  152.  
  153.     //Loop
  154.     for (int i = 0; i < correctWord.size(); i++) {
  155.         if (correctWord[i] == inputString[i])
  156.             matches++;
  157.     }
  158.  
  159.     return matches;
  160. }
  161.  
  162. bool checkInList(std::vector<int> list, int number) {
  163.     if (list.size() == 0) {
  164.         return false;
  165.     }
  166.     for (unsigned int i = 0; i < list.size(); i++) {
  167.         if (list[i] == number)
  168.             return true;
  169.     }
  170.     return false;
  171. }
  172.  
  173. //Pseudo-random number generator
  174. //NOTE! Including max
  175. std::vector<int> generateNumbers(int max, int size) {
  176.     //Numbers
  177.     std::vector<int> listOfNumbers;
  178.  
  179.     for (int x = 0; x < size; x++) { //Generate numbers
  180.         int n = 0;
  181.         do {
  182.             n = rand() % (max + 1); //Including max
  183.         } while (checkInList(listOfNumbers, n));
  184.         listOfNumbers.push_back(n);
  185.     }
  186.     return listOfNumbers;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement