The Truth about Booleans

Last week, we went over program logic a bit during the live show, but the limited time we had wasn’t going to be much help clearing up the issue, so here’s a proper follow-up. (We also went over classes and objects before that, so if you’re interested, check it out)

This introduction to logic is divided into levels. Go as far as you can, and when you get stuck, try to gain a little XP before going further.


LEVEL 1

What is logic?

First, the logic used in game development isn’t complicated, so don’t worry – most of it behaves like you’d expect in the real world using natural English.

A sentence like “If my dog is hungry, then I’ll feed it” is an example of a real world logic statement. You have a condition: “If my dog is hungry” and you have a result: “I’ll feed it” that only applies when that condition is true.

Unlike humans, computers live in a very black and white world. They’ve very good at playing logic games with “true” and “false.” Digital circuits with electricity are based on being turned ON and OFF, for example, so it’s easy enough to tell a computer something like off is false and on is true, and it’s easy for the computer to evaluate those kind of conditions. If the hungry state of your dog is something you could reliably convey to your computer, you could depend on it to feed Rover (probably more dependable than your kids – humans live in a very gray world).


LEVEL 2

How does the computer know what to do?

As our computer can’t tell when Rover is hungry, let’s switch to an analogy that uses our Eliminator 9000 shotgun from last week’s introduction to classes and objects. Like most guns, if it runs out of bullets, it needs reloading. For the computer, you would tell it “If bullets are 0 then perform reload.”

if bullets are 0 then reload

When the computer sees “bullets are 0”, it grabs the information about the number of bullets in the gun and compares it for us, so, if we have 2 bullets, our previous example goes from:

if bullets are 0 then reload

to:

if 2 is 0 then reload

Well, 2 is obviously not 0, so it’s not going to reload. To represent this, there’s another step the computer takes behind the scenes. It translates “2 is 0” into “true” or “false” before deciding what to do. Our computer is obsessed with blacks and whites – it wants to try to get everything it can boiled down to them. So, in the end, our example goes from:

if 2 is 0 then reload

to:

if false then reload

Well, it’s false – can’t get any more boiled down than that, so there’s no reload.


LEVEL 3

What if I want to check more than one condition?

You have two choices. You can either ask one question, then the other:

If my dog is hungry, then if I have dog food, then feed the dog

or you can combine your questions:

If my dog is hungry and I have dog food, feed the dog

Notice the “and”. That means both conditions have to be true, just like it does in human language. But what if only one of them has to be true? Think about it in terms of human language first.

If my dog is hungry or I’m leaving for work then feed the dog

In that case, the dog being hungry “or” your leaving for work are enough to get Rover his food.


LEVEL 4

So I could keep going, right?

Of course, you can come up with super-long conditions, like:

If my dog is hungry and I have dog food, or I’m leaving for work and I have dog food, or I’m feeling generous and I have dog food then feed the dog

That’s a bit convoluted, and no human being would say that, instead they might say:

If my dog is hungry or I’m leaving for work or I’m feeling generous, and I have dog food, then feed the dog

Being human, you know what to do with either sentence, but a computer might misunderstand you, unless you tell it what you mean.


LEVEL 5

So, how do I tell my computer what I mean?

You have to group the questions you want together. In most programming and scripting languages, this involves parentheses:

If ((my dog is hungry or I’m leaving for work or I’m feeling generous) and I have dog food) then feed the dog

Now the computer understands us. It’ll figure out the stuff in the innermost parentheses first and work outwards.
(And the dog gets fed as long as we have dog food)


LEVEL 6

What if I want to do something else if the condition fails? (What if I don’t have dog food!?)

Else. That’s all you need. (Just like human language)

If I have dog food then feed the dog else pet the dog.

Going back to our Eliminator 9000, we can say:

if bullets are 0 then reload else shoot

So in our previous example with 2 bullets, this becomes:

if 2 is 0 then reload else shoot

which becomes:

if false then reload else shoot

and that becomes:

shoot

Computers take things step-by-step. They’re very stubborn that way. But it’s also why they don’t make the kinds of mistakes humans make (unless a human tells them to make a mistake like Eric does on the show so very, very often).


LEVEL 7

Are those my only choices? What if I want to check more things?

You can keep adding conditions.

If bullets are 0 then reload else if bullets are 1 then grunt else if bullets are 2 then yell…


LEVEL 8

