Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using namespace std;
- #include <iostream>
- #include <vector>
- #include <fstream>
- #include <string>
- #include <ctime>
- const string data = "Data.txt";
- void ReadData(vector<string> & BigList);
- void PrintVector(const vector<string> & V);
- void GenerateChallenge(vector<string> & SmallList, const vector<string> & BigList, int wordLn);
- int CheckAnswer(const string & word, const string & guess);
- void StartGame(const string & word);
- int main()
- {
- srand(time(NULL));
- vector<string> BigList, SmallList;
- string word;
- int diff;
- ReadData(BigList);
- cout << "Difficulty (1-5)? ";
- cin >> diff;
- GenerateChallenge(SmallList, BigList, diff*3);
- PrintVector(SmallList);
- word = SmallList[rand() % SmallList.size()];
- StartGame(word);
- return 0;
- }
- void StartGame(const string & word) {
- int attempts = 4;
- string guess;
- while (attempts != 0) {
- cout << "Guess (" << attempts << " left) ? ";
- cin >> guess;
- cout << CheckAnswer(word, guess) << "/" << word.length() << " correct" << endl;
- attempts--;
- if (CheckAnswer(word, guess) == word.length()) {
- cout << "You win!" << endl;
- break;
- }
- }
- }
- void ReadData(vector<string> & BigList) {
- ifstream fd(data);
- string word;
- while (!fd.eof()) {
- fd >> word;
- BigList.push_back(word);
- }
- fd.close();
- }
- bool Exists(const vector<string> & SmallList, const string & word) {
- for (size_t i = 0; i < SmallList.size(); i++)
- if (SmallList[i] == word)
- return true;
- return false;
- }
- void GenerateChallenge(vector<string> & SmallList, const vector<string> & BigList, int wordLn) {
- unsigned long rndNumber;
- while (SmallList.size() != 7) {
- int divideRnd = rand() % (BigList.size() / RAND_MAX + 1);
- if (divideRnd == BigList.size() / RAND_MAX)
- rndNumber = (rand() % (BigList.size() - divideRnd * RAND_MAX)) + (divideRnd * RAND_MAX);
- else
- rndNumber = rand() + divideRnd * RAND_MAX;
- for (unsigned long i = rndNumber; i < BigList.size(); i++) {
- if (BigList[i].length() == wordLn && !Exists(SmallList, BigList[i])) {
- SmallList.push_back(BigList[i]);
- break;
- }
- }
- }
- }
- int CheckAnswer(const string & word, const string & guess) {
- int count = 0;
- for (size_t i = 0; i < word.length(); i++)
- if (toupper(word[i]) == toupper(guess[i]))
- count++;
- return count;
- }
- void PrintVector(const vector<string> & V) {
- for (size_t i = 0; i < V.size(); i++) {
- cout << V[i] << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement