Can't Print Out Correct Bull and Cow Counter

Somewhere along the end of the lessons for the Bull Cow game, I noticed that the output was always printing the number of bulls and cows as 0, nor reaching the win condition state. I tried going over the previous lessons to see where I went wrong, but I’m not able to locate it, despite recopying the code word-for-word. Can someone please take a look at let me know what’s causing issue?

MAIN.cpp

#include <iostream>
#include <string>
#include "FBullCowGame.h"

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

void printIntro();
void playGame();
FText GetGuess();
bool AskPlayAgain();
void PrintGameSummary();

FBullCowGame BCGame;

int main(){
    printIntro();
    bool PlayAgain = false;
    do  {
        playGame();
        PlayAgain = AskPlayAgain();
    }
    while (PlayAgain);
    
    return 0;
}

void printIntro(){
    std::cout << "Welcome to Bulls and Cows!" << std::endl;
    std::cout << "Can you guess the " << BCGame.GetHiddenWordLength() << " letter isogram I am thinking of?\n" << std::endl;
    return;
}

void playGame(){
    BCGame.Reset();
    int32 MaxTries = BCGame.GetMaxTries();
    
    
    while(!BCGame.IsGameWon() && BCGame.GetCurrentTry() <= MaxTries){
        FText Guess = GetGuess();
        
        FBullCowCount BullCowCount = BCGame.SubmitGuess(Guess);
        
        std::cout << "Bulls = " << BullCowCount.Bulls << ". ";
        std::cout << "Cows = " << BullCowCount.Cows << ". " << "\n\n";
    }
    
    PrintGameSummary();
    return;
}

FText GetGuess()
{
    FText Guess = "";
    EGuessStatus Status = EGuessStatus::Invalid_Status;
    do {
        int32 CurrentTry = BCGame.GetCurrentTry();
        std::cout << "Guess " << CurrentTry << " of " << BCGame.GetMaxTries() << ". Enter your guess: ";
        FText Guess = "";
        std::getline(std::cin, Guess);
        
        Status = BCGame.CheckGuess(Guess);
        switch (Status) {
            case EGuessStatus:: Wrong_Length:
                std::cout << "Please enter a " << BCGame.GetHiddenWordLength() << " letter word.\n";
                break;
            case EGuessStatus:: Not_Isogram:
                std::cout << "Please enter a word without repeating letters.\n";
                break;
            case EGuessStatus:: Not_Lowercase:
                std::cout << "Please enter all lowercase letters.\n";
                break;
            default:
                break;
        }
    } while (Status != EGuessStatus:: OK);
    return Guess;
}

bool AskPlayAgain(){
    std::cout << "Would you like to play again? 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!\n";
    }
    else {
        std::cout << "Sorry, you did not win.\n";
    }
    
}

FBullCowGame.cpp

#include <stdio.h>
#include "FBullCowGame.h"
#include <map>
#define TMap std::map

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

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

bool FBullCowGame::IsGameWon() const {
    return false;
}

void FBullCowGame::Reset(){
    constexpr int32 maxTries = 8;
    const FString HIDDEN_WORD = "planet";

    MyMaxTries = maxTries;
    MyHiddenWord = HIDDEN_WORD;
    MyCurrentTry = 1;
    return;
}

int32 FBullCowGame::GetMaxTries() const {
    return MyMaxTries;
}

int32 FBullCowGame::GetCurrentTry() const {
    return MyCurrentTry;
}

int32 FBullCowGame::GetHiddenWordLength() const {
    return MyHiddenWord.length();
}

EGuessStatus FBullCowGame::CheckGuess(FString Guess) const
{
    if (!isIsogram(Guess)){
        return EGuessStatus::Not_Isogram;
    }
    else if (!isLowercase(Guess)){
        return EGuessStatus::Not_Lowercase;
    }
    else if (Guess.length() != GetHiddenWordLength()){
        return EGuessStatus::Wrong_Length;
    }
    else {
        return EGuessStatus::OK;
    }
}

FBullCowCount FBullCowGame::SubmitGuess(FString Guess){
    
    MyCurrentTry++;
    
    FBullCowCount BullCowCount;
    
    int32 WordLength = MyHiddenWord.length();
    
    for (int32 i = 0; i < WordLength; i++){
        for (int32 j = 0; j < WordLength; j++){
            if (Guess[i] == MyHiddenWord[i])
            {
                if (i==j)
                    {
                        BullCowCount.Bulls++;
                    }
                else {
                    BullCowCount.Cows++;
                     }
            }
        }
    }
    
    if (BullCowCount.Bulls == WordLength){ bGameIsWon = true; }
    else { bGameIsWon = false; }
    return BullCowCount;
}

bool FBullCowGame::isIsogram(FString Guess) const{
    if (Guess.length() <=1){ return true;}
    
    TMap<char, bool> LetterSeen;
    for (auto  Letter : Guess){
        Letter = tolower(Letter);
        if (LetterSeen[Letter]){
            return false;
        }
        else{
            LetterSeen[Letter] = true;
        }
    }
    return true;
}

bool FBullCowGame::isLowercase(FString Guess) const{
    for (auto Letter : Guess){
        if (!islower(Letter)){
            return false;
        }
        else{
            return true;
        }
    }
    return true;
}

FBullCowGame.h

#ifndef BullCowGame_FBullCowGame_h
#define BullCowGame_FBullCowGame_h


#endif
#include <string>

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

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

enum EGuessStatus{
    Invalid_Status,
    OK,
    Not_Isogram,
    Wrong_Length,
    Not_Lowercase
};


class FBullCowGame{
public:
    
    FBullCowGame();
    
    int32 GetMaxTries() const;
    int32 GetCurrentTry() const;
    int32 GetHiddenWordLength() const;
    bool IsGameWon() const;
    EGuessStatus CheckGuess(FString) const;
    
    void Reset();
    FBullCowCount SubmitGuess(FString);

private:
    int32 MyCurrentTry;
    int32 MyMaxTries;
    bool isIsogram(FString);
    FString MyHiddenWord;
    bool bGameIsWon;
    bool isIsogram(FString) const;
    bool isLowercase(FString) const;
};
FText GetGuess()
{
	FText Guess = "";
	EGuessStatus Status = EGuessStatus::Invalid_Status;
	do {
		int32 CurrentTry = BCGame.GetCurrentTry();
		std::cout << "Guess " << CurrentTry << " of " << BCGame.GetMaxTries() << ". Enter your guess: ";
        //problem here
		FText Guess = ""; //creating a new Guess variable local to do-while loop
        //more code..
    } while (Status != EGuessStatus::OK); //Guess goes out of scope
	return Guess; //Original Guess variable which == ""

This is exactly how my code was laid out. I noticed in your reply, you left out std::getline(std::cin, Guess);

Was this intentional? Should I be calling for the user’s guess someplace else?

I just snipped it to showcase the problem, did you read the comments?
Also this if (Guess[i] == MyHiddenWord[i]) should be if (Guess[i] == MyHiddenWord[j])

Privacy & Terms