Lecture 36 Confusion

Just finished lecture 36 and i had no idea what to do for the challenge, i spent about half an hour staring at the computer screen wondering what to do. After seeing the solution would anyone be able to describe how the i’s and j’s work in the code as when i read it i lose track of what is going on. Thanks.

Hey !
Since I spent a good amount of time myself trying to understand the logic of it all; I will try to explain my conclusion.
Note that I am in no way an expert, so I cant be 100% sure I am right.

I will replace the “i” and “j” with other names, which makes this easier to understand. They are essentialy variables.

So the first part :
> for (int32 MHWchar = 0; MHWchar < HiddenWordLenght; MHWchar++)
is a loop which will go through all character for “MyHiddenWord”. So here “i” (or MHWchar) is simply referencing a position of a character.

The second :
> for (int32 Gchar = 0; Gchar < HiddenWordLenght; Gchar++)
is another loop for all character in the “Guess”. Here “j” (or Gchar) is the position of a character in the Guess.

Important note for the loop; The first character of the HiddenWord will loop through all character position in the Guess, before the first loop increment to the second character of the HiddenWord and then loop through all character of the guess, and so on…

Then, we jump to our “if” conditions to check for bulls and/or cows.
> if (Guess[Gchar] == HiddenWord[MHWchar])

This statement is where I was personally lost so I will try to be thorough here :
I mentionned Gchar/MHWchar was the position of a character so ; Guess[Gchar] returns the actual character at that position for “Guess”
Thus, during our loops (of characters in the Guess comparing to the characters in the HiddenWord):
IF a character(letter) in the Guess is EQUAL to a character(letter) in the HiddenWord…

Now we cant jump to any conclusion as we dont know yet if this is a BULL or a COW per our game rules and design.
(Bull = the character will be in the same POSITION, Cow = it will not)

Thus the last If / Else statement :

if (Gchar == MHWchar) {
BullsCowsCount.Bulls++;
} else {
BullsCowsCount.Cows++;
}

Since our first IF has caught an EQUAL character, we determine if it is a bull by comparing if the position is the same. Otherwise (else), by our rules, it must be an Cow.
So when I said Gchar & MHWchar was a position of a character; this is the simple statement we must verify here to increase a Bull or else; a Cow!

There you go !

1 Like

Hey everyone! Just finished section 36 of the course and am getting and error in my output. Im currently using XCode on Mac, and whenever I run my program both the bulls and cows out put as 0, and 0 still. I have double checked my code and it looks as if I’ve done everything that was required. Im hoping maybe a fresh set of eyes will help me see where I went wrong.

Here is my Main.CCP:

#include
#include
#include “FBullCowGame.hpp”

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

void printIntro();
void playGame();
FText GetGuess();
bool AskToPlayAgain();
FBullCowGame BCGame; //instantiate a new game

int main()
{
bool bPlayAgain = false;
do
{
printIntro();
playGame();
//TODO add a Game Summary
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);

return 0;

}

void printIntro()
{
constexpr int32 WORLD_LENGTH = 9;
std::cout << “Welcome to the Bulls and Cows\n” << std::endl;
std::cout << "Can you guess the “<<WORLD_LENGTH;
std::cout << " letter isogram I’m thinking of?\n”;
std::cout << std::endl;
return;
}

void playGame()
{
BCGame.Reset();
int32 MaxTries = BCGame.GetMaxTries();

std::cout << "Max Tries: "<< MaxTries <<std::endl;
for(int32 count = 1; count <= MaxTries; count++)
{
    FText Guess = "";
    
    FBullCowCount BullCowCount =  BCGame.SubmitGuess(Guess); // submits guess to te game 
    
    Guess = GetGuess();
    std::cout << "Bulls = " << BullCowCount.Bulls;
    std::cout << " Cows = " << BullCowCount.Cows << std::endl; //repeat guess back to player
    std::cout << std::endl;
}
//TODO summarize game

}

FText GetGuess()
{
int32 CurrentTry = BCGame.GetCurrentTry();
std::cout << "Try "<< CurrentTry << “, Enter your guess:”; //get a guess from the player
FText Guess = “”;
std::getline(std::cin,Guess);

return Guess;

}

bool AskToPlayAgain()
{
std::cout << “Do you want to play again? (y/n)”;
FText Response = “”;
getline (std::cin, Response);

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

}

Here is my FBullCowGame.hpp:

#ifndef FBullCowGame_hpp
#define FBullCowGame_hpp

#pragma once
#include <stdio.h>
#include

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

//all values initialized to zero
struct FBullCowCount
{
int32 Bulls = 0;
int32 Cows = 0;
};

class FBullCowGame
{
public:
FBullCowGame();//constructor

int32 GetMaxTries() const;
int32 GetCurrentTry() const;
bool IsGameWon() const;

void Reset(); //TODO make a more rich return value
bool CheckGuessValidity(FString); //TODO make a more rich return value

FBullCowCount SubmitGuess(FString);//counts bulls and cows and increases try#

private:

//see contructor for intialisation
int32 MyCurrentTry;
int32 MyMaxTries;
bool IsIsogram(FString);
FString MyHiddenWord;

};

#endif

And Finally, this is my FBullCowGame.ccp:

#include “FBullCowGame.hpp”
#include
using int32 = int;

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

int32 FBullCowGame::GetCurrentTry() const { return MyCurrentTry; }
int32 FBullCowGame::GetMaxTries() const { return MyMaxTries; }
bool FBullCowGame::IsGameWon() const { return false; }

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

const FString HIDDEN_WORD = "ant";
MyHiddenWord = HIDDEN_WORD;

MyCurrentTry = 0;
return;

}

bool FBullCowGame::CheckGuessValidity(FString)
{

return false;

}

FBullCowCount FBullCowGame:: SubmitGuess(FString Guess) //recieves a valid guess increments turn, and returns count

{

MyCurrentTry++;//increments the turn number


FBullCowCount BullCowCount;//setup a return variable

int32 HiddenWordLength = MyHiddenWord.length(); // loop through all letters in guess

for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++)
{
    for(int32 GChar = 0; GChar < HiddenWordLength; GChar++) // comapre letters against hidden word
    {
        if (Guess[MHWChar] == MyHiddenWord[MHWChar]) // if they match then
        {
            if (MHWChar == GChar) // if theyre in the same place
            {
                BullCowCount.Bulls++; // incriment bulls
            }
            else
            {
                BullCowCount.Cows++; // must be a cow
            }
            
        }
    }
}

return BullCowCount;

}

Hi @Emile_Pedneault. Just to clarify are you getting an actual error in your code or is the problem just that bulls and cows isn’t incrementing? I looked at your code and it looks good as far as bulls and cows go but do note that Ben in the video purposely made it increment wrong so we can fix it in the next video. I also am coding an windows not mac so if this was on windows it should work for the most part but unsure if macs has something else you might be missing. Next time can you try using: github to upload your code to make reading it easier and so we can easily copy and paste your code to try it out. Hope you figured out your issue by now. Good luck.