I'm stuck in trying to implement a score

Hello, how you doing? I’ve been trying to implement a score point with a system that when the player has 3 or fewer lives, a question is prompted if the player wants to spend their score point on finding out what letter is the cow, based on the player input of yes or no. Preformatted text

// Fill out your copyright notice in the Description page of Project Settings.
#include "BullCowCartridge.h"
#include "HiddenWordList.h" 


void UBullCowCartridge::BeginPlay() // When the game starts
{
    Super::BeginPlay();

    Isograms = GetValidWords(Words);

    SetupGame();
}

void UBullCowCartridge::OnInput(const FString& PlayerInput) //When the player hits enter
{
    /*if the game is over ClearScreen() and SetupGame() the game
    else check player guess */
    if (bGameOver)
    {
        ClearScreen();
        SetupGame();    
    }
    else // checking player guess
    {
        ProcessGuess(PlayerInput);
    }
     if (Lives < 3)
    {
    SpendScore(PlayerInput);
    }  
}

void UBullCowCartridge::SetupGame()
{    
//Welcoming the player
    PrintLine(TEXT("Welcome to Bull Cows"));

    HiddenWord = Isograms[FMath::RandRange(0, Isograms.Num()-1)]; 
    Lives = HiddenWord.Len();
    bGameOver = false;

    PrintLine(TEXT("Guess the %i letters word!"), HiddenWord.Len()); // magic number remove!
    PrintLine(TEXT("The Hidden Word is: %s."), *HiddenWord);
    PrintLine(TEXT("You have %i lives."), Lives);
    PrintLine(TEXT("Type in your guess and\n press 'Enter' to continue "));
  
    IsIsogram(HiddenWord);
}

void UBullCowCartridge::EndGame()
{
    bGameOver = true;
    PrintLine(TEXT("Please press 'Enter' to play again..."));
}

void UBullCowCartridge::ProcessGuess(const FString& Guess) 
{  
    Score = Score + 10;

    if (Guess == HiddenWord)
    {
        PrintLine(TEXT("You have won, are you ready for the next word? "));
        PrintLine(TEXT("Your score is %i"), Score);
        EndGame();
        return;
    }

    if (Guess.Len() != HiddenWord.Len())
    {
        PrintLine(TEXT("The HiddenWord is %i letters long"), HiddenWord.Len());
        PrintLine(TEXT("Sorry, try guessing again! \nYou have %i remaning lives"), Lives);
        return;
    }
//check if isogram
    if (!IsIsogram(Guess))
    {
        PrintLine(TEXT("No repeting letter, try again"));
    }
    
//remove life
    PrintLine(TEXT("You 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 bulls and cows
    FBullCowCount Score = GetBullCows(Guess);

     PrintLine(TEXT("You have %i Bulls and %i Cows"), Score.Bulls, Score.Cows);
     PrintLine(TEXT("Guess again you have %i lives left"), Lives);

//check if life is > to zero 
}

bool UBullCowCartridge::IsIsogram(const FString& Word) const
{
    /*int32 comparison = i + 1;*/
    
    for (int32 i = 0; i < Word.Len(); i++) {
        for (int32 Comparison = i + 1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[i] == Word[Comparison])
            {
                return false;
            }
        }
    }
    return true;

   /* for each letter
        start at element[0]
        compare against the next letter
        until we reach[Guess.Len()-1]
        if any letters are the same we return false*/
}

TArray<FString>UBullCowCartridge::GetValidWords(const TArray<FString>& WordList) const
{
    TArray<FString>ValidWords;
    
    for (FString Word: WordList)
    {
        if (Word.Len() >= 4 && Word.Len() <= 8 && IsIsogram(Word))
        {
            ValidWords.Emplace(Word);
        }
        
    }
    return ValidWords;
}

FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
    FBullCowCount Count;

    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    { 
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
            {
                Count.Bulls++;
                continue;
            } 
        for (int32 HiddenIndex = 0; HiddenIndex < HiddenWord.Len(); HiddenIndex++)
        {
            if (Guess[GuessIndex] == HiddenWord[HiddenIndex]) 
            {
                Count.Cows++;
                break;
            }
        }
    }
    return Count;
}

void UBullCowCartridge::SpendScore(const FString& ConfirmW)
{
        PrintLine(TEXT("Would you like to spend your score points to find the cow?"));
        if (ConfirmW == "yes" && Score >= 30)
        {
            Score = Score - 30;
            PrintLine(TEXT("Your Score is: %i"), Score);
            PrintLine(TEXT("Your cow is: %c "),  );
        }
}

The Header

// 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"

struct FBullCowCount
	{
		int32 Bulls = 0;
		int32 Cows = 0;
	};

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(); //I Want to initialize my game on this function.
	void EndGame();
	void ProcessGuess(const FString& Guess);
	bool IsIsogram(const FString& Word) const;
	TArray<FString>GetValidWords(const TArray<FString>& WordList) const;
	FBullCowCount GetBullCows(const FString& Guess)const;
	void SpendScore(const FString& ConfirmW);

	// Your declarations go below!
	private:
		FString HiddenWord;
		int32 Lives;
		bool bGameOver;
		TArray<FString> Isograms;
		int32 Score;
};

Thank you in Advance <3

1 Like

Privacy & Terms