Added difficulty progression, hidden word length change with correct guess

In my implementation the array of valid words is not limited in the number of characters only that the entries are isograms. When each hidden word is chosen the game tests each potential word randomly from the valid array until it gets to one that is 2 plus the player score in length. Each correct guess increments the players score by 1.

At 10 letters long the game tells the player that they have won the game and does not progress further.

If anyone is interested in the detail of how any of this was done respond to the message and I will post my code.

1 Like

I would love to see your code :grinning_face_with_smiling_eyes:

My code (the text about the dragon is because I imported a dragon mesh into my scene):

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


void UBullCowCartridge::BeginPlay() // When the game starts
{
	Super::BeginPlay();
	const FString WordListPath = FPaths::ProjectContentDir() / TEXT("WordLists/HiddenWordList.txt");
	FFileHelper::LoadFileToStringArrayWithPredicate(Words, *WordListPath, [](const FString& Word) {return TestIfIsogram(Word); });

	Score = 0;

	SetupGame();
}

void UBullCowCartridge::OnInput(const FString& Input) // When the player hits enter
{
	ClearScreen();
	PlayerTestWord = Input;

	ProcessGuess();

}

void UBullCowCartridge::EndGame()
{
	ClearScreen();
	SetupGame();
}

void UBullCowCartridge::SetupGame()
{
	if (Score == 9)
	{
		PrintLine(TEXT("You have beaten the Bull Cow game."));
		PrintLine(TEXT("Take pride in your greatness!!!."));

	}
	else
	{
		ChooseHiddenWord();

		Lives = HiddenWord.Len();
		bGameOver = false;
		if (Score == 0)
		{
			PrintLine(TEXT("Welcome to this strange bull and cow game,now also with dragon."));
		}
		else
		{
			int32 Round = Score + 1;
			PrintLine(TEXT("Welcome to round %i of the bull and cow game."), Round);
		}

		PrintLine(TEXT("Enter your guess for the " + FString::FromInt(HiddenWord.Len()) + " letter hidden \nword."));
		PrintLine(TEXT("You will get " + FString::FromInt(HiddenWord.Len()) + " guesses."));
	}



}

FBullCowCount UBullCowCartridge::CountBullsCows(const FString& Guess) const
{
	FBullCowCount Counter;

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

const bool UBullCowCartridge::IsGuessCorrect()
{

	if (HiddenWord == PlayerTestWord)
	{
		return true;
	}
	else
	{
		return false;
	}
}

const bool UBullCowCartridge::CompareWordLength()
{

	if (HiddenWord.Len() == PlayerTestWord.Len())
	{
		return true;
	}
	else
	{
		PrintLine(TEXT("Please enter a guess with " + FString::FromInt(HiddenWord.Len()) + " letters."));
		PrintLine(TEXT("You have %i guesses remaining."), Lives);
		return false;
	}
}

bool UBullCowCartridge::TestIfIsogram(const FString& TestWord)
{
	//1. get first letter
	//2. check all letters starting from position 2 for that letter, if found print error exit function and return false
	//3. if no letters are equal to the step one but increment all position references
	//4. if the end char is selected as the letter to find return true

	for (int32 i = 0; i < TestWord.Len(); i++)
	{
		for (int32 IndexR = i + 1; IndexR < TestWord.Len(); IndexR++)
		{
			if (TestWord[i] == TestWord[IndexR])
			{
				return false;
			}
		}
	}

	return true;
}

void UBullCowCartridge::ProcessGuess()
{
	//check length of player guess compared to hiddenword
	//check if isogram
	//DetermineBullsAndCows(); //check for bull and cows
	//remove life on fail, show remaining lives
	//are lives > 0
	//if life less then 0 ask if want to play again, display hidden word
	//if lives greater take in another guess

	FBullCowCount Count;

	if (CompareWordLength() && !bGameOver)
	{
		if (IsGuessCorrect())
		{
			PrintLine(TEXT("You Win the game."));
			bGameOver = true;
			++Score;
			PrintLine(TEXT("Press enter to play again."));
		}
		else if (!TestIfIsogram(PlayerTestWord))
		{
			PrintLine(TEXT("The word you entered was not an isogram."));
			PrintLine(TEXT("Please enter a guess with only \nunique letters."));
			PrintLine(TEXT("You have %i guesses remaining."), Lives);

		}
		else
		{
			if (Lives > 1)
			{
				FBullCowCount Count = CountBullsCows(PlayerTestWord);
				PrintLine(TEXT("%i Bulls, right letters in the right place."), Count.Bulls);
				PrintLine(TEXT("%i Cows, right letters in the wrong place."), Count.Cows);

				PrintLine(TEXT("You have used 1 guess, you have %i guesses remaining."), --Lives);

			}
			else
			{
				PrintLine(TEXT("You have lost the game."));
				PrintLine(TEXT("The HiddenWord was \"%s\"."), *HiddenWord);
				bGameOver = true;
				Score = 0;
				PrintLine(TEXT("Press enter to play again."));
			}
		}
	}
	else if (bGameOver)
	{
		EndGame();
	}
}

void UBullCowCartridge::ChooseHiddenWord()
{

	do
	{
		HiddenWord = Words[FMath::RandRange(0, Words.Num() - 1)];
		PrintLine(TEXT("DEBUG: Hidden word tested: %s "), *HiddenWord);
	} while (HiddenWord.Len() != Score + 2);

	//ClearScreen();
	PrintLine(TEXT("DEBUG: The Hidden Word is: %s"), *HiddenWord); //DEBUG LINE

	int32 WordNum = 0;
	for (FString CWord : Words)
	{
		++WordNum;
	}
	PrintLine(TEXT("DEBUG: There are %i HiddenWords"), WordNum); //DEBUG LINE
}
1 Like

Thank you for sharing this๐Ÿ™๐Ÿผ

Privacy & Terms