Hey Guys,
I’m in the middle of the bull cow course and I’ve gotten a little lost, maybe you can help me understand.
So this is my code of BullCowCartridge.cpp:
#include “BullCowCartridge.h”
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
SetupGame();
}
void UBullCowCartridge::OnInput(const FString& Guess) // When the player hits enter
{
if (bGameOver) {
ClearScreen();
SetupGame();
PrintLine(TEXT("Trying again? good luck!"));
}
else { //check player guess
ProcessGuess(Guess);
}
}
void UBullCowCartridge::SetupGame() {
HiddenWord = TEXT("Kitty");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Hi There!"));
PrintLine(TEXT("Guess the hidden word which is %i letter \nlong.\n"), HiddenWord.Len());
PrintLine(TEXT("You have %i Lives."), Lives);
PrintLine(TEXT("Type it here and press enter!"));
/* const TCHAR HW[] = TEXT("kitty");
PrintLine(TEXT("Character number 1 of the hidden word is: %c"), HiddenWord[2]);
HW; */
}
void UBullCowCartridge::EndGame() {
bGameOver = true;
PrintLine(TEXT("\nPress enter to play again"));
}
void UBullCowCartridge::ProcessGuess(FString Guess) {
if (HiddenWord == Guess) {
PrintLine(TEXT("You Win!"));
EndGame();
return;
}
if (HiddenWord.Len() != Guess.Len())
{
PrintLine(TEXT("Sorry, try guessing again, \nThe hidden word is %i Charachters long"), HiddenWord.Len());
PrintLine(TEXT(" you have %i lives remaining."), Lives);
return;
}
if (!IsIsogram(Guess)) {
PrintLine(TEXT("No repeating letters, guess again."));
return;
}
PrintLine(TEXT("lost a life!"));
--Lives;
if (Lives <= 0) {
ClearScreen();
PrintLine(TEXT("You have no lives left, you lose."));
PrintLine(TEXT("The hidden word was: %s "), *HiddenWord);
EndGame();
return;
}
PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}
bool UBullCowCartridge::IsIsogram(FString Word) const {
int32 Index = 0;
int32 Comparison = Index + 1;
for (; Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison]) {
return false;
}
}
return true;
/* for (int32 Index = 0; Index < Word.Len(); Index++) {
PrintLine(TEXT("%c"), Word[Index]);
}
*/
/* for each letter
start at [0]
Compare against the next letter
until we reach [Word.len() -1]
if any letters are the same return
*/
}
And this is the header file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include “CoreMinimal.h”
#include “Console/Cartridge.h”
#include “BullCowCartridge.generated.h”
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(FString Guess);
bool IsIsogram(FString Word) const;
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};
This code does work according to lesson “checking parameters 1”
I just can’t figure out how does the user input is being used both with the word “Guess” and the word “Word”, how does the value being directed into these FStrings?
Please help!