Recording a narrative.
Watch this video to learn about the new concepts shown in the program:
int[,] x = new int[y, z];
Declares x as a 2D array with y rows and z columns. A 2D array has a fixed number of rows and columns, set when it is created.
List<List<string>> x = new List<List<string>>();
Declares x as an empty list of string lists (a 2D list). Unlike an array, a 2D list can grow while the program runs.
x.Add(new List<string>());
Adds a new row to 2D list x. The new row contains an empty list.
x[y].Add(z);
Adds a new element z to row y in the 2D list x.
Questions to think about with this program to check your understanding:
What is the purpose of the nested generic parameters in:
List<List<string>> book = new List<List<string>>();
To define an empty list whose elements are themselves lists of strings.
This creates a table of unknown rows and unknown columns. It is easier to visualise like this…
| Outside list | Inside list |
|---|---|
| Chapter 1 | Story log 1 |
| Story log 2 | |
| Chapter 2 | Story log 3 |
| Story log 4 | |
| Story log 5 |
…where the number of chapters and story logs can increase and decrease at any time.
Why has the programmer chosen List<List<string>> instead of a fixed string[,] 2D array?
List<T> is dynamic. That means the number of elements in the structure can grow and shrink as the program is running. With an array the number of indexes are static (fixed) when the program is written and cannot change. Using a list of lists allows any number of chapters and story entries to be added to the book later. This is better for a situation where you may want to expand the story.
Change the program so that it:
log.txt, so that modding tools could read the data generated by the program and make use of it.CHAPTER 1
----------
Log 1 : I find myself alone on a strange world, unequipped and in danger. I have no memory of how I got here, no sense of a before.
Log 2 : My Exosuit at least seems to know what it's doing, and I am not dead yet...
CHAPTER 2
----------
Log 3 : I received a set of mysterious coordinates from an unknown source.
Log 4 : I followed the signal, and found the wreckage of an abandoned starship.
Log 5 : There was little to be gained from the wreck, but the distress beacon contained the hailing frequency labelled 'ARTEMIS'.
output
// Output the story so far
static void output()
{
---
int log = 1;
// Loop through the chapters
for (int chapter = 0; chapter < book.Count; chapter++)
{
---
Console.WriteLine($"CHAPTER {chapter + 1}");
Console.WriteLine("----------");
Console.WriteLine();
---
// Loop through the story entries in the chapter
foreach (string entry in book[chapter])
{
---
Console.WriteLine($"Log {log} : {entry}");
log++;
---
}
---
Console.WriteLine();
---
}
}
save_log
// Save the logbook
static void save_log()
{
---
int log = 1;
StreamWriter file = new StreamWriter("log.txt");
---
// Loop through each chapter starting at chapter 1
for (int chapter = 0; chapter < book.Count; chapter++)
{
---
file.WriteLine($"CHAPTER {chapter + 1}");
file.WriteLine("----------");
file.WriteLine();
---
// Loop through the story entries in the chapter
foreach (string entry in book[chapter])
{
---
file.WriteLine($"Log {log} : {entry}");
log++;
---
}
---
file.WriteLine();
---
}
---
file.Close();
}
// -------------------------
// Main program
// -------------------------
public static void Main(string[] args)
{
---
add_chapter();
add_story(0, "I find myself alone on a strange world, unequipped and in danger. I have no memory of how I got here, no sense of a before.");
add_story(0, "My Exosuit at least seems to know what it's doing, and I am not dead yet...");
---
add_chapter();
add_story(1, "I received a set of mysterious coordinates from an unknown source.");
add_story(1, "I followed the signal, and found the wreckage of an abandoned starship.");
add_story(1, "There was little to be gained from the wreck, but the distress beacon contained the hailing frequency labelled 'ARTEMIS'.");
---
output();
save_log();
---
}