TripleX Slides
TripleX Slides
● Try compiling
● Does it compile?
int main() & White Space
● int main() required in every c++ program
● Without int main() a c++ program will not run!
● We return 0 to signal program has run successfully
● Compiler ignores white space
● Code style improves readibility!
3. Your First Program
Preprocessor Directive
(Don’t be afraid!)
● Instructing the compiler to copy the contents of the iostream header file into our
code before the rest of our code is compiled
Include the header file!
● Add: #include <iostream> to the first line of your file
4. Hello, World!
std:: cout << “Hello, World!” ;
Namespace String
Scope Operator
Same name!
● Create your own intro messages for your game across two lines of
code
int a; Uninitialized
Print your the value of your variable
● Print your variable onto a new line
TripleX Codes
Declare two more variables!
● Declare an integer named b.
○ Initialize it
● Declare an integer named c.
○ Initialize it
Arithmetic Operators
Addition Operator = +
Subtraction Operator = -
Multiplication Operator = *
Division Operator = /
Multiply your variables!
● Declare the product of a, b & c
● Print your product variable on a new line
Variables
● You declare variables in code
● A variable with a given value is initialized
● Declaring a variable = Reserved space in memory
● You can add/multiply with arithmetic operators
● Print to the terminal like: std::cout << a;
6. const & Assigning Values
Variables
● Variables can be changed at runtime
● After a variable has been declared, we can assign a
new value to it at any point in our code
● “A value that is subject to change”
Assigning values to variables
● We never want to assign values to variables before
the variable has been declared
● The compiler will not allow it.
● The compiler will not be aware that the variable
exists
Try this...
● Prefix all your variable declarations with: const
const int a = 4;
● Assignment Operator =
● We can assign values to variables like: a = 4;
● Cannot assign before a variable has been declared!
● const keyword to mark your variables as constant
● The compiler will protect const variables!
7. Statements & Comments
Expression Statements
std::cout << “Hello, World!”;
myvariable = 5;
myVariable MyVariable
https://docs.unrealengine.com/en-us/Programming/Development/CodingStandard
Rename your variables!
● Rename your a, b & c variables:
○ CodeA, CodeB, CodeC
● Rename sum and product:
○ CodeSum, CodeProduct
○ Make sure they are initialized correctly!
● Follow Unreal Engine 4’s naming convention
Naming & Self Documenting Code
● Self document code with good naming!
● Must begin with a letter or an underscore
● You must not use a reserved keyword
● Unreal’s naming convention UpperCamelCase
● Right click and “change all occurances” in VS code
to rename and replace variable everywhere in file
9. Getting User Input
std::cout << “Hello, World!”;
Insertion Operator
Character Output
std::cin >> PlayerGuess;
Character Input
CodeB GuessB
CodeC GuessC
234
Your program will only ask for more input when cin is called
IF the input stream is empty!
What’s relevant?
● We want the players to enter numbers only
● They can enter numbers on seperate lines
● Or the same line separated by spaces
● We need to add a fix later! Incase anything other
than a number gets entered & for replayability
Declare Guess Sum & Products
● Declare GuessSum
○ Initialize it by adding GuessA, GuessB & GuessC
● Declare GuessProduct
○ Initialize it by multiplying GuessA, GuessB &
Guess C
Getting User Input
● cout = Character Output
○ Insertion Operator = <<
● cin = Character Input
○ Extraction Operator = >>
● Characters like ‘x’ get converted to 0 with integers
● Chars like ‘x’ will halt cin from working until reset
10. Using if and else in C++
if statements
if (condition)
{
}
if statements
if (condition)
{
std::cout << “you win!”;
}
if statements
if (true)
{
std::cout << “you win!”;
}
if statements
if (false)
{
std::cout << “you win!”;
}
Implement your if statement
● Implement an if statement
● Enter true as the condition
● Add a code block that prints a win message
● Play TripleX and see what happens if you win
● Play again and see what happens if you lose
if statements
if (condition)
{
std::cout << “you win!”;
}
if (GuessSum == CodeSum && GuessProduct == CodeProduct)
{
std::cout << “You win!”;
}
else
{
std::cout << “You lose!”;
}
You win...you lose...
● Print a win message if the player enters the
correct code
● Print a lose game message if the player enters
an incorrect code
● Share your work so far in the community!
Using if and else in C++
● == Equality operator
● && Logical “and” operator
● if (condition)
○ Executes the code block below if condition is met
● else
○ Executes code block below if condition is not met
11. Functions
std::cout << “Hello, World!” << std::endl;
New line
(Line feed)
Escape sequence
Make use of: \n
● Modify your strings to use \n escape sequence
○ Remove std::endl where necessary
Function
● A function is a group of statements that together,
performs a task and is given a name
● Every C++ program must have at least one function
○ main()
int main()
{
std::cout << “Hello, World!”;
return 0;
}
Let’s create a function!
● Our function will return no value
● The name of our function is going to be PlayGame
● The function will contain no parameters
● The function body will contain the code for playing
our game
Your first function!
● Return type: void
● Name the function PrintIntroduction
● PrintIntroduction() at the start of PlayGame()
● Create some cool ASCII art to share in the
community!
Functions
● \n new line escape sequence in strings
● A function is a group of statements that is named
and performs a task
● You must specify the return type of a function
● void to return no data
● Must be implemented before it is executed!
12. Returning Data From Functions
while loop
while (condition)
{
PlayGame();
}
LevelDifficulty + 1
4 + 1 =5
Variable Scope
● A variable declared inside a code block has scope
● It is local to where it is declared
● Cannot be accessed or used outside of its ‘scope’
Create a parameter for PlayGame()
1 0-0
rand() % Difficulty
Difficulty (Range)
1 0-0
rand() % Difficulty
Difficulty (Range)
1 0-0
2 0-1
3 0-2
4 0-3
5 0-4
rand() % Difficulty rand() % Difficulty + 1
Difficulty (Range) (Range)
1 0 1-1
2 0-1 1-2
3 0-2 1-3
4 0-3 1-4
5 0-4 1-5
Challenge
● Initialize your code variables with: rand() % Difficulty
● Add 1 to Difficulty to offset the range
1 1
2 2-3
3 3-5
4 4-7
5 5-9
Not random yet!
● Our game isn’t random yet!
● Each time we play it, it produces the same result!
● We have one more step...
Seeding rand()
● We need to initialize rand() with a different seed
○ This will produce more random results
● The best way to do this is based on your computer’s time
● #include <ctime>
● At the start of main add this line:
○ srand(time(NULL));
○ Creates new random sequence based on the time of day
How far can you get?
● Play your game!
● What level can you get to?
● What level do you find really hard?
● Share in the community!
Section 2 Ending
● Well done for completing section 2!
● You’ve built your first game in C++!
● Play an active part in the community!
○ Help out other students
○ Share your work
Generating Random Number Ranges
● Modulus Operator: %
● Performs a division but returns remainder
● We can use % to control the range of rand()
● + 1 to offset the range
● Well done for completing section 2!
● Play an active part in the community!
Unreal C++ Projects for VSCode & VS2019
Creating Unreal C++ Projects
● Unreal Engine 4 expects:
○ Visual Studio 2017 installed if running Windows
○ Xcode installed if running macOS
● By default, Unreal will not allow you to create C++
projects without having these installed
● What if we are running VS Code or Visual Studio 2019?
Fix for VS Code & Visual Studio 2019
● Launch the Unreal Engine 4 editor
● Create a Blueprint Project
● Change the source control editor from Unreal’s
Editor Preferences
Blueprint projects
● Blueprint projects can still be C++ projects!
● You just have to add a C++ class to your project
C++ Projects for VSCode & VS2019
● To use VSCode or VS2019:
● Create a Blueprint Project
● Change the source control editor from Unreal’s
Editor Preferences
● Blueprint projects can still be C++ projects!
● You just have to add a C++ class to your project