Editor

Eat the dots

Pygame challenges · Maze Chase, step 3 of 4 · 25 XP

The player runs over the dots, but they never disappear and the score stays at 0.

Your task

A string cannot be changed in place: line[3] = " " raises a TypeError. To take a dot away, build a new row from three pieces, the part before the dot, a space and the part after it: line[:column] + " " + line[column + 1:].

Write eat_dot(grid, cell). If cell holds a dot, return a new grid with a space in its place, and True. Otherwise return the grid as it is, and False. Either way return two things, (new_grid, ate), where the new grid is a list of strings. Never change the list you were given: copy it with list(grid) and change the copy.

Why a copy? A function that quietly changes its argument surprises any code still holding the old list, while a function that only returns a new value is easy to test.

The game calls grid, ate = eat_dot(grid, player) after every step and adds 10 points when ate is True. You win when no row has a dot left. Two ghosts wander the maze from now on: three catches and the game is over.

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
eats a dotgrid = ["#...#", "#. .#"]eat_dot(grid, (2, 0))(["#. .#", "#. .#"], True)
an empty path: nothing to eatgrid = ["#...#", "#. .#"]eat_dot(grid, (2, 1))(["#...#", "#. .#"], False)
the last column of a rowgrid = ["...", "..."]eat_dot(grid, (2, 1))(["...", ".. "], True)
the first columngrid = ["...", "..."]eat_dot(grid, (0, 0))([" ..", "..."], True)
the grid given is unchangedbefore = ["#.#"] eat_dot(before, (1, 0))before["#.#"]
the same dot cannot be eaten twicegrid, ate = eat_dot(["..."], (1, 0)) grid, ate = eat_dot(grid, (1, 0))(grid, ate)([". ."], False)