Editor

Stay out of the walls

Pygame challenges · Maze Chase, step 1 of 4 · 15 XP

The player walks straight through the walls and off the edge of the screen.

Your task

The maze is a list of strings, one per row: "#" is a wall, "." a dot and " " an empty path. A cell is (column, row), counted from 0 at the top left. To read a cell, pick the row first and then the column: grid[row] is a string, and grid[row][column] is the character in it. Mixing up that order is the most common bug in grid games.

Write can_move(grid, cell). Return True if cell is inside the maze and is not a wall, else False. Inside means row is from 0 to len(grid) - 1 and column from 0 to len(grid[row]) - 1.

Check that the cell is inside before you read grid[row][column]. A row or column of -1 does not raise an error in Python: it quietly reads the last row or the last character, on the far side of the maze.

Each time the player is about to step into a cell, the game asks can_move(grid, cell) and stays put when the answer is False.

The function is at the top of the program, marked fix this. Press Run to play the game as it is, then Check when your function is ready.

What Check tests

CallShould give
a dotgrid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (1, 1))True
an empty pathgrid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (2, 1))True
a wallgrid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (2, 2))False
off the left edge (column -1)grid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (-1, 2))False
below the bottom rowgrid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (1, 4))False
the last column is insidegrid = ["#####", "#. .#", " .#. ", "#####"]can_move(grid, (4, 2))True