This repository has been archived by the owner on Jul 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
210 lines (188 loc) · 7.07 KB
/
main.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
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
#include <iostream>
#include <string>
#include "FBullCowGame.h"
using FString = std::string;
using int32 = int;
void PrintIntro();
void SetGame();
void PrintStartMessage();
void PlayGame();
void PrintBCCount(FBullCowCount);
void PrintSummary();
FString GetGuess();
FString LowerCaseGuess(FString);
void DifficultyErrorMessage(EDifficultyStatus);
void GuessErrorMessage(FString);
bool AskToPlayAgain();
FBullCowGame BCGame; // instantiate a new game, globally
// top-level function for the Bulls and Cows game
int main()
{
do{
PrintIntro();
SetGame();
PrintStartMessage();
PlayGame();
PrintSummary();
}
while(AskToPlayAgain());
std::cout << std::endl;
return 0; // exit the application
}
// introduces the game
void PrintIntro()
{
std::cout << "\n\n\n";
std::cout << "Art thanks to: https://asciiart.website/index.php?art=animals/cows\n\n\n\n";
std::cout << " ,/ \\, / \\\n";
std::cout << " ((__,-\"\"\"-,__)) ((__-^^-,-^^-__))\n";
std::cout << " `--)~ ~(--` `-_---' `---_-'\n";
std::cout << " .-'( )`-, <__|o` 'o|__>\n";
std::cout << " `~~`d\\ /b`~~` \\ ` /\n";
std::cout << " | | ): :(\n";
std::cout << " (6___6) :o_o:\n";
std::cout << " `---` \" - \"\n\n\n";
std::cout << "Welcome to Bulls and Cows, a fun word game!\n\n";
std::cout << "In this game, you have to guess the isogram that I will be thinking of.\n";
std::cout << "(An isogram is a word without any repeatiing letters.)\n";
return;
}
// asks how many letters the player wants
void SetGame()
{
FWordLengthRange WLRange = BCGame.GetWordLengthRange();
FString Difficulty = "";
EDifficultyStatus DStatus = EDifficultyStatus::Not_Number;
std::cout << "\nLet's set the difficulty.";
while(DStatus!=EDifficultyStatus::OK){
std::cout << " How many letters would you like the isogram to have? (";
std::cout << WLRange.MyWLMin << "-" << WLRange.MyWLMax << "):";
getline(std::cin, Difficulty); // gets input from the player
DStatus = BCGame.CheckDifficultyValidity(Difficulty); // checks if it is valid
if (DStatus != EDifficultyStatus::OK) { DifficultyErrorMessage(DStatus); }; // if not, prints error message
};
BCGame.Set(Difficulty); // set hidden word in BCGame
return;
}
// prints an error message if difficulty request is not valid
void DifficultyErrorMessage(EDifficultyStatus DStatus)
{
std::cout << std::endl << "This is not a valid amount of letters: ";
switch (DStatus) {
case EDifficultyStatus::Not_Number:
std::cout << "it is not a number.";
break;
case EDifficultyStatus::Out_of_Bounds:
FWordLengthRange WLRange = BCGame.GetWordLengthRange();
std::cout << "it is out of bounds (";
std::cout << WLRange.MyWLMin << "-" << WLRange.MyWLMax << ").";
break;
}
std::cout << " Let's try again.";
return;
}
// confirms how many letters to use, informs how many max. tries, and invites to start
void PrintStartMessage()
{
std::cout << "\nOkay, I thought of a " << BCGame.GetWordLength() << " letter word!\n";
std::cout << "Can you guess it within " << BCGame.GetMaxTries() << " tries?\n";
return;
}
// iterates the number of turns, asking guesses and processing them
void PlayGame()
{
while (BCGame.GetCurrentTry() <= BCGame.GetMaxTries()) {
FString Guess = GetGuess(); // get guess from user
if (!BCGame.IsLowercase(Guess)) { Guess = LowerCaseGuess(Guess); } // convert guess to lowercase, if necessary
std::cout << "Your guess was: " << Guess;
if (BCGame.CheckGuessValidity(Guess)==EGuessStatus::OK) // check if guess is valid
{
BCGame.IncreaseCurrentTry(); // if so, increase try number...
FBullCowCount BCCount = BCGame.CountBullsCows(Guess); // ... print bulls and cows number, ...
PrintBCCount(BCCount);
if (BCGame.IsGameWon()) { // and check if game is won
return;
}
}
else { // otherwise warns user what was wrong
GuessErrorMessage(Guess);
}
}
return;
}
// gets a guess from the player and turns it to lowercase
FString GetGuess()
{
FString Guess="";
std::cout << std::endl << std::endl << "Try " << BCGame.GetCurrentTry() << " of ";
std::cout << BCGame.GetMaxTries() << ". Enter your guess: ";
getline(std::cin, Guess);
return Guess;
}
FString LowerCaseGuess(FString Guess)
{
for (int32 i = 0; i != Guess.length(); i++) // makes each char in Guess lowercase
{
Guess[i] = std::tolower(Guess[i]);
}
return Guess;
}
void GuessErrorMessage(FString Guess)
{
std::cout << std::endl << "This guess is not valid: ";
switch (BCGame.CheckGuessValidity(Guess)) {
case EGuessStatus::Forbidden_Characters:
std::cout << "it has forbidden characters.";
break;
case EGuessStatus::Not_Isogram:
std::cout << "it has repeated letters.";
break;
case EGuessStatus::Wrong_Size:
std::cout << "it has wrong size (should be " << BCGame.GetWordLength() << " letters).";
break;
}
std::cout << " Let's try again.";
}
// prints bulls and cows counts
void PrintBCCount(FBullCowCount BCCount)
{
std::cout << std::endl << "\tBulls: " << BCCount.MyBulls;
std::cout << std::endl << "\tCows: " << BCCount.MyCows;
}
// prints final message of the game, either for victory or failure
void PrintSummary()
{
if (BCGame.IsGameWon()) {
std::cout << std::endl;
std::cout << " ..---.\n";
std::cout << " // |\\||\\\n";
std::cout << " \\ / ///;||\\|;\\ \\ /\n";
std::cout << " o .__// \\\\____//\\_,. o\n";
std::cout << " / \\ Y{_\\_/ \\'' = __/ / \\\n";
std::cout << " \\___ (o) (o) } /\n";
std::cout << " / :--' SACRE MOO!\n";
std::cout << " .---/ \\_ `__\\-.\n";
std::cout << " / `--\\___(o'o) \\\n";
std::cout << " \\ / | \\ `====' |\\ /\n";
std::cout << " o `. `. .' .' o\n";
std::cout << " / \\ (`. `. ' .') / \\\n";
std::cout << " ( `. `...' )\n";
std::cout << " ( .+. `-. )\n";
std::cout << " (.-' .>-._ `-.)\n";
std::cout << " \\ / (___.-' `-.___) \\ /\n";
std::cout << " o ( ) o\n";
std::cout << " / \\ ( ) / \\\n";
std::cout << std::endl << "Congratulations, you guessed it!!";
}
else {
std::cout << std::endl << "Your tries are up... better luck next time!";
}
}
// asks if player wants to play again and gets response
bool AskToPlayAgain()
{
std::cout << std::endl << "Would you like to play again? (y/n)";
FString Answer = "";
getline(std::cin, Answer);
return (Answer[0] == 'y' || Answer[0] == 'Y');
}