| 47 | |
| 48 | |
| 49 | def 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 | |
| 75 | def iterativeFloodFill(field, startx, starty, newChar): |