Unit 1 – Programming

Learning the Basics

C++ is one of the most popular programming languages in the world, used to build everything from simple system utilities to complex video games. In this unit, you will learn the fundamentals of the C++ programming language, including its syntax, data types, control structures, and functions.

Syntax

The syntax of C++ is very similar to that of the C programming language, with a few notable differences. For example, C++ supports object-oriented programming concepts like classes and inheritance, while C does not. Additionally, C++ has a more extensive standard library, providing developers with more tools for building complex software.

Data Types

In C++, there are several built-in data types, including integers, characters, booleans, and floating-point numbers. You can also create your own custom data types using classes and structures. Understanding data types is essential in C++, as they determine how much memory a variable takes up and what operations can be performed on it.

Control Structures

Control structures in C++ allow you to change the order in which instructions are executed based on certain conditions. For example, you might use an “if” statement to execute one block of code if a certain condition is true, and another block of code if it is false. C++ also supports for-loops, allowing you to execute the same block of code multiple times until a certain condition is met.

Functions

Functions in C++ are self-contained blocks of code that perform a specific task and can be called from anywhere in your program. Understanding how to create and use functions is critical in C++, as it allows you to write modular, reusable code.

Overall, learning the C++ programming language is an excellent choice for anyone interested in game development.

Advent of Code

Advent of Code is an annual set of Christmas-themed computer programming challenges that follow an Advent calendar. It has been running since 2015. The programming puzzles cover a variety of skill sets and skill levels and can be solved using any programming language.

To help transition into learning the basics of C++, I was tasked to complete days 1 – 4 of the Advent of Code using Visual Studio Code.

Advent of Code Day 1

— Day 1: Sonar Sweep —
You’re minding your own business on a ship at sea when the overboard alarm goes off! You rush to see if you can help. Apparently, one of the Elves tripped and accidentally sent the sleigh keys flying into the ocean!

Before you know it, you’re inside a submarine the Elves keep ready for situations like this. It’s covered in Christmas lights (because of course it is), and it even has an experimental antenna that should be able to track the keys if you can boost its signal strength high enough; there’s a little meter that indicates the antenna’s signal strength by displaying 0-50 stars.

Your instincts tell you that in order to save Christmas, you’ll need to get all fifty stars by December 25th.

Collect stars by solving puzzles. Two puzzles will be made available on each day in the Advent calendar; the second puzzle is unlocked when you complete the first. Each puzzle grants one star. Good luck!

As the submarine drops below the surface of the ocean, it automatically performs a sonar sweep of the nearby sea floor. On a small screen, the sonar sweep report (your puzzle input) appears: each line is a measurement of the sea floor depth as the sweep looks further and further away from the submarine.

For example, suppose you had the following report:

199
200
208
210
200
207
240
269
260
263
This report indicates that, scanning outward from the submarine, the sonar sweep found depths of 199, 200, 208, 210, and so on.

The first order of business is to figure out how quickly the depth increases, just so you know what you’re dealing with – you never know if the keys will get carried into deeper water by an ocean current or a fish or something.

To do this, count the number of times a depth measurement increases from the previous measurement. (There is no measurement before the first measurement.) In the example above, the changes are as follows:

199 (N/A – no previous measurement)
200 (increased)
208 (increased)
210 (increased)
200 (decreased)
207 (increased)
240 (increased)
269 (increased)
260 (decreased)
263 (increased)
In this example, there are 7 measurements that are larger than the previous measurement.

How many measurements are larger than the previous measurement?

Advent of Code – Day 1: Sonar Sweep

Advent of Code Day 1 Solution

To solve this challenge, I needed to create random depths to calculate the increase or decrease. I created a function called ‘randomInt()’ which returns a random integer value between the given minimum and maximum values.

I used this function within a for-loop to create an array of random depth measurements for the solution.

Next, I needed a way to calculate if the current depth measurement has increased or decreased. I achieved this by creating a for-loop which iterates over the array of measurements and, if iterator is greater than zero, determines if the current value is greater than the previous value.

I then output the results in the format from the challenge, for example, 168 (increased).

Finally, the total depth increase is calculated and output to the console.

Advent of Code Day 2

— Day 2: Dive! —

Now, you need to figure out how to pilot this thing.

It seems like the submarine can take a series of commands like forward 1, down 2, or up 3:

forward X increases the horizontal position by X units.
down X increases the depth by X units.
up X decreases the depth by X units.
Note that since you’re on a submarine, down and up affect your depth, and so they have the opposite result of what you might expect.

The submarine seems to already have a planned course (your puzzle input). You should probably figure out where it’s going. For example:

forward 5
down 5
forward 8
up 3
down 8
forward 2
Your horizontal position and depth both start at 0. The steps above would then modify them as follows:

forward 5 adds 5 to your horizontal position, a total of 5.
down 5 adds 5 to your depth, resulting in a value of 5.
forward 8 adds 8 to your horizontal position, a total of 13.
up 3 decreases your depth by 3, resulting in a value of 2.
down 8 adds 8 to your depth, resulting in a value of 10.
forward 2 adds 2 to your horizontal position, a total of 15.
After following these instructions, you would have a horizontal position of 15 and a depth of 10. (Multiplying these together produces 150.)

