Section 2 Lecture 32 Using if Statements - here's what I tried

urc_s02_using_if_statements_in_c

#1

Took quite a bit of time to figure out what to do.
I had to Google how to access the FString Guess that was submitted because it didn’t have a name !

It also crashed when the guessWord.length() was less than the HiddenWordLength since it ran out of letters to access so I created a GuessWordLength analagous to HiddenWordLength to limit the checking.
The checking uses the j count as that loop is associated with GuessWordLength.

However I don’t know why the Cows and Bulls count reset after each loop, I think it should only go up until the game resets.


#2

If it’s crashing due to submitting a guess with fewer letters than your hidden word, it’s because you haven’t properly implemented the guess validation that only allows you to enter a guess with the same number of letters.

It crashes because, when using HiddenWordLength for both words, and entering a shorter guess, the final iteration through the for statements is trying to access an array value that doesn’t work, which errors it out.
Meaning if you were using a 5 letter hidden word, and the user entered "fork"
guessWord[0] = f
guessWord[1] = o
guessWord[2] = r
guessWord[3] = k
guessWord[4] = NOTHING HERE

When it does that, it has no way to handle that error, and crashes.

Also, Bulls and Cows don’t reset each loop. They “increment” each loop. It is adding cows and bulls to the same BullCowCount instance for the entire loop.

It does reset every guess, but that’s because it’s suppose to–that’s how players deduce which letters are actually bulls, which are cows and eventually learn to guess your word.


#3

Thanks for your detailed reply !

I think your answer very helpfully clearly clarifies why it crashed if the guessWord is shorter than HiddenWordLength, which for me was very satisfying to work out how to fix by creating and using a GuessWordLength. I should have deleted the comment after it !

Ah, now that you mention that it resets after each guess I can see it. It makes sense finally.
We create a new FBullCowCount struct instance each loop through because the information is discarded after each guess in PlayGame(). And also the reason we didn’t need to hold that information in any member variables.

So far this course has been interesting but probably the strangest part is clicking between 3 files and wondering why some parts of the game are in main.cpp or FBullCowGame.cpp and how to remember it all (terms, placement, syntax etc) without rereferencing all the time !

Better go introduce myself

Thank you again Bryant_McCracken


#4

We’ll when it’s your own project rather than someone else’s lesson plan, you generally have a bit more thought and panning beforehand and know where things are. Plus you can arrange them how they make sense to you (within reason).

The other benefit is encapsulation and abstraction. Day you’ve got enduring working right and you want to add something that chooses futon multiple words to be the hidden word… Because of the encapsulation, you can freely work on this file or that, knowing that enduring else controlled outside of that file is 100% safe from any screw ups you might make with your current code changes.

It might be a bit confusing at first, but it’s a blessing with time lol


#5

Just finished lesson 39 (Handling Game Win Condition) and the encapsulation (?) idea has been clearly used again where
bool bIsGameWon; (declared in FBullCowGame.h)
is kept well away from the main.cpp using a getter to access and act on it, neat !

You’re right this abstraction thing is going to be super useful as the code sprawls out so so quickly I find I keep having to reference back and forth a lot. Just as Ben mentioned it’s tough to keep more than a bundle things in mind at the same time.

I’m beginning to get the idea that naming, checking, tidying, cleaning, refactoring, commenting, renaming, rechecking, etc
is the essence of actual coding heh

Thanks again


#6

Thanks for sharing, I’ve now tagged with our new lecture specific tag. You can view all posts for a particular lecture now.