Change the color/shape of a tile using the recursive flood fill algorithm.
(tileType, board, x, y, charToChange=None)
| 182 | |
| 183 | |
| 184 | def changeTile(tileType, board, x, y, charToChange=None): |
| 185 | """Change the color/shape of a tile using the recursive flood fill |
| 186 | algorithm.""" |
| 187 | if x == 0 and y == 0: |
| 188 | charToChange = board[(x, y)] |
| 189 | if tileType == charToChange: |
| 190 | return # Base Case: Already is the same tile. |
| 191 | |
| 192 | board[(x, y)] = tileType |
| 193 | |
| 194 | if x > 0 and board[(x - 1, y)] == charToChange: |
| 195 | # Recursive Case: Change the left neighbor's tile: |
| 196 | changeTile(tileType, board, x - 1, y, charToChange) |
| 197 | if y > 0 and board[(x, y - 1)] == charToChange: |
| 198 | # Recursive Case: Change the top neighbor's tile: |
| 199 | changeTile(tileType, board, x, y - 1, charToChange) |
| 200 | if x < BOARD_WIDTH - 1 and board[(x + 1, y)] == charToChange: |
| 201 | # Recursive Case: Change the right neighbor's tile: |
| 202 | changeTile(tileType, board, x + 1, y, charToChange) |
| 203 | if y < BOARD_HEIGHT - 1 and board[(x, y + 1)] == charToChange: |
| 204 | # Recursive Case: Change the bottom neighbor's tile: |
| 205 | changeTile(tileType, board, x, y + 1, charToChange) |
| 206 | |
| 207 | |
| 208 | def hasWon(board): |