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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
void PlayGame();
void Rules();
std::string CreateHiddenWord(const std::string &SelectedTopic);
void PrintMan();
void PrintTopic(const std::string &SelectedTopic);
void PrintUnderscore(const std::string &HiddenWord, std::vector <char> &ReplaceUnderscore);
void CheckGameStatus(const std::vector <char> &ReplaceUnderscore, const std::string &HiddenWord);
std::string InputGuess();
std::vector <char> TestGuess(const std::string &Guess, const std::string &HiddenWord, std::vector <char> &ReplaceUnderscore);
void GameWon(const std::string &HiddenWord);
void GameLost(const std::string &HiddenWord);
void AskToPlayAgain();
void Reset();
int Errors = 0;
int main() {
srand((unsigned int(time(NULL))));
PlayGame();
return 0;
}
void PlayGame() {
std::vector <char> ReplaceUnderscore;
const std::string Topic[] = { "Animals", "Football", "C++" };
const std::string SelectedTopic = Topic[rand() % 3];
std::string HiddenWord = CreateHiddenWord(SelectedTopic);
Rules();
while (true) {
PrintMan();
PrintTopic(SelectedTopic);
PrintUnderscore(HiddenWord, ReplaceUnderscore);
CheckGameStatus(ReplaceUnderscore, HiddenWord);
std::string Guess = InputGuess();
ReplaceUnderscore = TestGuess(Guess, HiddenWord, ReplaceUnderscore);
std::cout << std::flush;
system("CLS");
}
}
void Rules() {
std::cout << "Basic Rules: 1. 7 Chances For Errors. 2. Only use lowercase. 3. Use only 1 letter.\n\n";
}
std::string CreateHiddenWord(const std::string &SelectedTopic) {
std::string HiddenWord;
const std::string RandomAnimal[] = { "dog", "cat", "elephant", "monkey", "bird", "panda", "snake", "donkey" };
const std::string RandomFootball[] = { "gators", "bulldogs", "patriots", "football", "kickoff", "quarter" };
const std::string RandomCPP[] = { "switch", "for", "while", "vector", "class", "pointer", "array", "header" };
switch (SelectedTopic[0]) {
case 'A':
HiddenWord = RandomAnimal[rand() % 8];
break;
case 'F':
HiddenWord = RandomFootball[rand() % 6];
break;
case 'C':
HiddenWord = RandomCPP[rand() % 8];
break;
}
return HiddenWord;
}
void PrintMan() {
switch (Errors) {
case 0:
std::cout << " #######\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 1:
std::cout << " #######\n";
std::cout << " ## |\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 2:
std::cout << " #######\n";
std::cout << " ## |\n";
std::cout << " ## @\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 3:
std::cout << " #######\n";
std::cout << " ## |\n";
std::cout << " ## @\n";
std::cout << " ## |\n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 4:
std::cout << " #######\n";
std::cout << " ## | \n";
std::cout << " ## @ \n";
std::cout << " ## -| \n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 5:
std::cout << " #######\n";
std::cout << " ## | \n";
std::cout << " ## @ \n";
std::cout << " ## -|- \n";
std::cout << " ##\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 6:
std::cout << " #######\n";
std::cout << " ## | \n";
std::cout << " ## @ \n";
std::cout << " ## -|- \n";
std::cout << " ## / \n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
case 7:
std::cout << " #######\n";
std::cout << " ## | \n";
std::cout << " ## @ \n";
std::cout << " ## -|-\n";
std::cout << " ## / \\\n";
std::cout << " ##\n";
std::cout << " #########\n";
break;
}
std::cout << "\n\n";
}
void PrintTopic(const std::string &SelectedTopic) {
std::cout << "Topic = " << SelectedTopic;
std::cout << "\n\n";
}
void PrintUnderscore(const std::string &HiddenWord, std::vector <char> &ReplaceUnderscore) {
std::vector <char> Output;
if (ReplaceUnderscore.size() > 0) {
if (ReplaceUnderscore.size() == 1) {
for (size_t i = 0; i < HiddenWord.length(); i++) {
if (ReplaceUnderscore[0] == HiddenWord[i]) {
std::cout << HiddenWord[i] << " ";
}
else {
std::cout << "_ ";
}
}
}
else {
for (size_t i = 0; i < HiddenWord.length(); i++) {
Output.push_back('_');
}
for (size_t j = 0; j < ReplaceUnderscore.size(); j++) {
for (size_t i = 0; i < Output.size(); i++) {
if (HiddenWord[i] == ReplaceUnderscore[j]) {
Output[i] = ReplaceUnderscore[j];
}
}
}
for (size_t i = 0; i < Output.size(); i++) {
std::cout << Output[i] << " ";
}
}
}
else {
for (size_t i = 0; i < HiddenWord.length(); i++) {
std::cout << "_ ";
}
}
std::cout << "\n\n";
}
void CheckGameStatus(const std::vector <char> &ReplaceUnderscore, const std::string &HiddenWord) {
std::string VectorString(ReplaceUnderscore.begin(), ReplaceUnderscore.end());
int Repeat = 0;
int Correct = 0;
for (size_t j = 0; j < HiddenWord.length(); j++) {
for (size_t i = 0; i < HiddenWord.length(); i++) {
if (HiddenWord[j] == HiddenWord[i] && j != i) {
Repeat += 1;
}
}
}
for (size_t j = 0; j < VectorString.length(); j++) {
for (size_t i = 0; i < HiddenWord.length(); i++) {
if (VectorString[j] == HiddenWord[i]) {
Correct += 1;
}
}
}
Correct -= Repeat;
if (Correct == VectorString.length() && Correct == HiddenWord.length()) {
GameWon(HiddenWord);
}
else if (Errors == 7) {
GameLost(HiddenWord);
}
}
std::string InputGuess() {
std::string Guess;
std::cout << "Enter your guess: ";
std::getline(std::cin, Guess);
return Guess;
}
std::vector <char> TestGuess(const std::string &Guess, const std::string &HiddenWord, std::vector <char> &ReplaceUnderscore) {
int Correct = 0;
for (size_t i = 0; i < HiddenWord.length(); i++) {
if (HiddenWord[i] == Guess[0]) {
ReplaceUnderscore.push_back(HiddenWord[i]);
Correct += 1;
}
}
if (Correct == 0) {
Errors += 1;
}
return ReplaceUnderscore;
}
void GameWon(const std::string &HiddenWord) {
std::cout << "You won! Hidden word was " << HiddenWord;
AskToPlayAgain();
}
void GameLost(const std::string &HiddenWord) {
std::cout << "You lost! Hidden word was " << HiddenWord;
AskToPlayAgain();
}
void AskToPlayAgain() {
std::string Choice;
std::cout << "\n\nDo you want to play again? [Y/N]: ";
std::cin >> Choice;
switch (Choice[0]) {
case 'y':
case 'Y':
break;
case 'n':
case 'N':
exit(0);
break;
default:
std::cout << "\n\nIncorrect answer, try again.";
AskToPlayAgain();
break;
}
Reset();
PlayGame();
}
void Reset() {
// ???????????????????/////
std::cout << std::flush;
system("CLS");
}
|