'F' → Fullscreen
Today you build a dungeon run in a new program named 1-24-dungeon-run. The path is an array of squares. Walk one square at a time or run two to four, dodge the traps, pick up the gold, and reach the exit.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
path.Length says how many squares there are, and path[path.Length - 1] is the last one."gold", "trap", just before "exit" — and run it again without changing anything else. Write down what the first line says now. Then take the two squares back out.string[] path = { "start", "empty", "gold", "trap", "empty", "gold", "trap", "empty", "gold", "trap", "gold", "exit" };
Console.WriteLine($"The path is {path.Length} squares long, and square {path.Length} is the {path[path.Length - 1]}.");Console.WriteLine("Walking moves 1 square. Running moves 2 to 4. Every trap costs 1 health. Escape with as much gold as you can.");position, health, and gold arrive with the game: where you are as an index, and what you have left.path.Length - 1 is the last index again — the exit.position forward, then reads the square you landed on. Gold is picked up: the square is set to "empty", the way Activity 1.23 changed an element.string[] path = { "start", "empty", "gold", "trap", "empty", "gold", "trap", "empty", "gold", "trap", "gold", "exit" };
int position = 0;int health = 3;int gold = 0;
Console.WriteLine($"The path is {path.Length} squares long, and square {path.Length} is the {path[path.Length - 1]}.");Console.WriteLine("Walking moves 1 square. Running moves 2 to 4. Every trap costs 1 health. Escape with as much gold as you can.");Console.WriteLine();
while (position < path.Length - 1 && health > 0){ Console.Write($"Square {position + 1}, health {health}, gold {gold}. Walk (1) or run (2)? "); int choice = int.Parse(Console.ReadLine());
int steps = 1;
if (choice == 2) { steps = Random.Shared.Next(2, 5); }
position = position + steps;
string square = path[position]; Console.WriteLine($" You move {steps} to square {position + 1}: {square}");
if (square == "trap") { health = health - 1; }
if (square == "gold") { gold = gold + 1; path[position] = "empty"; }}
Console.WriteLine();
if (health > 0){ Console.WriteLine($"You escaped with {gold} gold!");}else{ Console.WriteLine($"You collapsed on square {position + 1} with {gold} gold.");}string[] path = { "start", "empty", "gold", "trap", "empty", "gold", "trap", "empty", "gold", "trap", "gold", "exit" };
int position = 0;int health = 3;int gold = 0;
Console.WriteLine($"The path is {path.Length} squares long, and square {path.Length} is the {path[path.Length - 1]}.");Console.WriteLine("Walking moves 1 square. Running moves 2 to 4. Every trap costs 1 health. Escape with as much gold as you can.");Console.WriteLine();
while (position < path.Length - 1 && health > 0){ Console.Write($"Square {position + 1}, health {health}, gold {gold}. Walk (1) or run (2)? "); int choice = int.Parse(Console.ReadLine());
int steps = 1;
if (choice == 2) { steps = Random.Shared.Next(2, 5); }
position = position + steps;
if (position > path.Length - 1) { position = path.Length - 1; }
string square = path[position]; Console.WriteLine($" You move {steps} to square {position + 1}: {square}");
if (square == "trap") { health = health - 1; }
if (square == "gold") { gold = gold + 1; path[position] = "empty"; }}
Console.WriteLine();
if (health > 0){ Console.WriteLine($"You escaped with {gold} gold!");}else{ Console.WriteLine($"You collapsed on square {position + 1} with {gold} gold.");}path.Length, then look - 1 to read it. Activity 1.23's guess - 1, with a check in front.else. Select those lines and press Tab once to indent them; that is why so many lines are marked.string[] path = { "start", "empty", "gold", "trap", "empty", "gold", "trap", "empty", "gold", "trap", "gold", "exit" };
int position = 0;int health = 3;int gold = 0;int scouts = 2;
Console.WriteLine($"The path is {path.Length} squares long, and square {path.Length} is the {path[path.Length - 1]}.");Console.WriteLine("Walking moves 1 square. Running moves 2 to 4. Every trap costs 1 health. Escape with as much gold as you can.");Console.WriteLine();
while (position < path.Length - 1 && health > 0){ Console.Write($"Square {position + 1}, health {health}, gold {gold}. Walk (1), run (2), or scout (3)? "); int choice = int.Parse(Console.ReadLine());
if (choice == 3 && scouts > 0) { Console.Write(" Scout which square? "); int look = int.Parse(Console.ReadLine());
if (look >= 1 && look <= path.Length) { Console.WriteLine($" Square {look} is {path[look - 1]}."); scouts = scouts - 1; } else { Console.WriteLine($" There is no square {look}. The path is squares 1 to {path.Length}."); } } else if (choice == 3) { Console.WriteLine(" No scouts left."); } else { int steps = 1;
if (choice == 2) { steps = Random.Shared.Next(2, 5); }
position = position + steps;
if (position > path.Length - 1) { position = path.Length - 1; }
string square = path[position]; Console.WriteLine($" You move {steps} to square {position + 1}: {square}");
if (square == "trap") { health = health - 1; }
if (square == "gold") { gold = gold + 1; path[position] = "empty"; } }}
Console.WriteLine();
if (health > 0){ Console.WriteLine($"You escaped with {gold} gold!");}else{ Console.WriteLine($"You collapsed on square {position + 1} with {gold} gold.");}Three changes, one at a time. Write down exactly what happens, and put each one back before the next.
while condition's position < path.Length - 1 to position < path.Length. Play until you reach the exit, and keep going.look >= 1 && and scout square 0. Put it back, then delete && look <= path.Length and scout square 13. Record the exception and line number both times.path[path.Length - 1] to path[11]. It still works. Now delete "gold", just before "exit" in the array, and run it.Number 1 never crashes, and that is what makes it worth noticing. Say why the game cannot end. For number 3, say which line crashed and why path[11] was right until the path changed.
There is no code block for this task. When it is over, your program is exactly what it was at the end of Task 4.
| Meaning | C# | Exam notation |
|---|---|---|
| How many elements | path.Length | LENGTH(path) |
| The last element | path[path.Length - 1] | path[LENGTH(path)] |
| Add to the end | — | APPEND(path, "gold") |
| Insert at a position | — | INSERT(path, 3, "trap") |
| Remove at a position | — | REMOVE(path, 3) |
Exam lists start at 1, so the last element is at LENGTH(path) exactly. That is tidier than C#, and it is a trap in both directions: writing path[LENGTH(path) - 1] on the exam gets you the second to last, and writing path[path.Length] in C# crashes.
A C# array is the size it was made, for as long as it exists — which is why your path could only change by editing the line that creates it. Exam lists change size:
APPEND adds to the end, and the length goes up by one.INSERT(path, i, x) puts x at position i and shifts everything from there on one place right.REMOVE(path, i) takes out position i and shifts everything after it one place left.Both INSERT and REMOVE change the length, and they change which position everything after them is at. You will only ever read these on the exam, so trace them by writing the list out again after every line.
The reference sheet is explicit: if an index is less than 1 or greater than the length of the list, an error is produced and the program terminates. That is exactly what C# did in Task 2. If an exam question walks a list one step too far, the answer is not a blank or a zero — nothing after that point happens at all.
One real game from the finished program, scouting twice. Your runs will move different distances.
The path is 12 squares long, and square 12 is the exit.Walking moves 1 square. Running moves 2 to 4. Every trap costs 1 health. Escape with as much gold as you can.
Square 1, health 3, gold 0. Walk (1), run (2), or scout (3)? 1 [Enter] You move 1 to square 2: emptySquare 2, health 3, gold 0. Walk (1), run (2), or scout (3)? 1 [Enter] You move 1 to square 3: goldSquare 3, health 3, gold 1. Walk (1), run (2), or scout (3)? 3 [Enter] Scout which square? 4 [Enter] Square 4 is trap.Square 3, health 3, gold 1. Walk (1), run (2), or scout (3)? 2 [Enter] You move 4 to square 7: trapSquare 7, health 2, gold 1. Walk (1), run (2), or scout (3)? 3 [Enter] Scout which square? 9 [Enter] Square 9 is gold.Square 7, health 2, gold 1. Walk (1), run (2), or scout (3)? 2 [Enter] You move 4 to square 11: goldSquare 11, health 2, gold 2. Walk (1), run (2), or scout (3)? 2 [Enter] You move 2 to square 12: exit
You escaped with 2 gold!Answer the following questions before submitting your work.
path[path.Length - 1] better than path[11], even when the path happens to have twelve squares?aList[LENGTH(aList)], with no minus one. Explain why the exam and C# differ by one here, and which mistake you are more likely to make out of habit.Submit four things to the dropbox: