Comet sketches Pip on a cell with four arrows around it. "Next week Pip drives," she says. "From any cell, where can it go?"
Wren adds a column of numbers to his clipboard. "What does the evidence say?" he asks. "Some arrows hit rocks, and some fall off the edge. We should trace every one."
Nova projects one cell glowing, with its four neighbors outlined. "How can I help?" she asks. "Up, down, left and right. Ask is_open about each one."
"Lead programmer," Comet says, "paper first. Then we run it."
A cell's neighbors are up, down, left and right. Up is row - 1 and down is row + 1. Left is col - 1 and right is col + 1.
Some neighbors are rocks and some are off the edge. is_open checks both. The field is made-up data from the FieldSim story.
| Neighbor of (2, 2) | Cell | In bounds? | Mark | Open? |
|---|---|---|---|---|
| up | (1, 2) | yes | . | yes |
| down | (3, 2) | yes | . | yes |
| left | (2, 1) | yes | . | yes |
| right | (2, 3) | yes | # | no |
# neighbors.py
from field_data import FIELD
from grid import FieldGrid
def open_neighbors(grid, row, col):
"""Precondition: (row, col) is in bounds.
Postcondition: gives back the open cells next to it, up, down, left, right."""
found = []
for d_row, d_col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
r = row + d_row
c = col + d_col
if grid.is_open(r, c):
found.append((r, c))
return found
grid = FieldGrid(FIELD)
print(open_neighbors(grid, 0, 0))
print(open_neighbors(grid, 2, 2))
print(open_neighbors(grid, 4, 5))
assert open_neighbors(grid, 0, 0) == [(1, 0), (0, 1)]
assert len(open_neighbors(grid, 2, 2)) == 3
print("Neighbor tests passed") [(1, 0), (0, 1)] [(1, 2), (3, 2), (2, 1)] [(3, 5), (4, 4)] Neighbor tests passed
To total a column, swap the loops: the outer loop picks a column, and the inner loop walks down the rows. The cell is still read row first.
# column_counts.py
from field_data import FIELD
from grid import FieldGrid
grid = FieldGrid(FIELD)
for c in range(grid.width()):
marked = 0
for r in range(grid.height()):
if grid.cell(r, c) != ".":
marked += 1
print("Column", c, "marked:", marked) Column 0 marked: 1 Column 1 marked: 1 Column 2 marked: 2 Column 3 marked: 1 Column 4 marked: 1 Column 5 marked: 2
| Statement | True or false? |
|---|---|
| In column_counts.py the outer loop walks the columns. | ? |
| grid.cell(r, c) still names the row first. | ? |
| A corner cell can have four open neighbors. | ? |
| The assert lines in neighbors.py are tests. | ? |
Excellent tracing. Tomorrow is a Mission Quest on graph paper.