1.BullCowartridge.cpp
// 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();
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
if (bGameOver) //If the game is over then ClearScreen() and Setupgame().
{
ClearScreen();
SetupGame();
}
else//If the game isn't over then check the player guess
{
ProcessGuess(Input);
}
}
void UBullCowCartridge::SetupGame()
{
//Welcoming the player.
PrintLine(TEXT("Welcome to Bulls and Cows!"));
HiddenWord = TEXT("cake");//Set the hidden word.
Lives = HiddenWord.Len();
bGameOver = false;
PrintLine(TEXT("Guess the %i letter word!"), HiddenWord.Len());
PrintLine(TEXT("You have only %i lives."), Lives);
PrintLine(TEXT("Type in your guess and\n press enter to continue....."));
const TCHAR HW[] = TEXT("cake");
PrintLine(TEXT("First letter of the hidden word is : %c"), HiddenWord[0]);
PrintLine(TEXT("Last letter of the word is : %c"), HiddenWord[3]);
}
void UBullCowCartridge::EndGame()
{
bGameOver = true;
PrintLine(TEXT("Press enter to play again."));
}
void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (HiddenWord == Guess)//Cheking the player guess.
{
PrintLine(TEXT("Your answer is correct!"));
EndGame();
return;
}
if (Guess.Len() != HiddenWord.Len())
{
PrintLine(TEXT("Sorry,the word has %i letters."), HiddenWord.Len());
PrintLine(TEXT("You have %i lives remaining!"), Lives);
EndGame();
return;
}
//Cheking if it is a isogrm.
if (!IsIsogram(Guess))
{
PrintLine(TEXT("No repeating letters,Guess again!"));
return;
}
PrintLine(TEXT("You lost a live!"));
--Lives;
if(Lives <= 0)
{
EndGame();
PrintLine(TEXT("You have no lives!"));
PrintLine(TEXT("The hidden word was '%s'!"), *HiddenWord);
ClearScreen();
return;
}
//show the player bulls and cows
PrintLine(TEXT("Guess again,you have %i lives left."), Lives);
}
bool UBullCowCartridge::IsIsogram(FString Word)
{
int32 Index = 0;
int32 Comparison = Index + 1;
for (Comparison < Word.Len(); Comparison++)
{
if (Word[Index] == Word[Comparison])
{
return false;
}
}
return true;
// for ( = 0; < (); ++)
// {
// PrintLine(TEXT("%c"), Word[Index]);
// }
// For each letter
// star at element [0]
// compare aginst each letter
// untill we reach [Word.Len() -1]
// if any are the same return false.
}
2.BullCowCartridge.h
// 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);
// Your declarations go below!
private:
FString HiddenWord;
int32 Lives;
bool bGameOver;
};
Compile fails.Please help!