summaryrefslogtreecommitdiffstats
path: root/labb2/evilhangman/src/evilhangman.cpp
blob: c57995bf1551347243f8c39a3dd771ccacd9497b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <string>
#include <fstream>
#include <list>
#include <map>
#include <set>

using namespace std;

static const string ALPHABET = "abcdefghijklmnopqrstuvwxyz";

/*
 * Populate `wordsByLength` with lists of strings from file `path`.
 *
 * `wordsByLength` is "indexed" by the length of the words in the list.
 */
void openDictionary(map<int, list<string>> &wordsByLength, const string &path) {
    ifstream input;
    input.open(path);
    string word;
    while (input >> word) {
        int length = word.length();
        if (wordsByLength.count(length) == 0) {
            wordsByLength[length] = list<string>();
        }
        wordsByLength[length].push_front(word);
    }
    input.close();
}

/*
 * Update the prototype with the char `guess` to match `word`.
 */
void getWordPrototype(string &wordPrototype, const string &word, const char guess) {
    for (int i = 0; i < word.size(); i++) {
        if (word[i] == guess) {
            wordPrototype[i] = guess;
        }
    }
}

/*
 * Update `wordGroup` with the largest word group after `guess` was made,
 * given a previous word group and prototype. Returns whether the guess
 * was correct or not and updates the prototype.
 */
bool getWordGroup(list<string> &wordGroup, string &wordPrototype, const char guess) {
    map<string, list<string>> wordGroups; // { prototype: [ match1, match2, .. ], ..  }
    for (string &word : wordGroup) {
        if (word.find(guess) == string::npos) { // word doesn't contain guess
            // initial empty list
            if (wordGroups.count(wordPrototype) == 0) {
                wordGroups[wordPrototype] = list<string>();
            }
            wordGroups[wordPrototype].push_back(word);
        } else { // word contains guess at least once
            string newWordPrototype(wordPrototype);
            getWordPrototype(newWordPrototype, word, guess);
            // initial empty list
            if (wordGroups.count(newWordPrototype) == 0) {
                wordGroups[newWordPrototype] = list<string>();
            }
            wordGroups[newWordPrototype].push_back(word);
        }
    }

    // find largest word group
    int maxSize = 0;
    string maxPrototype;
    for (auto &n : wordGroups) {
        int size = n.second.size();
        if (size > maxSize) {
            maxSize = size;
            maxPrototype = n.first;
        }
    }

    // set wordgroup and prototype
    wordGroup = wordGroups[maxPrototype];
    if (wordPrototype == maxPrototype) {
        return false;
    } else {
        wordPrototype = maxPrototype;
        return true;
    }
}

/*
 * Play (evil) hangman with the given parameters.
 *
 * `wordLength` is the length of the word to guess.
 * `guesses` is the total amount of guesses.
 * `showWordsLeft` is whether to print how many valid words are left in each stage.
 * `wordGroup` is the initial group of valid words.
 */
bool hangman(const int wordLength, const int guesses, const bool showWordsLeft, list<string> &wordGroup) {
    string wordPrototype;
    for (int i = 0; i < wordLength; i++) {
        wordPrototype.push_back('-');
    }

    set<char> guessedChars;
    int guesses_left = guesses;
    string line;

    bool won = false;
    while (guesses_left > 0 && !won) {
        // print status
        if (showWordsLeft) {
            cout << "(" << wordGroup.size() << ") ";
        }
        cout << wordPrototype << " with " << guesses_left << " guesses left. Guessed characters: ";
        for (char c : guessedChars) {
            cout << c;
        }
        cout << endl;

        // ask for guess
        char guess;
        bool done = false;
        while (!done) {
            cout << "Guess a letter: ";
            getline(cin, line);
            if (line.size() != 1) {
                cout << "Please input one character only." << endl;
                continue;
            }
            guess = line[0];
            if (ALPHABET.find(guess) == string::npos) {
                cout << "Please input a valid (lower-case) character." << endl;
                continue;
            }
            if (guessedChars.count(guess)) {
                cout << "You have already guessed this character." << endl;
                continue;
            }
            done = true;
        }
        guessedChars.insert(guess);

        // check guess
        if (getWordGroup(wordGroup, wordPrototype, guess)) {
            // correct guess, new prototype
            won = (wordPrototype.find('-') == string::npos);
        } else {
            // wrong guess
            guesses_left--;
        }
    }
    return won;
}

int main() {
    cout << "Welcome to Hangman." << endl;

    cout << "Reading dictionary... " << endl;
    map<int, list<string>> wordsByLength;
    openDictionary(wordsByLength, "res/dictionary.txt");
    cout << "done" << endl;

    bool play = true;
    while (play) {
        string line;
        int wordLength;
        bool done = false;
        while (!done) {
            cout << "Choose a word length to guess: ";
            getline(cin, line);
            try {
                wordLength = stoi(line);
            } catch (const invalid_argument &) {
                cout << "Not a number." << endl;
                continue;
            }
            if (wordsByLength.count(wordLength) == 0) {
                cout << "No words of that length found." << endl;
            } else {
                done = true;
            }
        }
        int guesses;
        done = false;
        while (!done) {
            cout << "Choose number of guesses: ";
            getline(cin, line);
            try {
                guesses = stoi(line);
            } catch (const invalid_argument &) {
                cout << "Not a number." << endl;
                continue;
            }
            if (guesses <= 0) {
                cout << "Non-positive number of guesses doesn't make sense." << endl;
                continue;
            }
            done = true;
        }

        cout << "Would you like to see the number of words left? [Y/n]: ";
        getline(cin, line);
        bool showWordsLeft = (line != "n");

        list<string> wordGroup = wordsByLength[wordLength];

        if (hangman(wordLength, guesses, showWordsLeft, wordGroup)) {
            cout << "Well done!" << endl;
        } else {
            cout << "The word was '" << wordGroup.front() << "'. Better luck next time." << endl;
        }
        cout << "Play again? [Y/n]: ";
        getline(cin, line);
        play = (line != "n");
    }
    cout << "Goodbye" << endl;
}