-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathFBullCowGame.cpp
More file actions
107 lines (91 loc) · 2.55 KB
/
FBullCowGame.cpp
File metadata and controls
107 lines (91 loc) · 2.55 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
104
105
106
107
#pragma once
#include "FBullCowGame.h"
#include <map>
// to make syntax Unreal friendly
#define TMap std::map
using int32 = int;
FBullCowGame::FBullCowGame() { Reset(); } // default 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,16}, {7,20} };
return WordLengthToMaxTries[MyHiddenWord.length()];
}
void FBullCowGame::Reset()
{
const FString HIDDEN_WORD = "plane"; // this MUST be an isogram
MyHiddenWord = HIDDEN_WORD;
MyCurrentTry = 1;
bGameIsWon = false;
}
EGuessStatus FBullCowGame::CheckGuessValidity(FString Guess) const
{
// we are returning ;)
if (!IsIsogram(Guess)) // if the guess isn't an isogram
{
return EGuessStatus::Not_Isogram;
}
if (!IsLowercase(Guess)) // if the guess isn't all lowercase
{
return EGuessStatus::Not_Lowercase;
}
if (Guess.length() != GetHiddenWordLength()) // if the guess length is wrong
{
return EGuessStatus::Wrong_Length;
}
return EGuessStatus::OK;
}
// receives a VALID guess, incriments turn, and returns count
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
MyCurrentTry++;
FBullCowCount BullCowCount;
int32 WordLength = MyHiddenWord.length(); // assuming same length as guess
// loop through all letters in the hidden word
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++)
{
// compare letters against the guess
for (int32 GChar = 0; GChar < WordLength; GChar++)
{
// if they match then
if (Guess[GChar] == MyHiddenWord[MHWChar])
{
(MHWChar == GChar) ? BullCowCount.Bulls++ : BullCowCount.Cows++;
}
}
}
if (BullCowCount.Bulls == WordLength)
{
bGameIsWon = true;
}
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; // setup our 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
}
LetterSeen[Letter] = true;// add the letter to the map
}
return true; // for example in cases where /0 is entered
}
bool FBullCowGame::IsLowercase(FString Word) const
{
for (auto Letter : Word)
{
if (!islower(Letter)) // if not a lowercase letter
{
return false;
}
}
return true;
}