-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBullsAndCows.cpp
152 lines (124 loc) · 4.07 KB
/
FBullsAndCows.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
#pragma once
#include "FBullsAndCows.h"
#include <map>
//=============================================== Make Friendly Syntax According To UnrealEngineC++ Standards =========================================================//
#define TMap std::map
using int32 = int;
//==================================================== All Methods/functionalities Implemented In BCGame ==============================================================//
// Default Constructor
FBullCowGame::FBullCowGame() { Reset(); }
// Get The Current Guessing Try
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
// Get The Actual Length Of The HiddenIsogram
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }
// Check If Game Is Won
bool FBullCowGame::IsGameWon() const { return bGameIsWon; }
// Get The Maximum Tries For The Player
int32 FBullCowGame::GetMaxTries() const
{
TMap <int32, int32> WordLengthToMaxTries{ {3,4}, {4,6}, {5,10}, {6,115} ,{7,20} };
return WordLengthToMaxTries[MyHiddenWord.length()];
}
// Reset The Game
void FBullCowGame::Reset()
{
const FString HIDDEN_WORD = "plane"; // This Must Be An Isogram
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
bGameIsWon = false;
return;
}
// Validte Player's Guess According To The Terms/Conditions
EGuess_Status FBullCowGame::IsGuessValid(FString Guess) const
{
//Check If The Word Is Not Isogram ,then:
if (!IsIsogram(Guess))
{
return EGuess_Status::Not_Isogram;
}
//Check If There's Any Lowercase Used ,then:
else if (!IsLowerCase(Guess))
{
return EGuess_Status::Not_LowerCase;
}
//Check If There Are Any Word Length Errors ,then:
else if (Guess.length() != GetHiddenWordLength())
{
return EGuess_Status::Wrong_Length;
}
//If Everythings Fine, No Errors Found, then:
else
{
return EGuess_Status::OK;
}
}
// Recieves A Validated Guess ,Increment Turn/try , And Return Counts.
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
//Increment CurrentTurn.
MyCurrentTry++;
//Setup Or Declare A Return Varibale.
FBullCowCount BullCowCount;
//Consider / Assume Same Lenght As Guess
int32 WordLength = MyHiddenWord.length();
//Loop Through All Letters In The HiddenWord
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) {
//Compare Letters Against Guess
for (int32 GChar = 0; GChar < WordLength; GChar++) {
//Check If They Match :
if (Guess[GChar] == MyHiddenWord[MHWChar]) {
//Increment Bulls, If They Are In The Same Place.
if (MHWChar == GChar) {
BullCowCount.Bulls++;
}
//Else Are On A Different Spot ,Then Increment Cows.
else {
BullCowCount.Cows++;
}
}
}
}
if (BullCowCount.Bulls == WordLength)
{
bGameIsWon = true;
}
else
{
bGameIsWon = false;
}
return BullCowCount;
}
// Check If The Player's Guess Is An Isogram i.e No Repaeted Letters
bool FBullCowGame::IsIsogram(FString Word) const
{
//Consider 0 and 1 Letters Words also as isograms
if (Word.length() <= 1) {return true;}
TMap <char, bool> LetterSeen; //setup our map
for (auto Letter : Word) //Loop through all the letters of the word (including numeric(0 to 9) / characters (#,$,% etc)
//[Auto] used above!, will detect and declare the letter type int,char
{
Letter = tolower(Letter); //handle mixed case words
//Check if the letter is in the map
if (LetterSeen[Letter]) {
//if the letter is in the map
return false;} //we do'nt have the isogram i.e false
else //otherwise
{
LetterSeen[Letter] = true;
} //add the letter to the map as seen i.e true
}
return true;//for example in cases where 0/1 is entered
}
// Check The Player's Guess For Uppercase Letters
bool FBullCowGame::IsLowerCase(FString Word) const
{
for (auto Letter : Word)
{
if (!islower(Letter))
{
return false;
}
}
return true;
}
//========================================================================== The-End ==================================================================================//