-
Notifications
You must be signed in to change notification settings - Fork 0
/
FBullCowGame.cpp
133 lines (106 loc) · 2.76 KB
/
FBullCowGame.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
#include "FBullCowGame.h"
#include <map>
#define TMap std::map // to make syntax unreal friendly
FBullCowGame::FBullCowGame() {
Reset();
}
void FBullCowGame::Reset() {
bGameIsWon = false;
MyCurrentTry = 1;
const FString HIDDEN_WORD = "muskan"; // this word must be an isogram
MyHiddenWord = HIDDEN_WORD;
return;
}
int FBullCowGame::GetMaxTries() const {
TMap<int32, int32> WordLengthToMaxTries = { {3,5} ,{4,5},{5,7},{6,10},{7,10} };
return WordLengthToMaxTries[MyHiddenWord.length()];
}
int FBullCowGame::GetCurrentTry() const{
return MyCurrentTry;
}
int32 FBullCowGame::GetHiddenWordLength() const {
return MyHiddenWord.length();
}
bool FBullCowGame::IsGameWon() const{
return bGameIsWon;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) {
// if the guess isn't isogram,
if (!IsIsogram(Guess)) {
return EGuessStatus::Not_Isogram;
}
// if the guess isn't all lowercase,
else if (!IsLowerCase(Guess)) {
return EGuessStatus::Not_Lowercase;
}
// if the guess length is wrong,
else if (Guess.length() != MyHiddenWord.length()) {
return EGuessStatus::World_Length;
}
// otherwise
else {
return EGuessStatus::OK;
}
}
// receives a valid guess, increments turns and return counts
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess) {
// increment the turn number
MyCurrentTry++;
// setup a return variable
FBullCowCount BullCowCount;
int32 HiddenWordLength = MyHiddenWord.length();
// loop through all letters in the hidden word
for (int32 HdnChar = 0; HdnChar < HiddenWordLength; HdnChar++) {
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++) {
// if they match then
if (Guess[GChar] == MyHiddenWord[HdnChar]){
// if they are in the same place
if (HdnChar == GChar){
// increment bulls
BullCowCount.Bulls++;
} else {
// increment cows
BullCowCount.Cows++;
}
}
}
}
// if thhey match then
// increment bulls if they are in the same place
// increment cows if not
if (BullCowCount.Bulls == HiddenWordLength) {
bGameIsWon = true;
}
else {
bGameIsWon = false;
}
return BullCowCount;
}
bool FBullCowGame::IsIsogram(FString Guess) {
// treat 0 and 1 letter word as isograms because the letter can't be repeated
if (Guess.length() <= 1) {
return true;
}
// setup a map
TMap<char , bool> LetterSeen;
// loop through all the letters in the map
for (auto Letter : Guess) { // for all letters of the hidden word
Letter = tolower(Letter); // handle mixed case
if (LetterSeen[Letter]) {
return false;
}
else {
LetterSeen[Letter] = true;
}
}
return true;
}
bool FBullCowGame::IsLowerCase(FString Guess) {
for (auto Letter : Guess) {
if (!islower(Letter)) {
return false;
}
}
return true;
}