Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <ctime>
- #include <string>
- using namespace std;
- string alphabet = "a b c d e f g h i j k l m n o p q r s t u v w x y z";
- string line, guessed, word, display;
- int numlines = 0, inttries = 0;
- ifstream myfile("words.txt");
- void makeDisplay() {
- display = "";
- for (size_t k = 0;k<string(word).size();k++)
- display = display + "_";
- guessed = "_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _";
- }
- string makeMan() {
- switch (inttries) {
- case 10:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n ||\n || || \n || /||\\\n || //||\\\\\n || // || \\\\\n || * || *\n || //\\\\\n || // \\\\\n /\\ // \\\\\n//\\\\ *** ***\n/||\\\\\n_||_\\\\\n";
- case 9:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n || ||\n || /||\\\n || //||\\\\\n || // || \\\\\n || * || *\n || //\n || //\n || //\n /\\ ***\n//\\\\ \n/||\\\\ \n_||_\\\\\n";
- case 8:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n || ||\n || /||\\\n || //||\\\\\n || // || \\\\\n || * || *\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 7:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n || ||\n || /||\n || //||\n || // ||\n || * ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 6:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n || ||\n || ||\n || ||\n || ||\n || ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 5:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n || ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 4:
- return "\n ================|\n // |\n || |\n || |\n || _^_\n || / ..\\\n || [ _ ]\n || \\___/\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 3:
- return "\n ================|\n // |\n || |\n || |\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 2:
- return "\n ================\n //\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 1:
- return "\n\n\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n ||\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- case 0:
- return "\n\n\nPress * anytime to quit!\n\n\n\n\n\n\n\n\n\n\n\n /\\\n//\\\\\n/||\\\\\n_||_\\\\\n";
- }
- return " ";
- }
- void guesslet(char a) {
- int correct = 0, tried = 0, validlet = 0;
- size_t k = 0;
- for (k = 0; k < 51; k += 2)
- if (alphabet[k] == a) {
- validlet = 1;
- if (guessed[k] != a)
- guessed[k] = a;
- else
- tried = 1;
- }
- if ((tried != 1) && (validlet == 1)) {
- for (k = 0;k<string(word).size();k++)
- if (word[k] == a)
- {
- display[k] = a;
- correct = 1;
- }
- if (correct != 1)
- inttries++;
- }
- }
- void selectRandLine() {
- int randline = 0, wordlength = 0, k = 0;
- string newword;
- randline = (rand() % numlines) + 1;
- myfile.clear();
- myfile.seekg(0, ios::beg);
- for (int lineno = 0; getline(myfile, newword) && lineno < randline; lineno++);
- wordlength = string(newword).size();
- word = "";
- for (int i = 0; wordlength>i; i++) {
- for (k = 0; k < 51; k += 2)
- if (alphabet[k] == newword[i])
- word = word + newword[i];
- }
- }
- int main() {
- char a = '.';
- srand((unsigned)time(0));
- while (getline(myfile, line)) ++numlines;
- while (a != '*') {
- selectRandLine();
- makeDisplay();
- inttries = 0;
- while (display != word) {
- system("cls");
- cout << makeMan();
- cout << "Please guess a letter!\nWord: " << display << "\nGuessed: " << guessed << "\nTries left:" << 10 - inttries << "\n";
- cin >> a;
- guesslet(a);
- if (a == '*') inttries = 10;
- if (inttries >= 10) display = word;
- }
- cout << makeMan();
- if (inttries >= 10)
- cout << "\nYou lost! The word was \"" << word << "\".\nPress any key to play again, or * to quit.\n\n";
- if (inttries < 10)
- cout << "\nYou won! The word is \"" << word << "\"!\nPress any key to play again, or * to quit.\n\n";
- cin >> a;
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment