-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (87 loc) · 2.51 KB
/
main.cpp
File metadata and controls
104 lines (87 loc) · 2.51 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
/* This is the console executable that makes use of
the BullCow class. This acts as the view in a MVC
patters responsible for all user interaction. For game logic
see FBullCowGame class
*/
#include <iostream>
#include <string>
#include "FBullCowGame.h"
using FText = std::string;
using int32 = int;
void PrintIntro();
void PlayGame();
FText GetValidGuess();
bool AskToPlayAgain();
FBullCowGame BCGame; // instantiate a new game
// entry point into the application
int main()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0;
}
void PrintIntro()
{
// int32 WORD_LENGTH = BCGame.GetHiddenWordLength();
// introduce the game
std::cout << "\n\nWelcome to Bulls and Cows, a fun word game.\n";
std::cout << "Can you guess the " << BCGame.GetHiddenWordLength();
std::cout << " letter isogram that I'm thinking of?\n";
return;
}
void PlayGame()
{
int32 MaxTries = BCGame.GetMaxTries();
// loop asking for guesses while the game is NOT won
// and there are still tries remaining
while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries){
FText Guess = GetValidGuess();
// submit valild guess to game
FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << ". Cows: " << BullCowCount.Cows << "\n\n";
}
}
FText GetValidGuess() // loop continually until valid guess is given
{
EGuessStatus Status = EGuessStatus::Invalid_Status;
FText Guess = "";
do {
// get a guess from the player
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try: " << CurrentTry << " of " << BCGame.GetMaxTries();
std::cout << "\nEnter a guess: ";
std::getline(std::cin, Guess);
Status = BCGame.CheckGuessValidity(Guess);
switch (Status)
{
case EGuessStatus::Wrong_Length:
std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n\n";
break;
case EGuessStatus::Not_IsoGram:
std::cout << "Please enter an isogram (no letters used more than once).\n\n";
break;
case EGuessStatus::Not_Lowercase:
std::cout << "Please enter all guesses in lowercase only.\n\n";
break;
default:
// assume the guess is valid
break;
}
} while (Status != EGuessStatus::OK);
return Guess;
}
bool AskToPlayAgain()
{
std::cout << "Do you want to play again (y/n)? ";
FText Response = "";
std::getline(std::cin, Response);
return (Response[0] == 'y') || (Response[0] == 'Y'); // return value of 1 if conditions met
return false;
}