Part II · Growing the Project
Chapter 5: Collections of Things
For the rest of this book, we'll assume your player found the right answer and made it through the door. (If they didn't, the game just ends, and that's a perfectly good ending too. Feel free to keep both paths in your own version.) Now that they're inside, they're going to find things. That means it's time for your game to hold more than one value at once.
Why Not Just Use More Variables?
Here's a question worth sitting with for a second: if your player finds a sword, a torch, and a rope, why not just do this?
var item1 = "sword"
var item2 = "torch"
var item3 = "rope"You could. But what happens when they find a fourth item? A tenth? You'd be adding a new variable every time, and writing new code every time, just to handle one more thing. This is the exact wall I hit myself, years ago, first learning to program. Separate variables work fine until the number of things you're tracking stops being fixed. Once it isn't fixed, you need a different tool.
Making an Array
An array is a single variable that holds a list of values, in order.
var inventory = ["torch", "rope", "sword"]That's one variable holding three things, not three variables. You can also make an empty one and fill it in later:
var inventory = array()Reading and Changing Items by Index
Every item in an array has a position, called an index, starting at zero, not one.
writeln(inventory[0]) // torch, the first item
writeln(inventory[1]) // rope, the second item
writeln(inventory[2]) // sword, the third itemStarting at zero trips up almost everyone the first time. The first item
is index 0, and the last item's index is always one less than however
many items you have. You can change an item the same way:
inventory[0] = "lantern"Growing and Shrinking
Three functions handle adding and removing items:
append(inventory, "map") // adds "map" to the end
insert(inventory, 0, "key") // inserts "key" at position 0
remove(inventory, 2) // removes whatever is at position 2And length() tells you how many items are currently in the array:
writeln("You are carrying " + length(inventory) + " items.")The Moment This Clicks
Here's the anecdote from the introduction, made concrete. An array by itself is just a labeled shelf. What makes it powerful is pairing it with a loop, so you can do something to every item without writing a separate line for each one.
for each item in inventory
writeln("- " + item)
end forThat one small block prints your entire inventory, however many items are in it, whether that's three or thirty. Change the array, and the loop still works, no edits required. That pairing, a collection plus a loop that walks through it, is the moment arrays stop being a curiosity and start being the reason you'd choose them over separate variables at all.
Growing the Adventure
Give your player somewhere to actually collect things.
// The Hollow Cottage
// Chapter 5
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var tries = 0
var solved = false
while tries < 3 and not solved
tries = tries + 1
var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
solved = true
else
writeln("Nothing happens.")
end if
end while
if not solved then
writeln("The door refuses to budge. You'll have to find another way in.")
else
writeln("The cottage is dusty but intact.")
var inventory = array()
append(inventory, "rusty key")
append(inventory, "half-burned candle")
writeln("On the floor, you notice:")
for each item in inventory
writeln("- " + item)
end for
writeln("You pick up both items.")
end ifMake It Yours. Add a third item that fits your version of the story,
then remove one of the original two using remove(). Print the inventory
again afterward so you can see the change take effect.
Chapter Checkpoint
Open either known-good checkpoint directly in QF Code, or download the .qfc file to keep locally. The IDE opens in a new tab and does not run the program automatically.
Start this chapter
Chapter 5 starting checkpoint.
Continue from here
Chapter 5 completed checkpoint.
Starting code: the Chapter 4 checkpoint.
Completed code (Chapter 5):
// The Hollow Cottage
// Chapter 5
const cottageName = "The Hollow Cottage"
writeln("You are standing outside " + cottageName + ".")
var playerName = input("What is your name, traveler? ")
writeln("Welcome, " + playerName + ".")
var tries = 0
var solved = false
while tries < 3 and not solved
tries = tries + 1
var answer = input("There is a riddle carved into the door. It asks: what has keys but no locks? ")
if answer == "piano" then
writeln("The door swings open. You step inside.")
solved = true
else
writeln("Nothing happens.")
end if
end while
if not solved then
writeln("The door refuses to budge. You'll have to find another way in.")
else
writeln("The cottage is dusty but intact.")
var inventory = array()
append(inventory, "rusty key")
append(inventory, "half-burned candle")
writeln("On the floor, you notice:")
for each item in inventory
writeln("- " + item)
end for
writeln("You pick up both items.")
end ifCopyright © 2026 Edison Mooers. Published by Gibidda Press. Free for personal learning and qualifying noncommercial educational use. Commercial training, organizational use, redistribution, and adaptation require written permission. Full use terms.