Bulls and cows both staying at 0

I am stuck trying to make my bull/cow counting loop work. Earlier I was having a problem with an error coming up saying debug assertion failed: string subscript out of range. I managed to fix that problem by implementing a fix offered on a different discussion that switched out gChar < hiddenWordLength for gChar < guess.length() in the second for loop.
I now have the problem of both bulls and cows staying at zero when I enter a word. Below is my fBullCowGame.cpp code, as well as my main.cpp. Any help would be appreciated. Thanks!

#include "stdafx.h"
#include "fBullCowGame.h"

fBullCowGame::fBullCowGame(){ reset(); }
int32 fBullCowGame::getMaxTries() const { return myMaxTries; }
int32 fBullCowGame::getCurrentTry() const { return myCurrentTry; }
int32 fBullCowGame::getHiddenWordLength() const{ return myHiddenWord.length();}

void fBullCowGame::reset()
{
	constexpr int MAX_TRIES = 8;
	const fString HIDDEN_WORD = "ant";
	myMaxTries = MAX_TRIES;
	myCurrentTry = 1;
	myHiddenWord = HIDDEN_WORD;
	return;
}

bool fBullCowGame::isGameWon() const
{
	return false;
}

bool fBullCowGame::checkGuessValidity(fString) const
{
	return false;
}

//recieves valid guess, increments turn, returns count
fBullCowCount fBullCowGame::submitGuess(fString guess)
{
	myCurrentTry++;
	fBullCowCount bullCowCount;

	int32 hiddenWordLength = myHiddenWord.length();
	for (int32 mHWchar = 0; mHWchar < hiddenWordLength; mHWchar++) 
	{
		for (int32 gChar = 0; gChar < guess.length(); gChar++) 
		{
			if (guess[gChar] == myHiddenWord[mHWchar]) 
			{
				if (mHWchar == gChar) 
				{
					bullCowCount.bulls++;
				}
				else 
				{
					bullCowCount.cows++;
				}
			}
		}
	}
	return bullCowCount;
}

// BullCowGame.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#include <iostream>
#include <string>
#include "fBullCowGame.h"

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

void playGame();
void printIntro(); 
fText getGuess();
fText returnGuess();
bool playAgain();

fBullCowGame BCGame; //instantiate a new game

//the entry point for our application
int main() 
{
	do {
		printIntro();
		playGame();
	} while (playAgain() == 1);
	return 0;
}
 

void printIntro() //Introduce the game
{
	
	std::cout << "Welcome to Bulls and Cows, a fun word game.\n";
	std::cout << "Can you guess the " << BCGame.getHiddenWordLength();
	std::cout << " letter isogram I'm thinking of?\n"; 
	return;
}

void playGame() //plays the game
{
	BCGame.reset();
	int32 maxTries = BCGame.getMaxTries();
	std::cout << "Max tries: " << maxTries << std::endl; //displays player's max amount of trys
	
	//loop for the number of turns asking for guesses
	//TODO change to while loop once tries are being validated
	for (int32 count = 1; count <= maxTries; count++) {
		fText guess = getGuess(); //TODO make check for valid guess
		
		fBullCowCount bullCowCount = BCGame.submitGuess(guess);
		std::cout << "Bulls = " << bullCowCount.bulls;
		std::cout << ". Cows = " << bullCowCount.cows << std::endl;
		returnGuess();
		std::cout << std::endl;
	}
}

//TODO summarize game

fText getGuess() //gets guess from the player
{
	int32 myCurrentTry = BCGame.getCurrentTry();
	std::cout << "Try " << myCurrentTry << ". Enter your guess. "; /*displays player's current try and
																	askes for player's guess*/
	return fText();
}
 
fText returnGuess() //repeats the player's guess on screen
{
	fText guess = "";
	std::getline(std::cin, guess); //allows std::cin to accept a line instead of a single word
	std::cout << "Your guess was: " << guess << std::endl;
	return fText();
}

bool playAgain() //askes player if they want to play again
{
	std::cout << "Do you want to play again(y,n)? "; 
	fText response = "";
	std::getline(std::cin, response);
	return (response[0] == 'y' || response[0] == 'Y');
}

The problem is that you have your conditions mixed up.
you want to use the following. ( If I’m reading it correctly this should properly increment and decrement your counts.

		if (guess[gChar] == myHiddenWord[mHWchar]) 
		{
			bullCowCount.bulls++;
		}
		else 
		{
			bullCowCount.cows++;
		}

The conditions are fine, cows are for correct letter but incorrect position.

@Scott_Young your problem is these two functions

fText getGuess() //gets guess from the player
{
	int32 myCurrentTry = BCGame.getCurrentTry();
	std::cout << "Try " << myCurrentTry << ". Enter your guess. "; /*displays player's current try and
																   askes for player's guess*/
	return fText();
}

fText returnGuess() //repeats the player's guess on screen
{
	fText guess = "";
	std::getline(std::cin, guess); //allows std::cin to accept a line instead of a single word
	std::cout << "Your guess was: " << guess << std::endl;
	return fText();
}

In getGuess you aren’t getting a guess which would mean in these two lines you are passing an empty string into submitGuess

fText guess = getGuess(); //will return an empty string
fBullCowCount bullCowCount = BCGame.submitGuess(guess);

Also returnGuess and it’s function call can be removed so you would have

fText getGuess() //gets guess from the player
{
	int32 myCurrentTry = BCGame.getCurrentTry();
	std::cout << "Try " << myCurrentTry << ". Enter your guess. "; /*displays player's current try and
																   askes for player's guess*/

	fText guess = "";
	std::getline(std::cin, guess); //allows std::cin to accept a line instead of a single word
	return guess;
}

The conditionals are still messed up here unless I’m missing something…
the
If (guess[gChar] == myHiddenWord[mHWchar])
will only match if the letters are the same and then it will do the
if (mHWchar == gChar)
which is comparing 2 integer values ( which are supposed to be where the letter comparison is)

Which is exactly what you want, you want to test if the letters are the same and then check if the positions are the same.

Guess = "tar"
HiddenWord = “ant”

Should be 2 cows not 3.

Yes my bad I went back and reviewed that section. the logic in the submit guess is correct…
Guess I shouldn’t read code really late at night. :slight_smile:

dan IS correct. your getting your input word in your return guess logic so getGuess is submitting a null word to submit guess and your logic isn’t even running.

Awesome! that fixed the problem @DanM. Thanks guys

Privacy & Terms