-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
139 lines (114 loc) · 3.16 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
/*
This is the console executable, that makes use of the BullCow class.
Acts as the view in the MVC pattern, and is responsible for all
user interaction. For game logic, see the FBullCowGame class..
*/
#pragma once
#include <iostream>
#include <string>
#include "FBullCowGame.h"
// To make syntax Unreal friendly
using int32 = int;
using FText = std::string;
// function prototypes outside of class
// Large functons for game play
void PrintIntro();
void PlayGame();
FText GetValidGuess();
void PrintGuess(FText Guess);
bool AskToPlayAgain();
void PrintGameSummary();
FBullCowGame BCGAME; // instantiate a new game, which we reuse across plays
int main()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
} while (bPlayAgain);
return 0;
}
void PrintIntro()
{
std::cout << "\n\nWelcome to Bulls and Cows, a fun word game." << std::endl;
std::cout << "Can you guess the " << BCGAME.GetHiddenWordLength() << " letter isogram I'm thinking of?" << std::endl;
std::cout << std::endl;
return;
}
// plays a single game to completion
void PlayGame()
{
BCGAME.Reset();
int32 MaxTries = BCGAME.GetMaxTries();
//loop for number of turns while the game is
// NOT won and there are still tries remaining
while (! BCGAME.IsGameWon() && BCGAME.GetCurrentTry() <= MaxTries)
{
FText Guess = GetValidGuess();
FBullCowCount BullCowCount = BCGAME.SubmitValidGuess(Guess);
std::cout << "Bulls = " << BullCowCount.Bulls;
std::cout << ". Cows = " << BullCowCount.Cows << std::endl;
PrintGuess(Guess);
}
PrintGameSummary();
return;
}
// Function will loop continuosly till it recieves a valid guess
FText GetValidGuess()
{
FText Guess = "";
EGuessStatus Status = EGuessStatus::Invalid;
do
{
// get the guess from the player
int32 CurrentTry = BCGAME.GetCurrentTry();
std::cout << "Enter guess #" << CurrentTry << " of " << BCGAME.GetMaxTries() << ": ";
std::getline(std::cin, Guess);
Status = BCGAME.CheckGuessValidity(Guess);
switch (Status)
{
case EGuessStatus::Wrong_Length:
std::cout << "Please enter a " << BCGAME.GetHiddenWordLength() << " letter word." << std::endl;
break;
case EGuessStatus::Not_Isogram:
std::cout << "Please enter a word without repeating letters." << std::endl;
break;
case EGuessStatus::Not_Lowercase:
std::cout << "Please enter a word in lowercase letters." << std::endl;
break;
default:
break; // assume guess is valid
}
std::cout << std::endl;
} while (Status != EGuessStatus::OK);
return Guess;
}
void PrintGuess(FText Guess)
{
// Repeat guess back do them
std::cout << "Your guess was: " << Guess << std::endl;
std::cout << "You have " << BCGAME.GetMaxTries() - BCGAME.GetCurrentTry()+1 << " guesses left" << std::endl;
std::cout << std::endl;
return;
}
bool AskToPlayAgain()
{
std::cout << "Do you want to play again with the same hidden word? (y/n) : ";
FText Response = "";
std::getline(std::cin, Response);
return ((Response[0] == 'y') || (Response[0] == 'Y'));
}
void PrintGameSummary()
{
if (BCGAME.IsGameWon())
{
std::cout << "Congratulations! You Win! " << std::endl;
}
else
{
std::cout << "Better Luck Next Time!" << std::endl;
}
return;
}