That looks icky, isn’t there a cleaner way?

Well, thankfully, you can use something called a switch statement to prevent the horrible nesting of conditions that makes code so ugly. All this does is test one part of a condition against multiple possible cases. In plain language, it says: With no bullets, I’ll reload, with one, I’ll grunt, with two, I’ll yell.

You can also add a default behavior for when none of the conditions are met. This is like that one “else” at the end of the long, long if…then…else chain. In plain languages, it’s like saying “otherwise”:

With no bullets, I’ll reload, with one, I’ll grunt, with two, I’ll yell, otherwise I’ll shoot.

In programming and scripting languages, it usually looks something like this:

switch (bullets)
case 0:
reload
break;
case 1:
grunt
break;
case 2:
yell
break;
default:
shoot
break;

Don’t worry about those “break”s, they just tell the computer it’s done worrying about the stuff inside the switch. If you forget, the computer will keep on going.

This isn’t always a mistake, you could take advantage of that to do the same thing for multiple conditions.

case 0:
reload
break;
case 1:
case 2:
grunt
yell
break;
default:
shoot

In this case, when we only have one or two bullets, we’ll grunt and yell.
We could also do this:

case 0:
reload
break;
case 1:
grunt
case 2:
yell
break;
default:
shoot

If we have one bullet, we’ll grunt and yell. If we have two bullets, we’ll just yell. Except in special cases like this, a missing “break” is usually a typo, so, if strange things are happening near a switch, check to make sure you have those “break”s in the right places.


LEVEL 9

Great, I know how to feed my dog, but what if it’s not hungry?

Not – that’s all you need to turn the logic upside down:

If my dog is not hungry then pet my dog

For our bullets example, you could say something like:

if bullets are not 0 then shoot else reload


LEVEL 10

Great, let me at those conditions, I’ll NOT the lot of them!

Hold on, hold on. There’s one more thing you’ll have to be careful about before you start going not-crazy. This is going to go deep, but you’ll need to understand this before you unleash the power of the not statement because sometimes the results aren’t what you expect (until you think about them). Going back to the dog, what happens if we just slap a giant “not” in front of this statement:

If ((my dog is hungry or I’m leaving for work or I’m feeling generous) and I have dog food) then feed the dog

as in:

If not ((my dog is hungry or I’m leaving for work or I’m feeling generous) and I have dog food) then pet my dog

Surely that’s the opposite of when I feed the dog, right?

When you’re not sure, try to remember that, for a computer, not just flips true to false or false to true, and the computer evaluates things from inside to outside. So, let’s say Rover isn’t hungry, you’re not leaving for work and you’re not feeling generous, even if you have some dog food:

If not ((my dog is hungry or I’m leaving for work or I’m feeling generous) and I have dog food) then pet my dog

becomes:

If not ((false or false or false) and true) then pet my dog

becomes:

If not (false and true) then pet my dog

becomes:

If not (false) then pet my dog

becomes:

If true then pet my dog

So Rover gets a pet (even though you weren’t feeling generous). That one worked out, but how about this:

If not (my dog is hungry or I’m leaving for work) then pet my dog else feed my dog

What would that be if you chose to get rid of the not?

If (my dog is not hungry) or (I’m not leaving for work) then pet my dog else feed my dog

Right…? Wrong. Think like a computer to find the problem. What if your dog is hungry but you’re not leaving for work?
Let’s evaluate the things in those parentheses.

If (my dog is not hungry) or (I’m not leaving for work) then pet my dog else feed my dog

becomes:

If (false or true) then pet my dog else feed my dog

If (true) then pet my dog else feed my dog

Well, Rover doesn’t get fed, apparently. Poor guy, he was hungry too. So, back to:

If not (my dog is hungry or I’m leaving for work) then pet my dog else feed my dog

How can we get rid of that outside not? By swapping the “or” with an “and”.

If (my dog is not hungry) and (I’m not leaving for work) then pet my dog else feed my dog

That becomes:

If (false) and (true) then pet my dog else feed my dog

If (false) then pet my dog else feed my dog

Rover gets fed!

When flipping conditions with the all-powerful not, remember that “and”s become “or”s and “or”s become “and”s.
We humans don’t usually deal in long chained conditions with nots, ors, and ands, so this is where you’ll have to think like a computer to get things right.

If any of this was confusing then please let us know, else good luck!

Leave a Reply

Scroll to Top