Bulls and Cows Extra credit

Added in Easy/Medium/Hard difficulty that can be selected when the game starts or when you lose. it effects the number of lives you start with and minimum/maximum number of characters allowed in a word accordingly, Easy also lets you see the second character of the hidden word. really enjoyed this section :slight_smile:


.cpp

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


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


   //PrintLine(TEXT("The Hidden Word is %s."), *HiddenWord); //Debug Line
   // PrintLine(TEXT("the number of valid words is %i"), ValidWord.Num());
   PrintLine(TEXT("Type in your difficulity to play(Easy/\nMedium/Hard)."));
}


void UBullCowCartridge::OnInput(const FString& PlayerInput) // When the player hits enter
{
    if (NewGame && !bGameOver)
    {
            ClearScreen();
            PrintLine(TEXT("Your Guess was: %s"),*PlayerInput);
            GuessProcessing(PlayerInput);
            return;
    }

    if (bGameOver) //sets up the game again if the player presses enter when the game is over
    {
        DifficulitySet(PlayerInput);
        ClearScreen();
        SetUpGame(); 
        return;
    }

    if (DifficulitySet(PlayerInput)) //Will set difficulity when the player inputs a valid string
    {
        ClearScreen();
        NewGame = true;
        SetUpGame();
        return;
    }
    
}


void UBullCowCartridge::SetUpGame() 
{
    bGameOver = false;

    PrintLine(TEXT("You've stumbled upon the ancient cows of \nthe forest,they demand you answer their \nriddle.")); //welcome and introduction
    PrintLine(FString::Printf(TEXT("Guess the %i letter word!"), HiddenWord.Len()));
    PrintLine(FString::Printf(TEXT("Remaining Lives: %i"), Lives));

    if (GameDifficulity == 1 || GameDifficulity == 0)
    {
    PrintLine(TEXT("The first character of the hidden word is:%c"), HiddenWord[0]);
    }
     if (GameDifficulity == -1)
    {
    PrintLine(TEXT("The first and second characters of the \nhidden word are:%c %c"), HiddenWord[0],HiddenWord[1]);
    }

}


bool UBullCowCartridge::DifficulitySet(const FString& Difficulity)
{

    if (Difficulity == "Easy")
    {
        ValidWord.Empty();
        GameDifficulity = -1;
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() * 2 + 2;
        NewGame = true;
        return true;
    }

    if (Difficulity == "Medium")
    {
        ValidWord.Empty();
        GameDifficulity = 0;
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() * 2;
        NewGame = true;
        return true;
    }

    if (Difficulity == "Hard")
    {
        ValidWord.Empty();
        GameDifficulity = 1;
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() + 1;
        NewGame = true;
        return true;
    }

    if (GameDifficulity == -1)
    {
        ValidWord.Empty();
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() * 2 + 2;
        return true;
    }
    
    if (GameDifficulity == 0)
    {
        ValidWord.Empty();
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() * 2;
        return true;
    }
    
    if (GameDifficulity == 1)
    {
        ValidWord.Empty();
        GetValidWord(ValidWord);
        HiddenWord = ValidWord[FMath::RandRange(0, ValidWord.Num()-1)];
        Lives = HiddenWord.Len() + 1;
        return true;
    }
    
    return false;
}


