Home Blog Gaming

Propositional Logic

Godot Tutorials > Propositional Logic
Logic is the foundation of all computer programming, including game development. The better you understand logic, the easier it is to make your games fun, fast, and efficient.

Propositional Logic for Game Developers Video


What is Logic?

Mathematics, science, and computer programming are all based on logical reasoning. Logic defines a set of rules. It is a way of making sure everyone is on the same page.

Common human language is often left open to interpretation. If you tell your friend "Timmy's house is so big!", you are not telling her the exact size of Timmy's house. What you consider a big house, someone else might consider small. There is no room for this type of interpretation in game development. A computer can not interperate this type of language. Even if it could, its interpretation could lead to unexpected results.

As a programmer, your goal is telling the computer exactly what to do in any possible scenario. You do this by writing logic. Game development logic is based on mathematical logic. Propositional logic is the building block. Conditional statements such as if-statements are directly based on propositional logic.

Note: It is humanly impossible to think of every possible scenario. Why set an impossible goal? If you don't try to think of every possible scenario, you will end up ignoring many probable scenarios. We are all unique individuals with our own thought patterns. If you write your code only based on scenarios that are probable for you, you will ignore the needs and thought patterns of your users.


What is Propositional Logic?

Propositional logic defines the rules of validity (true or false). At the end of the day, anything can be broken down to "true or false" logic. For example, say you are taking a multiple-choice test with four options: a, b, c, or d. Only one answer is correct. This can be broken down to propositional logic by evaluating each possible answer as true or false. Once each possibility is evaluated, the true answer is the answer you select.

Propositional logic is also known as "statement logic". It is the process of determining if a statement is true or false. We use propositional logic to define conditions. When will what code execute? Since logic is based on rules and definitions, let's define a proposition.


What is a Proposition?

  1. A proposition is a statement.
  2. A proposition has a truth value: It is equal to true or false.
  3. A proposition has one truth value. It is either true or false. It can not be both.

Which of the Following are Propositions?

  1. Commander Candy is 4'11.
  2. x + y = 47
  3. Please choose a color.
In the examples above, #1 is the only proposition. It is the only statement that evaluates true or false and only has one truth value.

Combining Propositions

We can combine propositions to solve more complicated problems. We do this using variables, comparison operators, and logical operators. You may want to look back at GDScript Operators

Let's start with two statements:

 let p = Kobe completed level three.
 let q = Kobe got a perfect score on level three

p ⋀ q

In mathematical notation this is read "p and q". Pluging in our values, this would be read "Kobe completed level three and Kobe got a perfect score on level three."

In order for p ⋀ q to evaluate true, both conditions must be satisfied:
1) Kobe completed level three.
2) Kobe got a perfect score on level three.

Here is an example of how this might look in GDScript:
extends Node
var completed: bool = false
var perfect: bool = false

func _ready():
  if (completed == true) && (perfect == true):
    print("Kobe completed level three with a perfect score!")

p ⋁ q

In mathematical notation this is read "p or q". Pluging in our values, this would be read Kobe completed level three or Kobe got a perfect score on level three.

In order for p ⋁ q to evaluate true, only one of the statements must be true. Either Kobe completed level three or he got a perfect score on level three.

Here is an example of how this might look in GDScript:
extends Node
var completed: bool = false
var perfect: bool = false

func _ready():
  if (completed == true) || (perfect == true):
    print("Level Complete!")

p → q

In mathematical notation this is read "p implies q" or "if p than q". This means if p is true than q must also be true. Plugging in our values, this would be read "If Kobe completed level three, then Kobe got a perfect score on level three."

Here is an example of how this may look in GDScript:
extends Node
var completed: bool = false

func _ready():
  if (completed == true):
    print("Kobe got a perfect score on level three!")

If you want to learn more about logic, check out Logic for Programmers. The page includes additional videos and a logic quiz game!

Support Coding Commanders

Because it is crucial to achieving upward economic mobility, Coding Commanders believes everyone should have access to free quality STEM education. My tutorials explain complex concepts in plain English using relatable examples. No previous mathematics or technical knowledge required!



Donate
Linux (Ubuntu)+ NodeJS + Postgres
Learn PHP on Linux with Commander Candy
Linux Gaming Blog - Coding Commanders
Palm Beach Techie - Diverisity in Tech
Learn ReactJS VR on Linux with Commander Candy
Coding Commanders Twitter
murks - Linux Game Dev
Flux Caves Game Review on Linux by Hatnix
Sexy Linux T-shirt
Twitch: daisychaincosplay Live Coding!!!
Coding Commanders YouTube
Coding Commanders Twitter
Learn HTML on Linux with Commander Candy
ccStack - Linux, NodeJS, ExpressJS, SQL
Linux Gaming Blog YouTube Introduction
Previous Lesson | Next Lesson