Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Fallout hacking game
- //https://www.reddit.com/r/dailyprogrammer/comments/3qjnil/20151028_challenge_238_intermediate_fallout/
- //Programmed by Bruno Petrus 1/1/2016
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- #include <ctime>
- #include <cstdlib>
- //Number of words
- const int NUMBER_OF_WORDS = 10;
- //TRIES
- const int NUMBER_OF_TRIES = 4;
- //Load words
- std::vector<std::string> loadWords(std::string path, int length);
- //Compare words with input
- int compareInputWithWords(std::string correctWord, std::string inputString);
- //Pseudo-random number generator
- std::vector<int> generateNumbers(int max, int size);
- int main(int argc, char* argv[]) {
- //Feed the random number generator
- srand(static_cast<unsigned int>(time(nullptr)));
- //Game loop
- bool quit = false;
- while (!quit) {
- //Ask for difficulty
- std::cout << "Difficulty (1,2,3,4,5) ? ";
- int difficulty = 0;
- std::cin >> difficulty;
- while (std::cin.fail() || difficulty > 5 || difficulty < 1) {
- std::cin.clear();
- std::cin.ignore();
- std::cout << "Difficulty (1,2,3,4,5) ? ";
- std::cin >> difficulty;
- }
- //Decide the length
- int length = 0;
- switch (difficulty)
- {
- case 1:
- length = 5;
- break;
- case 2:
- length = 7;
- break;
- case 3:
- length = 9;
- break;
- case 4:
- length = 12;
- break;
- case 5:
- length = 14;
- break;
- }
- //Load words
- std::vector<std::string> listWithWords = loadWords("enable1.txt", length);
- //Choose one
- std::string correctWord = listWithWords[rand() % (listWithWords.size() - 1)];
- //Print the options
- for (unsigned int i = 0; i < listWithWords.size(); i++)
- std::cout << listWithWords[i] << std::endl;
- //Tries user has
- int remainingTries = NUMBER_OF_TRIES;
- while (remainingTries > 0) {
- std::cout << "Guess (" << remainingTries << " left) ? ";
- std::string input;
- std::cin >> input;
- //Convert to lower case
- for (int i = 0; i < input.size(); i++) {
- input[i] = tolower(input[i]);
- }
- int matches = compareInputWithWords(correctWord, input);
- std::cout << matches << "/" << length << " correct\n";
- //User wins
- if (matches == length) {
- std::cout << "You won!\n";
- break;
- }
- remainingTries--;
- }
- //Print correct word
- std::cout << "Correct word: " << correctWord << std::endl;
- //Restart ?
- std::cout << "\n\nRestart [Y/N]? ";
- char temp;
- std::cin >> temp;
- if (temp == 'Y' || temp == 'y')
- quit = false;
- else
- quit = true;
- }
- return 0;
- }
- //Load words
- std::vector<std::string> loadWords(std::string path, int length) {
- //Final vector filled with words
- std::vector<std::string> finalList;
- //Open file
- std::ifstream file(path);
- if (!file.fail()) {
- //List with every word
- std::vector<std::string> list;
- std::string line = "";
- while (std::getline(file, line)) {
- if (line.size() == length) //If the word is exactly the same size as length
- list.push_back(line);
- line = "";
- }
- //Generate random indices
- std::vector<int> indices = generateNumbers(list.size() - 1, NUMBER_OF_WORDS);
- for (unsigned int i = 0; i < indices.size(); i++)
- finalList.push_back(list[indices[i]]);
- }
- else {
- std::cout << "Can't open file: " << path;
- }
- return finalList;
- }
- //Compare words with input
- int compareInputWithWords(std::string correctWord, std::string inputString) {
- //How many letters matched
- int matches = 0;
- //Loop
- for (int i = 0; i < correctWord.size(); i++) {
- if (correctWord[i] == inputString[i])
- matches++;
- }
- return matches;
- }
- bool checkInList(std::vector<int> list, int number) {
- if (list.size() == 0) {
- return false;
- }
- for (unsigned int i = 0; i < list.size(); i++) {
- if (list[i] == number)
- return true;
- }
- return false;
- }
- //Pseudo-random number generator
- //NOTE! Including max
- std::vector<int> generateNumbers(int max, int size) {
- //Numbers
- std::vector<int> listOfNumbers;
- for (int x = 0; x < size; x++) { //Generate numbers
- int n = 0;
- do {
- n = rand() % (max + 1); //Including max
- } while (checkInList(listOfNumbers, n));
- listOfNumbers.push_back(n);
- }
- return listOfNumbers;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement