Hilariously wrong

Something went wrong with my code, everyone, big surprise. Right after I did the win lose tutorial, suddenly the game posts the bulls = #, cows = # 6 times on the screen! I don’t even know why. Plus, I’m so close to my code now I can’t even SEE why this is happening either. I’m hoping someone can help me? I’ll upload the entire sln here for people to see, maybe see something that I definitely cannot. :frowning:

Hopefully someone will help me soon, but it is the holidays, so I’ll wait. A good Christmas present would be a solution to this crazy code~! Thanks everyone.

Main.cpp

//Trying to figure out why the bulls and cows are printed off multiple times…

/*This is the console executable, that makes use of the BullCow Class
This acts as the view in a MVC pattern and is responsible for all
user interaction for game logic see the FBullCowGame Class.
*/

#include
#include
#include “FBullCowGame.h”

using int32 = int;
using FText = std::string; //substitutes std::string with FText

void PrintIntro();
void PlayGame();
FText GetValidGuess();
void PrintGameSummary();
bool AskPlayAgain();

FBullCowGame BCGame; //initiate a new game, looks for a constructor and runs any code in the constructor

int main()
{
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskPlayAgain();
}
while (bPlayAgain);

return 0; //exit the application 

}

void PrintIntro()
{
//Starts the game
//constexpr is a constant expression that cannot be changed when used in the console but can be used when coding.
std::cout << “Welome to Bulls and Cows, a fun word game!!” << std::endl;
std::cout << "Can you guess the " << BCGame.GetHiddenWordLength();
std::cout << " letter isogram? " << std::endl;
std::cout << std::endl;
return;
}

void PlayGame()
//loops a constant number of turns wand returns the results of the players guess
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();

//loop the game for guess while the game is NOT won and the
//and there are still guesses left
while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries)
{
	FText Guess = GetValidGuess();

	for (int32 count = 1; count <= MaxTries; count++) 
	{
	


	//Submit valid guess to the game and recieve counts
	FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess); 

		// Print the number of bulls (successes) and cows (misses)
		std::cout << "Bulls = " << BullCowCount.Bulls;
		std::cout << " Cows = " << BullCowCount.Cows;
		std::cout << std::endl;
		std::cout << std::endl;
	}

}
PrintGameSummary();
}

//loop continously until the user gets a valid guess
FText GetValidGuess()
{
FText Guess = " ";
GetGuessStatus Status = GetGuessStatus::Ivalid_Status;
do {
int32 CurrentTry = BCGame.GetCurrentTry();
//get a guess from the player
std::cout << "Try " << CurrentTry << “. What is your guess?” << std::endl;
std::getline(std::cin, Guess);

	Status = BCGame.CheckGuessValidiaty(Guess);
	switch (Status)
	{
	case GetGuessStatus::Wrong_Length:
		std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
		break;
	case GetGuessStatus::Not_Lowercase:
		std::cout << "Must be lower case! Try again " << std::endl;
		break;
	case GetGuessStatus::Not_Isogramm:
		std::cout << "Letters must not repeat! Try again " << std::endl;
		break;
	default:
		//assume the guess is valid after all checks valid
		Guess;
		
		
	}

	std::cout << std::endl;

} while (Status != GetGuessStatus::OK); //keep looping until you get valid input or no errors
return Guess;

}
bool AskPlayAgain()
{
//gives player the option to play again
std::cout << "Would you like to play again with the same hidden word (y/n) " << std::endl;
FText Response = “”;
std::getline(std::cin, Response);

return (Response[0] == 'y') || (Response[0] == 'Y');

}

void PrintGameSummary()
{
if (BCGame.IsGameWon())
{
std::cout << “Well Done, You Win!” << std::endl;
}
else
{
std::cout << “Better Luck Next Time!” << std::endl;
}
}

Header.cpp

#include “FBullCowGame.h”
#include

using int32 = int;

FBullCowGame::FBullCowGame()
{
Reset();
}

