If (Guess[GChar] == MyHiddenWord[MyHWChar]) How does this work?

I know it has something to do with arrays, and evaluating them but could someone explain logically what is going on here: (bare with me because I’m thinking this through as I type this) I know it works but I don’t really understand why.

if (Guess[GChar] == MyHiddenWord[MyHWChar])

Are we accessing the array element of index"GChar", which increments from 0 with each loop) in the Guess array and comparing it, but how? guess isn’t a declared array, it’s a string.

My loop:

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

//setup a return variable
FBullCowCount BullCowCount;

//loop through all letters in guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MyHWChar = 0; MyHWChar < HiddenWordLength; MyHWChar++)
{
//compare letters against hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++)
{
//if they match
if (Guess[GChar] == MyHiddenWord[MyHWChar])
{
//if theyre in the same place
if (MyHWChar == GChar)
{
BullCowCount.Bulls++;//increment bulls
}
else
{
BullCowCount.Cows++;//increment cows
}
}
}
}
return BullCowCount;
}

a string is essentially an array of chars and std::string operator[] returns a reference to the char at the specified location

http://en.cppreference.com/w/cpp/string/basic_string/operator_at

1 Like

Ahh thanks. That whole block of code make a lot more sense now.

Privacy & Terms