Hi Everyone,
I have added a couple of features such as:
- Game levels by multiply Lives;
- Help with examples of game rules after writing the word ‘help’ - NeedHelp() ;
- End game after writing the word ‘end’;
- Scores by counting wins;
- And the hardest part for me - hint:
- I have made TArray with 8 answers;
- Function to store every letter form hidden word - GetHint();
- Function to print another letter of the hidden word after e.g 3 loses - PrintHint().
This is my version of the game:
// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h"
#include "NextLetter.h"
void UBullCowCartridge::BeginPlay()
{
Super::BeginPlay();
Isograms = GetValidWords(Words);
GameLevel = 3;
WinLevel = 3;
LoseLevel = 2;
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& PlayerInput)
{
if (bGameOver)
{
ClearScreen();
if (Win == WinLevel && GameLevel > 1)
{
GameLevel--;
Win = 0;
}
SetupGame();
}
else
{
ProcessGuess(PlayerInput);
}
}
void UBullCowCartridge::SetupGame()
{
PrintLine(TEXT("Bulls & Cows welcoming the player!\n"));
PrintLine(TEXT("Write 'help' word if you need help!\nWrite 'end' to end the game.\n"));
HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num() - 1)];
Lives = HiddenWord.Len() * GameLevel;
HintOut = GetHint(HiddenWord);
bGameOver = false;
NumLetterIndex = 0;
Lose = 1;
// PrintLine(TEXT("Win: %i, GameLevel: %i"), Win, GameLevel); //Debug line
PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
PrintLine(TEXT("You have %i lives."), Lives);
PrintLine(TEXT("Enter the word..."));
// PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord); //Debug Line
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press enter to play again."));
}
void UBullCowCartridge::ProcessGuess(const FString& Guess)
{
if (Guess == TEXT("help"))
{
NeedHelp(true);
return;
}
if (Guess == TEXT("end"))
{
PrintLine(TEXT("Thanks for the game!\n\nYour scors: %i"), Scores);
GameLevel = 3;
Win = 0;
Scores = 0;
Lose = 1;
EndGame();
return;
}
if (Guess == HiddenWord)
{
PrintLine(TEXT("You have Won!"));
Lose = 1;
Win++;
Scores++;
EndGame();
return;
}
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters, guess again!"));
return;
}
if (HiddenWord.Len() != Guess.Len())
{
PrintLine(TEXT("The hidden word is %i characters long, try again!"), HiddenWord.Len());
return;
}
if (Guess != HiddenWord && Guess != TEXT("help"))
{
--Lives;
FBullCowCount Score = GetBullCows(Guess);
PrintLine(TEXT("You have %i Bulls and %i Cows."), Score.Bulls, Score.Cows);
if (Lives == 1)
{
PrintLine(TEXT("You have %i live left."), Lives);
}
else
{
PrintLine(TEXT("You have %i lives left."), Lives);
}
// PrintLine(TEXT("Lose: %i, NumLetterIndex: %i"), Lose, NumLetterIndex); // Debug line
if (Lose >= HiddenWord.Len() - LoseLevel && NumLetterIndex + 1 == HiddenWord.Len())
{
PrintLine(TEXT("\nYou are really suck!!!"));
PrintLine(TEXT("\nHidden word was: %s"), *HiddenWord);
PrintLine(TEXT("\nYour scors: %i\n"), Scores);
Win = 0;
GameLevel = 3;
Scores = 0;
EndGame();
return;
}
if (Lose >= HiddenWord.Len() - LoseLevel && GameLevel > 1)
{
PrintLine(TEXT("\nHINT:\n%s %s\n"), *NumLetter[NumLetterIndex], *PrintHint(HintOut, NumLetterIndex));
Lose = 0;
NumLetterIndex++;
}
if (Lives == 0 && NumLetterIndex + 1 < HiddenWord.Len())
{
PrintLine(TEXT("\nYou suck!!!"));
PrintLine(TEXT("\nHidden word was: %s"), *HiddenWord);
PrintLine(TEXT("\nYour scors: %i\n"), Scores);
Win = 0;
GameLevel = 3;
Scores = 0;
EndGame();
return;
}
else
{
PrintLine(TEXT("Try again!"));
}
Lose++;
}
}
bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
for (int32 PreIndex = 0; PreIndex < Word.Len() - 1; PreIndex++)
{
for (int32 Index = PreIndex, Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
}
}
return true;
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& GetWord) const
{
TArray<FString> ValidWords;
for (FString Word : GetWord)
{
if (Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word))
{
ValidWords.Emplace(Word);
}
}
// for (int32 Index = 0; Index < GetWord.Num(); Index++)
// {
// if (GetWord[Index].Len() >= 4 && GetWord[Index].Len() <= 8 && IsIsogram(GetWord[Index]))
// {
// ValidWords.Emplace(GetWord[Index]);
// }
// }
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;
}
TArray<TCHAR> UBullCowCartridge::GetHint(FString HintGuess)
{
TArray<TCHAR> HintOutGuess;
for (int32 X = 0; X < HintGuess.Len(); X++)
{
HintOutGuess.Emplace(HintGuess[X]);
}
return HintOutGuess;
}
FString UBullCowCartridge::PrintHint(TArray<TCHAR> PrintTar, int32 PrintIndex)
{
FString HintLetter;
for (int32 X = 0; X < PrintIndex + 1; X++)
{
HintLetter.AppendChar(PrintTar[X]);
}
return HintLetter;
}
void UBullCowCartridge::NeedHelp(bool Help)
{
if (Help)
{
PrintLine(TEXT("\nRole of the game:\n\n1. You have 3 levels(1 is the hardest).\n2. Bull - letter is in a correct place."));
PrintLine(TEXT("3. Cow - letter is in a string.\n4. After 3 mistakes you get hint.\n\nGood luck!!!"));
PrintLine(TEXT("\nEnter the word..."));
}
}
NextLetter.h:
#include "CoreMinimal.h"
const TArray <FString> NumLetter =
{
TEXT("First letter of hidden word:"),
TEXT("First 2 letters of hidden word:"),
TEXT("First 3 letters of hidden word:"),
TEXT("First 4 letters of hidden word:"),
TEXT("First 5 letters of hidden word:"),
TEXT("First 6 letters of hidden word:"),
TEXT("First 7 letters of hidden word:"),
TEXT("First 8 letters of hidden word:")
};
BullCowCartridge.h:
#pragma once
#include "CoreMinimal.h"
#include "Console/Cartridge.h"
#include "BullCowCartridge.generated.h"
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};
UCLASS(ClassGroup=(Custom), meta=(BlueprintSpawnableComponent))
class BULLCOWGAME_API UBullCowCartridge : public UCartridge
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void OnInput(const FString& Input) override;
void SetupGame();
void EndGame();
void ProcessGuess(const FString& Guess);
bool IsIsogram(const FString& Word) const;
TArray<FString> GetValidWords(const TArray<FString>& GetWords) const;
// void GetBullCows(const FString& Guess, int32& BullCount, int32& CowCount) const;
FBullCowCount GetBullCows(const FString& Guess) const;
TArray<TCHAR> GetHint(FString HintGuess);
FString PrintHint(TArray<TCHAR> PrintTar, int32 PrintIndex);
void NeedHelp(bool Help);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
TArray<FString> Isograms;
bool bSetLevel;
int32 GameLevel;
int32 Win;
int32 WinLevel;
int32 Lose;
int32 LoseLevel;
int32 NumLetterIndex;
TArray<TCHAR> HintOut;
int32 Scores = 0;
};
This course is great so far
Thanks,
Bart