-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFBullCowGame.cpp
118 lines (102 loc) · 2.74 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
#pragma once
#include "FBullCowGame.h"
#include <map>
#define TMap std::map //to make syntax Unreal friendly
using int32 = int;
FBullCowGame::FBullCowGame() { Reset(); } //constructor
int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() const { return MyHiddenWord.length(); }
bool FBullCowGame::IsGameWon() const { return bGameIsWon; }
int32 FBullCowGame::GetMaxTries() const {
TMap<int32, int32> WordLengthToMaxTries{ {3,4}, {4,7}, {5,10}, {6,13}, {7,15} };
return WordLengthToMaxTries[MyHiddenWord.length()];
}
void FBullCowGame::Reset()
{
const fString HiddenWord = "plane";
MyHiddenWord = HiddenWord;
MyCurrentTry = 1;
bGameIsWon = false;
return;
}
FBullCowCount FBullCowGame::SubmitGuess(fString Guess)
{
//increment the turn number
MyCurrentTry++;
//setup a return variable
FBullCowCount BullCowCount;
// loop through all letter in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++)
{
//compare letter against the hidden word 1 @ a time
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++)
{
//if they match
if (Guess[GChar] == MyHiddenWord[MHWChar])
{
//if they're in the same plae
if (MHWChar == GChar) {
BullCowCount.Bulls++; //increment bulls
}
else {
BullCowCount.Cows++; //must be a cow
}
}
}
}
if (BullCowCount.Bulls == HiddenWordLength) {
bGameIsWon = true;
}
else {
bGameIsWon = false;
}
return BullCowCount;
}
bool FBullCowGame::IsIsogram(fString Word) const
{
//treat 0 and 1 letter words as Isograms
if (Word.length() <= 1) { return true; }
TMap<char, bool> LetterSeen;//set up map
for (auto Letter : Word) //for all letters of the word
{
Letter = tolower(Letter); //handle mixed case
if (LetterSeen[Letter]) //if the letter is in the map
{
return false; //we do NOT have an Isogram
}
else
{
LetterSeen[Letter] = true;
}
}
return true;
}
bool FBullCowGame::IsLowerCase(fString Word) const
{
for (auto Letter : Word)
{
if (!islower(Letter)) { return false; } //if not lowercase //return false
else { return true; }
}
return false;
}
EGuessStatus FBullCowGame::CheckGuessValidity(fString Guess) const
{
if (!IsIsogram(Guess)) //If the guess isn't an isogram
{
return EGuessStatus::Not_Isogram;
}
else if (!IsLowerCase(Guess)) //If the guess isn't all lowercase
{
return EGuessStatus::Not_Lowercase;
}
else if (Guess.length() != GetHiddenWordLength()) //If the length is wrong
{
return EGuessStatus::Wrong_Length;
}
else
{
return EGuessStatus::OK;
}
}