// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "Word_List.h"
#include "Math/UnrealMathUtility.h"
void UBullCowCartridge::BeginPlay() // When the game starts
{
Super::BeginPlay();
Isogram = GetValidWords(Hidword_List);
SetUpGame();
PrintLine(TEXT("no of words in list: %i"),Isogram.Num());
}
void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
GuessCheck(Input);
IsogramCheck(Input);
}
void UBullCowCartridge::SetUpGame()
{
ClearScreen();
HiddenWord = Isogram[FMath::RandRange(0,Isogram.Num())];
PrintLine(TEXT("%s"),*Isogram[FMath::RandRange(0,GetValidWords(Hidword_List).Num()-1)]);
Lives = HiddenWord.Len();
PrintLine(TEXT("Wellcome to bull and cow"));
PrintLine(TEXT("Guess The Hiddeword , Its an isogram, you have %i lives."),Lives);
//PrintLine(TEXT("The hidden word is: %s"), *HiddenWord);
}
bool UBullCowCartridge::Endgame(bool end)
{
PrintLine("Press enter to play again");
SetUpGame();
return false;
}
bool UBullCowCartridge::IsogramCheck(FString Iso_Input) const
{
for (int32 Index=0,Comparison; Index < Iso_Input.Len(); Index++)
{
for ( Comparison= Index+1; Comparison < Iso_Input.Len(); Comparison++)
{
if (Iso_Input[Index] == Iso_Input[Comparison])
{
// PrintLine(TEXT("%s:Is not an isogram"),*Iso_Input);
return false;
}
}
}
return true;
}
void UBullCowCartridge::GuessCheck(const FString& Guess)
{
int32 Lastno = Lives-1;
if (HiddenWord == Guess)
{
SetUpGame();
PrintLine(TEXT("%s : is correct answer""\nYou Win!..Play Again"),*Guess);
}
if (HiddenWord != Guess)
{
--Lives;
PrintLine(TEXT("you lost 1 life,you have: %i lives..Try again"),Lives);
}
if(Lives==2)
{
PrintLine(TEXT("HINT: the word start with: %c and ends with: %c"),HiddenWord[0],HiddenWord[Lastno]);
}
if (Lives<1)
{
Endgame(true);
}
FBullCowCount Score = BullCowCount(Guess);
PrintLine(TEXT("you have %i bulls and %i cows"),Score.Bulls,Score.Cows);
}
TArray<FString> UBullCowCartridge::GetValidWords(const TArray<FString>& _WordList)
{
TArray<FString> ValidWords;
for (FString WordNum : _WordList)
{
if (WordNum.Len() > 4 && WordNum.Len() < 15)
{
if (IsogramCheck(WordNum))
{
ValidWords.Emplace(WordNum);
}
}
}
return ValidWords;
}
FBullCowCount UBullCowCartridge::BullCowCount(const FString& Guess) const
{
FBullCowCount Count;
for (int32 BullIndex = 0; BullIndex < Guess.Len(); BullIndex++)
{
if (Guess[BullIndex] == HiddenWord[BullIndex])
{
Count.Bulls++;
continue;
}
for (int32 CowIndex =0; CowIndex < Guess.Len(); CowIndex++)
{
if (Guess[BullIndex] == HiddenWord[CowIndex])
{
Count.Cows++;
break;
}
}
}
return Count;
}
//welcome player---
//store player input---
//give a hidden isogram---
//compare player guess with hidden word---
//assign lives with no of words in isogram---
//if false reduce a life---
//display win and restart game---
//check for repeating words compare them---