Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Procedural generation to make a galaxy.

Search for life

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

Giving a list some data when it is declared is called enumerating. In this program, the arrays creature, colour and characteristic are all enumerated with data for the program to use.

An alternative to using a List<T> is to use an array. They have quite different syntax but feel very similar to use. The key difference is that a List<T> can grow and shrink in size while the program is running (it is dynamic), whereas an array has a fixed number of indexes that is decided when the program is written (it is static). This makes arrays more performant, at the expense of flexibility.

As the size of these data structures never changes when the program is running, we use C# arrays (string[]).

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of using the variable planet as the seed for new Random(planet)?

Reveal answer

Every time you run the program you need the planet to be the same as it was last time the program was run. By setting the seed using the planet variable you can ensure the same deterministic set of values are always generated for a given planet by the (not so) random number generator.

This is a pretty neat trick called procedural generation used in games development. Instead of storing all the data, the computer can generate it when it needs it instead.

In this example, we can have billions of planets that stay the same while only using a small fraction of memory because the attributes are stored, not the planets.

Reason question

Why are the random indexes generated with random_generator.Next(0, 3) (max exclusive) rather than Next(1, 4) given that each list has three possible values?

Reveal answer

Arrays are zero-indexed. I.e. they start at zero and not one. E.g. creature[0] is "lizards", creature[1] is "humanoids" and creature[2] is "insects".

Make

Change the program so that:

  1. It allows for planets to have a hot, frozen, barren or a temperate climate.
  2. The weather on the planet can be one of four values corresponding to the climate.
  3. Only temperate planets can sustain life.
  4. The output messages match the format shown below.

Typical inputs and outputs from the program would be:

Enter the catalogue number of a planet:
10
Probes report a hot planet with no signs of life.
Enter the catalogue number of a planet:
11
Probes report green, angry insects on a temperate planet.

The test runner provides the catalogue number on its own line of stdin and accepts any valid procedural output that matches the format Probes report <colour>, <characteristic> <creature> on a temperate planet. or Probes report a <climate> planet with no signs of life.. The actual colour/creature for a given seed will differ between Python and C# because the languages use different random number algorithms.

🆘 If you're really stuck, use this Parsons code sorting exercise
Complete program
// Search for life program

using System;

class Submission
{
---
    // -------------------------
    // Subprograms
    // -------------------------
    // Function to return what probes find
---
    static string probe(int planet)
    {
---
        // Ensure the planet always returns the same result
        Random random_generator = new Random(planet);
        string[] climate = { "hot", "frozen", "barren", "temperate" };
        int weather = random_generator.Next(0, 4);
---
        // Only temperate planets can host life
        string report = "";
        if (weather == 3)
        {
---
            string[] creature = { "lizards", "humanoids", "insects" };
            string[] colour = { "red", "green", "blue" };
            string[] characteristic = { "shy", "angry", "docile" };
            int lifeform = random_generator.Next(0, 3);
            int specimen = random_generator.Next(0, 3);
            int behaviour = random_generator.Next(0, 3);
            report = colour[specimen];
            report = report + ", " + characteristic[behaviour];
            report = report + " " + creature[lifeform];
            report = report + " on a " + climate[weather] + " planet.";
---
        }
---
        else
        {
---
            report = "a " + climate[weather] + " planet with no signs of life.";
---
        }
---
        return report;
---
    }
---


    // -------------------------
    // Main program
    // -------------------------
    public static void Main(string[] args)
    {
---
        Console.WriteLine("Enter the catalogue number of a planet:");
        int planet = Convert.ToInt32(Console.ReadLine());
        Console.WriteLine("Probes report " + probe(planet));
---
    }
---
}