TIL: Learn to Code in C++ by Developing Your First Game (Section 2, Part 2)

Alright, I’ve been so focused on Japanese recently, it’s easy to forget that I have other things I want to do too! Yesterday is the first day in close to a month that I decided to just go with the flow, not do anything planned after work, and ended up watching the first two episodes of Better Call Saul. It starts off great! I’d love to binge watch the rest of it, but all play and no work makes for an empty blog.

It’s been a week since I last followed along with the coursework over on Udemy, so lets get back into gear:

Section: 2 – Bulls & Cows Word Game – Your First C++

Lecture 20: Clarity

The goal with this tutorial is simply to tidy up the work that we’ve been doing.

It’s important to load the entire Solution, not just your recent file. Loading *just* main.cpp, will result in things not working. You won’t be able to compile, and the Extract Function described below won’t do as much for you. Inside main() we not only have PrintIntro() but we’re also calling a for loop and providing lots of information to it. It’s not clear, coherent, or consistent to do it this way. It’s better to create a function that will call the loop. If you select your code, you can press “Ctrl + .” to perform quick action -> Extract Function. In my case the option wasn’t there at first, so ensure that you allow the menu option by going to Tools->Options

ExtractMethod

Now in the case of this tutorial, Ben for some reason decides not to use the main.h. Aside from that, all we do is take the main game loop, package it into it’s own function, and then also simplify the GetGuessAndPrint() into a GetGuess() and then print the result inside the loop.

Lecture 21: Booleans

Booleans are simply True/False statements. We can determine True/False with basic comparisons.

== compares if two items are equal
&& does AND statements
|| does OR statements
[n] can be used to access a string, starting at n=0
‘ ‘ for characters, and ” ” for strings.

When right clicking and creating a new definition: if you aren’t happy with the bracing method the Visual Studio does by default, you can go to tools, options, and search for BRACE and then describe the “Position of open braces for functions”

Lecture 22: Do While

Do while loops allow us to do something as long as a variable has a given value. We can do(play our game) while(we haven’t said we want to stop playing). The do will get executed a minimum of one time since we need to do something before we can state whether or not we want to stop.

http://www.cplusplus.com/doc/tutorial/control
https://msdn.microsoft.com/en-us-library/b0kk5few.aspx

In Ben’s example he creates a bool variable, then gives it the value of asktoplayagain(), then uses that variable to determine the while statement. I feel it’s simpler to just do the following:

do {
    PlayGame();
}
while (AskToPlayAgain());

For some reason Ben chose to remove the main.h after creating it earlier. Here he’s creating a header again…

One thing to be aware of, is that in the Unreal Coding Standards, it states that “Most other classes are prefixed by F, though some subsystems use other letters”. In this case, we prefix the header with an f so that we grow in familiarity with UE.

The goal with C++ is not to leave variables exposed. When we create a class, we will have public and private data. Public is where we will use functions to acquire data, where as Private is where we store data.

Never use “using Namespace” in a header file.

Lecture 24: Including Our Header

– We remove “using namespace std;” all together from main.cpp in this lesson.
– we also create a new cpp file where we have all our methods.

Once we remove the namespace, we again need to explicitly tell things such as strings to recognize std by stating “std::string”

using namespace inside a header is very bad, as it makes it extremely difficult to remember where we have access to things and where we don’t. We will avoid namespace entirely to avoid such headaches.

This unfortunately means that we will be more “strict” and more “correct” by replacing:

string with std::string
cout with std::cout
cin with std::cin
getline with std::getline

50%