Calculate the horizontal position and depth you would have after following the planned course. What do you get if you multiply your final horizontal position by your final depth?

Advent of Code – Day 2: Dive!

Advent of Code Day 2 Solution

To solve this, I needed to create a function which executes the commands from the console, so if the user inputs ‘forward 3,’ then the horizontal position in incremented by 3.

I named this function ‘executeCommand()’ with 2 arguments:

  • ‘cmd’ the command (forward, down, up),
  • x, the value to move by (number).

If the forward command is chosen, then increment the horizontal position by x.

Otherwise, if the down or up command is chosen, then increment or decrement the depth by x accordingly.

If a command is not recognised, output ‘Invalid command’.

Advent of Code Day 3

— Day 3: Binary Diagnostic —
The submarine has been making some odd creaking noises, so you ask it to produce a diagnostic report just in case.

The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions
of the submarine. The first parameter to check is the power consumption.

You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the gamma rate and the epsilon rate). The power consumption
can then be found by multiplying the gamma rate by the epsilon rate.

Each bit in the gamma rate can be determined by finding the most common bit in the corresponding position of all numbers in the diagnostic report. For example,
given the following diagnostic report:

00100
11110
10110
10111
10101
01111
00111
11100
10000
11001
00010
01010
Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.

The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.

The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.

So, the gamma rate is the binary number 10110, or 22 in decimal.

The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001,
or 9 in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, 198.

Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. What is the power consumption of the
submarine? (Be sure to represent your answer in decimal, not binary.)

Advent of Code – Day 3: Binary Diagnostic

Advent of Code Day 3 Solution

To solve this challenge, I needed to make 3 functions to simplify the problem.

The first function converts a binary number (base 2) into a decimal number (base 10). To do this, I converted the binary number into a string, then created a for-loop to iterate over each index of the string. With every iteration, I check if the bit is equal to 0, if not then add the multiple value to the decimal number. Finally, the multiple value is divided by 2.

The next function is the inverse of the previous function. It converts a decimal number (base 10) into a binary number (base 2).

Advent of Code Day 4

— Day 4: Giant Squid —
You’re already almost 1.5km (almost a mile) below the surface of the ocean, already so deep that you can’t see any sunlight.
What you can see, however, is a giant squid that has attached itself to the outside of your submarine.

Maybe it wants to play bingo?

Bingo is played on a set of boards each consisting of a 5×5 grid of numbers. Numbers are chosen at random, and the chosen
number is marked on all boards on which it appears. (Numbers may not appear on all boards.) If all numbers in any row or
any column of a board are marked, that board wins. (Diagonals don’t count.)

The submarine has a bingo subsystem to help passengers (currently, you and the giant squid) pass the time. It automatically
generates a random order in which to draw numbers and a random set of boards (your puzzle input). For example:

7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1

22 13 17 11 0
8 2 23 4 24
21 9 14 16 7
6 10 3 18 5
1 12 20 15 19

3 15 0 2 22
9 18 13 17 5
19 8 7 25 23
20 11 10 24 4
14 21 16 12 6

14 21 17 24 4
10 16 15 9 19
18 8 23 26 20
22 11 13 6 5
2 0 12 3 7
After the first five numbers are drawn (7, 4, 9, 5, and 11), there are no winners, but the boards are marked as follows
(shown here adjacent to each other to save space):

22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
After the next six numbers are drawn (17, 23, 2, 0, 14, and 21), there are still no winners:

22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
Finally, 24 is drawn:

22 13 17 11 0 3 15 0 2 22 14 21 17 24 4
8 2 23 4 24 9 18 13 17 5 10 16 15 9 19
21 9 14 16 7 19 8 7 25 23 18 8 23 26 20
6 10 3 18 5 20 11 10 24 4 22 11 13 6 5
1 12 20 15 19 14 21 16 12 6 2 0 12 3 7
At this point, the third board wins because it has at least one complete row or column of marked numbers (in this case,
the entire top row is marked: 14 21 17 24 4).

The score of the winning board can now be calculated. Start by finding the sum of all unmarked numbers on that board; in
this case, the sum is 188. Then, multiply that sum by the number that was just called when the board won, 24, to get the
final score, 188 * 24 = 4512.

To guarantee victory against the giant squid, figure out which board will win first. What will your final score be if you
choose that board?

Advent of Code – Day 4: Giant Squid

Advent of Code Day 4 Solution

The first thing I needed to create to solve this challenge is 3 boards each with 25 (5*5) random numbers.

Advent of Code Conclusion

This concludes my Advent of Code practise challenges. This has given me the experience and understanding of the C++ programming language that will be needed for continuing my games development journey.

GameMaker Studio 2

GameMaker Studio 2 is a small and simple game engine for making 2D games, The engine features two ways of programming games: drag-and-drop and GML code.

Drag and drop is similar to how scratch uses code blocks to form executable code, this makes the game logic more visual and easier to understand.

GML is the programming language used, which is a sub-language of C++. GML has built-in functions and keywords specifically for new game developers to simplify the process of making games.