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');
}