Bug hunt

Side Quest: The Copycat Map

* can repeat a list: [0] * 3 gives [0, 0, 0]. Handy!

A list of lists

A map is often a list of lists, with one inner list per row. grid[1][2] means "row 1, then column 2 of that row":

grid = [[0, 0, 0],
        [0, 0, 0]]
grid[1][2] = 7      # row 1, column 2
print(grid)         # [[0, 0, 0], [0, 0, 7]]

The trap

It's tempting to build a grid with [[0] * 3] * 2. But * copies the name tag, not the row. You get one row list with two tags on it, the same aliasing trap as The Backup That Wasn't. Change "one" row and they all change.

To get separate rows, make a new row each time round a loop:

grid = []
for r in range(2):
    grid.append([0] * 3)   # a brand-new row each time

[0] * 3 on its own is safe. Numbers can't change, so sharing them never causes trouble.

The bug

Pip's treasure map should show one treasure (9) and one rock (5). The program draws the map on Pip's pixel canvas: c.set(x, y, "gold") paints the square in column x, row y. But the picture shows a whole column of treasure and rocks!

Assignment

  1. Fix how grid is built, so each row is its own list.
  2. Keep width = 5, height = 4 and the drawing code.
  3. The map should show exactly one gold square and one stone square.