// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
//PrintLine(FString::Printf(TEXT("The HiddenWord is: %s"), *HiddenWord));
//We haven't used the FString::Printf() in above line but it will still work for this project as it is included in PrintLine function..
//PrintLine(TEXT("The HiddenWord is: %s. \nIt is %i characters long."), *HiddenWord, HiddenWord.Len()); //Debug Line
}
void UBullCowCartridge::OnInput(const FString &Input) // When the player hits enter
{
ProcessGuess(Input);
}
void UBullCowCartridge::SetupGame()
{
//Welcome message
PrintLine(TEXT("HELLO,WELCOME TO BULLS & COWS GAME!"));
PrintLine(TEXT("PRESS TAB TO ACCESS THE TERMINAL..."));
HiddenWord = TEXT("orange");
Lives = HiddenWord.Len(); //Can't declare lives variable directly here because of scope issues..
bGameOver = false;
//Display Lives
PrintLine(TEXT("YOU HAVE %i LIVES TO CRACK THE WORD."), HiddenWord.Len());
PrintLine(TEXT("GUESS THE %i LETTER ISOGRAM\nAND PRESS ENTER.."), HiddenWord.Len());
}
//Press Enter to PlayAgain
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Please Press Enter to Play Again..."));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
// Check PlayerGuess
else
{
if (HiddenWord == Guess)
{
PrintLine(TEXT("FABULOUS, YOU NAILED IT!!"));
EndGame();
return;
}
//Check for correct length of word
//Prompt to Guess Again
else if (HiddenWord.Len() > Guess.Len())
{
PrintLine(TEXT("Hidden word is %i characters long.\nPlease try again...."), HiddenWord.Len());
PrintLine(TEXT("No lives deducted %i lives remaining"), Lives);
return;
}
//Check for Isogram
else if (!IsIsogram(Guess))
{
PrintLine(TEXT("CAUTION! Repetition is not allowed."));
return;
}
//Deduct lives
else
{
//Show lives left
PrintLine(TEXT("You have lost a life, %i lives remaining."), --Lives);
//Check if lives > zero
//If No, show GameOver and HiddenWord
if (Lives == 0)
{
ClearScreen();
PrintLine(TEXT("BAD LUCK! YOU HAVE LOST THE GAME.\nThe Hidden Isogram was %s."), *HiddenWord);
EndGame();
}
}
}
}
bool UBullCowCartridge::IsIsogram(FString Word) const
{
// For each letter
int32 Index = 0;
int32 Comparison = Index + 1;
for (; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
}
return true;
// Start with element [0]
// compare against next letter
// untill we reach [Word.Len() - 1]
// if any are same return false
}```
Its going felt little bit difficulty const member function rest all ok..Nice work gamedev team..
1 Like
Awesome!
1 Like