'F' → Fullscreen
Copy template-console to 1-20-walk. Make a table of five things — anything — and print it twice, once each way:
local crew = {"Ada", "Bo", "Cy", "Dee", "Eli"}
for i = 1, #crew do print(i .. ": " .. crew[i])end
print("---")
for i, name in ipairs(crew) do print(i .. ": " .. name)endThe output is identical. Then add a sixth name and run it again without touching either loop.
Make a table of five numbers. In one traversal, print each number doubled. In another, add them all up and print the total:
local scores = {12, 8, 21, 3, 17}local total = 0
for i, score in ipairs(scores) do total = total + scoreend
print("Total: " .. total)print("Count: " .. #scores)print("Average: " .. total / #scores)local total = 0 goes above the loop, not inside it. This is the same rule as session 15's tries and session 14's hits. Third time; it should be a habit by now.Copy template-game to 1-20-sprites:
local xs = {120, 260, 400, 540, 680}local ys = {180, 320, 240, 430, 200}
function love.draw() for i, x in ipairs(xs) do love.graphics.setColor(0.3, 0.6 + i / 20, 1) love.graphics.circle("fill", x, ys[i], 34) endendThen add a sixth position to both tables and run it. A sixth circle appears and you changed no code.
Add a value to xs and not to ys. Run it.
The window turns blue. Copy the message and work out which table the loop was walking, and which one ran out.
Then do the opposite — add to ys and not xs. This one does not crash, and that is worse. Write down what you see and why.
Add a third parallel table and use it for something — radius, color, or both. Then pick one:
Back in the terminal:
local gappy = {"a", nil, "c"}
for i, v in ipairs(gappy) do print(i, v)end
print("#gappy is " .. #gappy)1 a#gappy is 1The "c" is in the table and the traversal never reached it. So did #gappy.
This is last session's warning with a loop attached: a table with a gap in it is not a list, and every tool that walks a list stops at the hole. Write down what this means for a table you build by deleting things out of the middle.
The exam traverses lists constantly, and it does it with the loop that has no counter — so a counter gets built by hand.
local crew = {"Ada", "Bo", "Cy"}
for i = 1, #crew do print(crew[i])endcrew ← ["Ada", "Bo", "Cy"]i ← 1
REPEAT LENGTH(crew) TIMES{ DISPLAY(crew[i]) i ← i + 1}| Lua | Exam notation | |
|---|---|---|
| How many elements | #crew | LENGTH(crew) |
| The first one | crew[1] | crew[1] |
| Walk the whole list | for i = 1, #crew do | REPEAT LENGTH(crew) TIMES |
ipairs on the exam. It is a Lua convenience, and a good one. Use it in your own programs and expect the indexed form on the exam.The index still starts at 1 in both, which is the agreement from session 18 doing real work. A traversal written in Lua and the same traversal in exam notation visit the same elements in the same order.
1-20-walk prints the same list two different ways1-20-sprites draws its circles from two parallel tablesAnswer the following questions before submitting your work.
xs crashed and a longer ys did not. Explain the difference, and say which of the two you would rather have happen in a program you were about to hand in.xs and ys belong together. Explain what actually connects them, and describe one way that could go wrong that Lua would never warn you about.Submit the required files to the appropriate dropbox.