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

Function recursiveFloodFill

inprogress/floodfill.py:49–72  ·  view source on GitHub ↗
(field, x, y, newChar, curChar=None)

Source from the content-addressed store, hash-verified

47
48
49def recursiveFloodFill(field, x, y, newChar, curChar=None):
50 if curChar == None:
51 # If the current character isn't given, use the char at field[(x, y)].
52 curChar = field[(x, y)]
53
54 # Base case; just return:
55 if curChar == newChar or field[(x, y)] != curChar:
56 return
57
58 field[(x, y)] = newChar # Change the character.
59
60 # Recursive case; change the neighboring characters:
61 if y + 1 < HEIGHT and field[(x, y + 1)] == curChar:
62 # Call recursiveFloodFill() on the southern neighbor:
63 recursiveFloodFill(field, x, y + 1, newChar, curChar)
64 if y - 1 >= 0 and field[(x, y - 1)] == curChar:
65 # Call recursiveFloodFill() on the northern neighbor:
66 recursiveFloodFill(field, x, y - 1, newChar, curChar)
67 if x + 1 < WIDTH and field[(x + 1, y)] == curChar:
68 # Call recursiveFloodFill() on the eastern neighbor:
69 recursiveFloodFill(field, x + 1, y, newChar, curChar)
70 if x - 1 >= 0 and field[(x - 1, y)] == curChar:
71 # Call recursiveFloodFill() on the western neighbor:
72 recursiveFloodFill(field, x - 1, y, newChar, curChar)
73
74
75def iterativeFloodFill(field, startx, starty, newChar):

Callers 1

floodfill.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected