-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman.cpp
155 lines (136 loc) · 5.3 KB
/
hangman.cpp
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
#include <iostream>
#include <cstdlib>
using namespace std;
const int max_bad_guesses = 7;
const string WORD_LIST[] = {"angle", "ant", "apple", "arch", "arm", "army",
"baby", "bag", "ball", "band", "basin", "basket", "bath", "bed", "bee", "bell", "berry",
"bird", "blade", "board", "boat", "bone", "book", "boot", "bottle", "box", "boy",
"brain", "brake", "branch", "brick", "bridge", "brush", "bucket", "bulb", "button",
"cake", "camera", "card", "cart", "carriage", "cat", "chain", "cheese", "chest",
"chin", "church", "circle", "clock", "cloud", "coat", "collar", "comb", "cord",
"cow", "cup", "curtain", "cushion",
"dog", "door", "drain", "drawer", "dress", "drop", "ear", "egg", "engine", "eye",
"face", "farm", "feather", "finger", "fish", "flag", "floor", "fly",
"foot", "fork", "fowl", "frame", "garden", "girl", "glove", "goat", "gun",
"hair", "hammer", "hand", "hat", "head", "heart", "hook", "horn", "horse",
"hospital", "house", "island", "jewel", "kettle", "key", "knee", "knife", "knot",
"leaf", "leg", "library", "line", "lip", "lock",
"map", "match", "monkey", "moon", "mouth", "muscle",
"nail", "neck", "needle", "nerve", "net", "nose", "nut",
"office", "orange", "oven", "parcel", "pen", "pencil", "picture", "pig", "pin",
"pipe", "plane", "plate", "plow", "pocket", "pot", "potato", "prison", "pump",
"rail", "rat", "receipt", "ring", "rod", "roof", "root",
"sail", "school", "scissors", "screw", "seed", "sheep", "shelf", "ship", "shirt",
"shoe", "skin", "skirt", "snake", "sock", "spade", "sponge", "spoon", "spring",
"square", "stamp", "star", "station", "stem", "stick", "stocking", "stomach",
"store", "street", "sun", "table", "tail", "thread", "throat", "thumb", "ticket",
"toe", "tongue", "tooth", "town", "train", "tray", "tree", "trousers", "umbrella",
"wall", "watch", "wheel", "whip", "whistle", "window", "wire", "wing", "worm"};
const int WORD_COUNT = sizeof(WORD_LIST) / sizeof(string);
const string FIGURE[] = {
" ------------- \n"
" | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | \n"
" | \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | | \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | /| \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | /|\\ \n"
" | \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | /|\\ \n"
" | / \n"
" | \n"
" ----- \n",
" ------------- \n"
" | | \n"
" | O \n"
" | /|\\ \n"
" | / \\ \n"
" | \n"
" ----- \n",
};
string chooseWord()
{
int randomIndex = rand() % WORD_COUNT;
return WORD_LIST[randomIndex];
}
string update(string guessedWord, string secretWord, char guess)
{
for(int i = secretWord.length()-1; i>=0; i--)
{
if(secretWord[i] == guess) guessedWord[i] = guess;
}
return guessedWord;
}
bool contains(string secretWord, char c)
{
for(int i=secretWord.length()-1; i>=0; i--)
{
if(secretWord[i] == c) return true;
}
return false;
}
void renderGame(string guessedWord, int badGuessCount)
{
cout << FIGURE[badGuessCount] << endl;
cout << guessedWord << endl;
cout << "Number of wrong guesses: " << badGuessCount << endl;
}
char readAGuess()
{
char c;
cin >> c;
return c;
}
int main()
{
string secretWord = chooseWord();
string guessedWord = string(secretWord.length(), '-');
int badGuessCount = 0;
do {
renderGame(guessedWord, badGuessCount);
char guess = readAGuess();
if (contains(secretWord, guess))
guessedWord = update(guessedWord, secretWord, guess);
else badGuessCount++;
} while (badGuessCount < max_bad_guesses && secretWord != guessedWord);
renderGame(guessedWord, badGuessCount);
if (badGuessCount < max_bad_guesses) cout << "Congratulations! You win!";
else cout << "You lost. The correct word is " << secretWord;
}