Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <time.h>
- #include <conio.h>
- using namespace std;
- string randomWord();
- int main()
- {
- int guess;
- int maxGuess = 10;
- char letter;
- int i;
- int found_letters[6];
- bool letter_match;
- for(;;)
- {
- guess = 0;
- string word = randomWord();
- for(i=0; i<(sizeof(found_letters)/sizeof(int)); i++)
- found_letters[i] = -1;
- //replace characters in string with '*'s
- string hiddenword = word;
- for (int i = 0; i < word.length(); i++)
- {
- hiddenword[i] = '*';
- }
- while (guess < maxGuess)
- {
- letter_match = false;
- cout << "Guess a letter: ";
- cin >> letter;
- /*
- for(i=0,int j=0; i<word.size(); i++)
- if( word[i] == letter )
- {
- letter_match = true;
- found_letters[j++] = i;
- }
- if(letter_match)
- {
- cout << "You got a match\n";
- for(i=0; i<(sizeof(found_letters)/sizeof(int)); i++)
- {
- if(found_letters[i] != -1)
- hiddenword[found_letters[i]] = word[found_letters[i]];
- }
- }
- else
- {
- cout << "sorry, no match\n";
- guess++;
- }
- cout << hiddenword << endl;
- */
- int found = word.find(letter);
- if (found != string::npos)
- {
- hiddenword[found] = word[found];
- cout << "Congratz, you got a letter!" << endl;
- cout << hiddenword << endl;
- }
- else
- {
- cout << "no letter for you.." << endl;
- cout << hiddenword << endl;
- guess++;// += 1;
- }*
- }
- cout << "The game is over!" << endl;
- cout << "The word was: " << word << endl;
- cin.get();
- }
- return 0;
- }
- int linesInFile()
- {
- ifstream myfile ("words.txt");
- string line;
- vector <string> lines;
- while(getline (myfile, line) ) lines.push_back(line);
- return lines.size();
- }
- string randomWord()
- {
- string* words;
- words = new string[linesInFile()];
- //string words[128];
- ifstream myfile ("words.txt");
- while (myfile.good() )
- {
- for(int i = 0; i < 128; i++)
- {
- getline (myfile, words[i]);
- }
- }
- srand(unsigned(time(NULL)));
- return words[(rand() % linesInFile())];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement