-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
169 lines (133 loc) · 3.31 KB
/
Copy pathhangman.cpp
File metadata and controls
169 lines (133 loc) · 3.31 KB
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
#include "games.h"
#include "info.h"
#include "player.h"
#include "misc.h"
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <set>
constexpr char Unknown{ '-' };
constexpr int MaxWords = 30;
constexpr char words[][MaxWords]{
"measurement",
"recommendation",
"employment",
"childhood",
"signature",
"society",
"perspective",
"village",
"imagination",
"communication",
"complaint",
"possibility",
"independence",
"administration",
"significance",
"appearance",
"economics",
"hangman",
"application",
"maintenance",
"consequence",
"appointment",
"distributable",
"unconstitutionally",
"inspector",
"imagination",
"description",
"birthday",
"environment",
"temperature"
};
static bool isValid(char letter) {
return (letter >= 'A' && letter <= 'Z');
}
static void drawWord(const std::string& known) {
for (int i = 0; i < known.size(); ++i) {
std::cout << ' ' << known.at(i) << ' ';
}
std::cout << "\n\n";
}
static char getInput(const Player& player) {
char letter{};
std::cout << player.name << ", pick a letter: ";
if (isComputer(player)) {
do {
letter = (char) nextInteger('A', 'Z' + 1);
} while (!isValid(letter));
std::cout << letter << "\n";
}
else {
do {
letter = toupper(inputChar());
if (!isValid(letter)) {
std::cout << "Enter a valid letter which wasn't picked yet!\n";
}
} while (!isValid(letter));
}
return letter;
}
bool processLetter(std::string& known, const char* selectedWord, char letter) {
bool atLeastOne{ false };
for (int i = 0; i < known.size(); ++i) {
if ((known[i] == Unknown) && (letter == toupper(selectedWord[i]))) {
known[i] = letter;
atLeastOne = true;
}
}
return atLeastOne;
}
bool complete(const std::string& word) {
for (auto c : word) {
if (c == Unknown)
return false;
}
return true;
}
Info runHangman(const Player& firstPlayer, const Player& secondPlayer) {
Info info{};
const char* selectedWord = words[nextInteger(MaxWords)];
std::string knownLetters{};
for (int i = 0; i < std::strlen(selectedWord); ++i) {
knownLetters.append(std::string{Unknown});
}
const Player* turn{ &firstPlayer };
do {
header("Hangman");
drawWord(knownLetters);
char input{ getInput(*turn) };
if (processLetter(knownLetters, selectedWord, input)) {
std::cout << "'" << input << "' is in this word.\n";
}
else {
std::cout << "'" << input << "' is NOT in this word.\n";
}
drawWord(knownLetters);
char yn{ (char) toupper(inputChar("Do you know the word? [Y/N] ")) };
if (yn == 'Y') {
auto word{ inputStr("Enter it: ")};
if (word.compare(selectedWord) == 0) {
std::cout << "Correct, the word was " << selectedWord << "! " << turn->name << " wins!\n";
info.winner = turn;
info.loser = (turn == &firstPlayer ? &secondPlayer : &firstPlayer);
break;
} else {
turn = (turn == &firstPlayer ? &secondPlayer : &firstPlayer);
std::cout << "Incorrect, the word was " << selectedWord << "! " << turn->name << " wins!\n";
info.loser = turn;
info.winner = (turn == &firstPlayer ? &secondPlayer : &firstPlayer);
break;
}
}
if (complete(knownLetters)) {
std::cout << "It's a Draw\n";
info.winner = nullptr;
info.loser = nullptr;
break;
}
turn = (turn == &firstPlayer ? &secondPlayer : &firstPlayer);
} while (true);
return info;
}