This is a fun little programme that creates a universe of planets for us to explore.
00:07
Games like no Man's sky use a concept known as procedural generation to create the planets in the game.
00:13
Instead of storing all the data for the planets, instead it uses sequences of numbers to determine what the planet will be like.
00:20
And since the sequence is always the same, the same planets are generated every time the player visits them.
00:27
In the early days of video games, when computers had very little memory, this allowed vast worlds to be created using only a tiny amount of storage.
00:37
as always, we'll start with our boilerplate.
00:40
In a moment, we'll write a subprogram to probe a planet.
00:43
But first we'll start in the main programme by asking the user to enter a number for a planet.
00:50
We'll make this an integer with int and store it in a variable called planet.
00:55
As inputs are strings, we also cast this to an integer.
00:59
These aren't real catalogue numbers, just whole numbers.
01:04
Then we want a probe to send back a report of what it found on the planet.
01:08
So this will be a string identified as report, and it will be assigned to be what is returned from the subprogram probe of the planet.
01:19
Next, we want to output the report using console writeline, concatenating the text with the variable report.
01:27
Okay, now the fun part.
01:29
Let's programme the probe.
01:31
Define a subprogram staticstringprobe with the parameter of the planet number.
01:38
Here we're going to return a string which will be our report.
01:42
We need a random number generator to get a sequence of numbers, but this time we'll give it a seed, which is the number of the planet.
01:51
This means that every time we ask for a random sequence, we'll get the same set of random numbers for that planet, meaning its characteristics will not change.
02:00
We have random planets, but what's on the planet will be deterministic.
02:06
In this programme, we're going to use an array that's a set of variables, all with the same identifier.
02:12
Previously we used a list, but in this programme we won't need to add or delete items from the list.
02:18
So an array is easier.
02:20
String, open and close brackets.
02:22
We're not going to define the number of elements.
02:24
Here.
02:25
Creature is assigned to be an array containing lizards, humanoids and insects.
02:34
Next, the colours will have red, green and blue.
02:37
The characteristics will be shy, angry and docile.
02:41
This is all the data that our programme needs.
02:44
Giving values to the elements when array is declared is called enumerating the array.
02:51
Okay, Our lifeform will be a random creature from the creature array either index zero, a lizard, index one, a humanoid or index two insects.
03:03
So lifeform becomes a random integer.
03:06
Open bracket between zero and three, close bracket.
03:11
Remember, random numbers go from the bottom number but don't include the top number.
03:16
So essentially this is a number between 0 and 2 matching the indexes in the array.
03:23
We'll repeat this for the specimen, which is the colour and behaviour, which is the characteristic.
03:29
It may seem odd to be using different variable names here, but it's so the next part of our programme is easier to read and understand.
03:37
Our report will be blank until it's written.
03:40
So report becomes an empty string.
03:45
Report is then assigned to be the colour of the specimen.
03:49
So what's actually happening here is that the specimen is a random number between 0 and 2, so it becomes the element at that index from the colour array.
03:58
In other words, if specimen is one, then colour specimen is green because.
04:04
Because the numbers start at zero, we will now add that to the report.
04:10
So report is itself plus a comma and a space plus the random characteristic.
04:19
We'll do the same for the type of creature.
04:22
And finally we'll finish the sentence we've created in the variable report with on the planet, so it makes sense.
04:30
The report can now be returned back to the main programme.
04:34
So let's break this down.
04:36
Planet is a number that the user can choose.
04:39
This is used as the seed for the random number generator.
04:42
So we always get the same random sequence for the chosen planet.
04:46
We generate three random numbers between 0 and 2 representing the creature, the colour and the characteristic.
04:55
We use these numbers to look up the text data, in the creature colour and characteristic arrays.
05:02
We join all this together into a single sentence.
05:06
Let's give the programme a run.
05:09
Planet 34 has green docile insects.
05:12
But if we run the programme again, we can see that planet 34 still has green docile insects, even though we never stored the data about a planet.
05:22
Probes find red shy humanoids on Planet 36.
05:28
There's another important computing concept here.
05:31
Our planets are just numbers and our creatures and their characteristics are ultimately just numbers too, from the deterministic sequence, or what is known as procedural generation in the games industry.
05:44
So the only thing that really mattered was the numbers and what they corresponded to in the arrays.
05:51
Nothing else about our universe was important.
05:54
Planets just exist.
05:55
They don't orbit, they don't have suns, there's no solar systems.
05:59
In fact, they're not planets at all, really.
06:02
This is known as abstraction, only including the necessary details and, leaving out the unnecessary details when solving a problem.