void UBullCowCartridge::GuessProcessing(const FString& Guess)
{
    if (Guess == HiddenWord) // checks if the player guessed correctly or not.
    {
        EndGame();
        return;
    }
        
    if (HiddenWord.Len() != Guess.Len()) //Checks if the word is the correct length.
    {
        PrintLine(FString::Printf(TEXT("The Length of the word is %i characters!\n"), HiddenWord.Len()));
        PrintLine(FString::Printf(TEXT("You lost a life! Lives remaining: %i\n\nplease try again."),--Lives));
        FBullCowCount Animals = GetBullCow(Guess);
        PrintLine(TEXT("You have %i Bulls and %i Cows."),Animals.BullCount,Animals.CowCount);
        LivesManager();
        return;
    }

    IsIsogram(Guess);

    if (!IsIsogram(Guess)) 
    {
        ClearScreen();
        LivesManager();
        PrintLine(TEXT("The hidden word can only be an isogram!"));
        PrintLine(FString::Printf(TEXT("You lost a life! Lives remaining: %i\n\nplease try again."), --Lives));
        FBullCowCount Animals = GetBullCow(Guess);
        PrintLine(TEXT("You have %i Bulls and %i Cows."),Animals.BullCount,Animals.CowCount);
        LivesManager();
        return;
    }
    
    if (HiddenWord.Len() == Guess.Len() && IsIsogram(Guess))
    {
        LivesManager();
        PrintLine(TEXT("Incorrect Guess! \n "));
        PrintLine(FString::Printf(TEXT("You lost a life! Lives remaining: %i\n\nplease try again."),--Lives));
        FBullCowCount Animals = GetBullCow(Guess);
        PrintLine(TEXT("You have %i Bulls and %i Cows."),Animals.BullCount,Animals.CowCount);
        LivesManager();
        return;
    }


}


bool UBullCowCartridge::IsIsogram(const FString& Word) //Checks if input is an isogram
{
    for (int32 Letter = 0; Letter < Word.Len() - 1; Letter++)
    {
        for (int32 ComparedLetter = Letter+1; ComparedLetter < Word.Len(); ComparedLetter++)// Checks if the two letters are identical if theyre in a different position within the string
        {
            if (Word[Letter] == Word[ComparedLetter])
            { 
                return false;
            }
        }
    }
    return true;
}



TArray<FString> UBullCowCartridge::GetValidWord(const TArray<FString>& Words) //Filters out the word list 
{
    
    for (FString WordCheck : HiddenWords)
    {
        if (WordCheck.Len() <= 8 + GameDifficulity && WordCheck.Len() >= 4 + GameDifficulity && IsIsogram(WordCheck))
        {
            ValidWord.Emplace(WordCheck);
        }
    }
    return ValidWord;
}


void UBullCowCartridge::LivesManager()
{
    if (Lives < 1)
    {
        EndGame();
    }
}


void UBullCowCartridge::EndGame() //Ends the game when the player wins or loses.
{
    ClearScreen();

    bGameOver = true;

    if (Lives < 1)
    {
        PrintLine(TEXT("You have no remaining lives. You Lost the game.\n"));
    }
    else
    {
        PrintLine(TEXT("Congratulations! you successfully guessed the forest cow's words!\n"));
    }
    
    PrintLine(FString::Printf(TEXT("The word was: %s\n"), *HiddenWord));
    PrintLine(TEXT("\nPlay again? Press Enter to continue or \ntype a new difficulity to change.")); // guides the player on how to replay
}


FBullCowCount UBullCowCartridge::GetBullCow(const FString& Guess) const 
{
    FBullCowCount Animals;
    
    for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[GuessIndex])
        {
            ++Animals.BullCount;
                continue;
        }
         for (int32 ComparedLetter = 0; ComparedLetter < HiddenWord.Len(); ComparedLetter++)
        {
            if (Guess[GuessIndex] == HiddenWord[ComparedLetter])
            {
                ++Animals.CowCount;
                break;
            }
        }
    }
    return Animals;
}

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

struct FBullCowCount
{
	int32 BullCount = 0;
	int32 CowCount = 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();
	bool DifficulitySet(const FString& Difficulity);
	void EndGame();
	void LivesManager();
	void GuessProcessing(const FString& Guess);
	bool IsIsogram(const FString& Word);
	TArray<FString> GetValidWord(const TArray<FString>& Words);
	FBullCowCount GetBullCow(const FString& Guess) const;

	// Your declarations go below!
private:
	FString HiddenWord;
	int32 Lives;
	bool bGameOver;
	TArray<FString> ValidWord;
	int32 GameDifficulity = 2;
	bool NewGame = false;
};
2 Likes

Great job for going above and beyond

Thank you!

Privacy & Terms