forked from seanamassa/BullsCows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBullCowCartridge.cpp
More file actions
149 lines (123 loc) · 3.6 KB
/
Copy pathBullCowCartridge.cpp
File metadata and controls
149 lines (123 loc) · 3.6 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
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
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Check PlayerGuess
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
//Welcome the player
PrintLine(TEXT("Welcome to Bull Cows Group 9 Version"));
HiddenWord = GetValidWords(Words)[FMath::RandRange(0, GetValidWords(Words).Num() -1)];
Lives = HiddenWord.Len() * 2;
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You have %i lives."), Lives);
PrintLine(TEXT("Type in your guess and \npress enter to continue...")); // Prompt player for guess
PrintLine(TEXT("The HiddenWord is: %s."), *HiddenWord); // Debug line
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nPress enter to play again."));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == HiddenWord)
{
PrintLine(TEXT("You have won!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("The hidden word is %i letters long."), HiddenWord.Len());
PrintLine(TEXT("Sorry, try guessing again! \nYou have %i lives remaining."), Lives);
return;
}
// Check if Isogram
if (!IsIsogram(Guess))
{
/* code */
PrintLine(TEXT("No repeating letters, guess again!"));
--Lives;
return;
}
// Remove Life
PrintLine(TEXT("Lost a life!"));
--Lives;
if (Lives <=0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left and proceed to get mauled by the lions!")); // add lion roar audio when no lives are left
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
EndGame();
return;
}
// Show player Bulls and Cows
FBullCowCount Score = GetBullCows(Guess);
PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}
bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
for (int32 Index = 0; Index < Word.Len(); Index++)
{
for (int32 Comparison = Index +1; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
}
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString> &WordList) const
{
TArray<FString> ValidWords;
for (FString Word : WordList)
{
if (Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
return ValidWords;
}
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Count;
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
{
if (Guess[GuessIndex] == HiddenWord[GuessIndex])
{
Count.Bulls++;
continue;
}
for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
{
if (Guess[GuessIndex] == HiddenWord[HiddenIndex])
{
Count.Cows++;
break;
}
}
}
return Count;
}