int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetHiddenWordLength() const
{
return MyHiddenWord.length();
}
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
bool FBullCowGame::IsGameWon() const { return bGameIsWon; }

void FBullCowGame::Reset()
{
constexpr int32 MAX_TRIES = 5;
MyMaxTries = MAX_TRIES;

const FString HIDDEN_WORD = "gemini";
MyHiddenWord = HIDDEN_WORD;

MyCurrentTry = 1;
bGameIsWon = false;
return;

}

//receives a VALID guess, increments turn, and returns count
FBullCowCount FBullCowGame::SubmitValidGuess(FString Guess)
{
//increment turn number
MyCurrentTry++;

//setup a return variable
FBullCowCount BullCowCount;


int32 WordLength = MyHiddenWord.length();
//loop through all letters in the word
for (int32 MHWChar = 0; MHWChar < WordLength; MHWChar++) 
	{
	//compare letters against the hidden word
	for (int32 GChar = 0; GChar < WordLength; GChar++)
	{
		//if they match and if they are in the same place
		if (Guess[GChar] == MyHiddenWord[MHWChar]) 
		{
			if (MHWChar == GChar) 
			{
				//incriment bulls
				BullCowCount.Bulls++;
			}
				//otherwise incriment cows
			else
			{
				BullCowCount.Cows++;
			}
		}
	}
}
if (BullCowCount.Bulls == WordLength) {
	bGameIsWon = true;
}
else {
	bGameIsWon = false;
}
return BullCowCount;

}

GetGuessStatus FBullCowGame::CheckGuessValidiaty(FString Guess)
{
// Return error
if (false)//not isogram
{
return GetGuessStatus::Not_Isogramm;
}
else if (false)//not lowercase
{
return GetGuessStatus::Not_Lowercase;
}
else if (Guess.length() != GetHiddenWordLength())//wrong length
{
return GetGuessStatus::Wrong_Length;
}
else//can’t find anything wrong with it
{
return GetGuessStatus::OK;
}

}

FBullCowGame.h

#pragma once
#include

using int32 = int;
using FString = std::string;

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

enum class GetGuessStatus
{
Ivalid_Status,
OK,
Not_Isogramm,
Wrong_Length,
Not_Lowercase,
};

//never use using namespace in a header file because you will not be able to keep track of anything
class FBullCowGame
//classes do not need to be the same name as the file but for simplicity’s sake…
{
public:
FBullCowGame(); //constructor

//this is just the skeletons, the blueprints up. 	
//public methods	
int32 GetMaxTries() const;
int32 GetCurrentTry() const;
int32 GetHiddenWordLength() const;

bool IsGameWon() const;
GetGuessStatus CheckGuessValidiaty(FString); // TODO return value later

void Reset();
//variables are to remain private, because the users of your program should not have access
//Counts bulls and cows and increases try number assuming valid guess
FBullCowCount SubmitValidGuess(FString);

private:
//See constructor for initalization
int32 MyCurrentTry;
int32 MyMaxTries;
FString MyHiddenWord;
bool bGameIsWon;

};

You have FText Guess = GetValidGuess(); outside of your loop so it will only get a guess once and then check bulls and cows on that one guess multiple times. Also your hidden word isn’t an isogram as it has two i’s

while (!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries)
{
    FText Guess = GetValidGuess(); //should be inside the loop
    for (int32 count = 1; count <= MaxTries; count++)
    {
        //Submit valid guess to the game and recieve counts
	FBullCowCount BullCowCount = BCGame.SubmitValidGuess(Guess);
        // Print the number of bulls (successes) and cows (misses)
	std::cout << "Bulls = " << BullCowCount.Bulls;
	std::cout << " Cows = " << BullCowCount.Cows;
	std::cout << std::endl;
	std::cout << std::endl;
    }
}
PrintGameSummary();

Aha! It workes now, thank you for noticing that. I couldn’t figure it out for the life of me. This issue is resolved thanks to you, my friend.

Privacy & Terms