MCPcopy Create free account
hub / github.com/asweigart/PythonStdioGames / iterativeFloodFill

Function iterativeFloodFill

inprogress/floodfill.py:75–96  ·  view source on GitHub ↗
(field, startx, starty, newChar)

Source from the content-addressed store, hash-verified

73
74
75def iterativeFloodFill(field, startx, starty, newChar):
76 curChar = field[(startx, starty)]
77
78 coordinatesToChange = [(startx, starty)]
79
80 while len(coordinatesToChange) > 0:
81 x, y = coordinatesToChange.pop()
82 field[(x, y)] = newChar # Change the character.
83
84 # Change the neighboring characters:
85 if y + 1 < HEIGHT and field[(x, y + 1)] == curChar:
86 # Change the character for the southern neighbor:
87 coordinatesToChange.append((x, y + 1))
88 if y - 1 >= 0 and field[(x, y - 1)] == curChar:
89 # Change the character for the northern neighbor:
90 coordinatesToChange.append((x, y - 1))
91 if x + 1 < WIDTH and field[(x + 1, y)] == curChar:
92 # Change the character for the eastern neighbor:
93 coordinatesToChange.append((x + 1, y))
94 if x - 1 >= 0 and field[(x - 1, y)] == curChar:
95 # Change the character for the western neighbor:
96 coordinatesToChange.append((x - 1, y))
97
98
99def printField(field):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected