Hello everyone!
I tried to repeat the actions made in lecture #68 (Checking Characters Part 2), but when I compiled code and launched the game, my Isogram check doesn’t work.
When I try to guess the word with the repeated letters (for example “ccake”) - the game takes away 1 live and suggests to try again (instead of expected notifying “No repeating letters, guess again”)
Could anybody help to identify where I went wrong here?
// 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(TEXT("The HiddenWord is: %s."), *HiddenWord); // Debug Line
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver)
{
ClearScreen();
SetupGame();
}
else // Checking PlayerGuess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
// Welcoming the player
PrintLine(TEXT("Hi! Welcome to the Bull Cows Game!"));
HiddenWord = TEXT("cakes");
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word"), HiddenWord.Len());
PrintLine(TEXT("Your have %i lives."), Lives);
PrintLine(TEXT("Type in your guess \nand press Enter to Continue...")); // Prompt player for guess
// const TCHAR HW[] = TEXT("plums");
// PrintLine(TEXT("Character 1 of the hidden word is: %c"), HiddenWord[0]); // print "c"
// PrintLine(TEXT("The 4th character of HW is: %c"), HW[3]); // prints "m"
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("\nPress enter to play again."));
}
void UBullCowCartridge::ProcessGuess(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))
{
PrintLine(TEXT("No repeating letters, guess again"));
return;
}
// Remove Life
PrintLine(TEXT("Lost a life!"));
--Lives;
if (Lives <= 0)
{
ClearScreen();
PrintLine(TEXT("You have no lives left!"));
PrintLine(TEXT("The hidden word was: %s"), *HiddenWord);
EndGame();
return;
}
// show the player the buuls and cows
PrintLine(TEXT("Guess again, you have %i lives left"), Lives);
}
bool UBullCowCartridge::IsIsogram(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 true;
}
}
}
return true;
// int32 Index = 0;
// int32 Comparison = Index + 1;
// for (int32 Comparison = Index + 1; Comparison < Word.Len(); Comparison++)
// {
// if (Word[Index] == Word[Comparison])
// {
// return false;
// }
// }
// For each letter
// Start at element [0].
// Compare against the next letter.
// Until we reach [Word.Len() -1].
// if any are the same return false
}