I just wanted to make people aware of a bug in this code.
for (int32 GuessIndex = 0; GuessIndex < Guess.Len(); GuessIndex++){
if (Guess[GuessIndex] == HiddenWord[GuessIndex]) { // BUG: index out of bounds exception
++Count.Bulls;
continue;
}
...
The problem is that if Guess is longer than HiddenWord, and we are iterating the for loop through all of the characters in Guess, we could potentially attempt to access more characters than are available in HiddenWord.
For Example:
Say HiddenWord is apple and Guess is apples. Iterating through the for loop for Guess and we get to the âsâ in apples which would be Guess[5]. HiddenWord[5] doesnât exist and as soon as we try to access it as in the above if statement, we get the out of bounds exception.
A simple fix could be to include an additional check to make sure that GuessIndex isnât larger than HiddenWord.Len() before trying to use it as an index to the character array of HiddenWord.
Or to also include exception handling as I have done in my own code.