Skip to content

Instantly share code, notes, and snippets.

@laserbat
Created July 16, 2011 12:43
Show Gist options
  • Save laserbat/1086321 to your computer and use it in GitHub Desktop.
Save laserbat/1086321 to your computer and use it in GitHub Desktop.
def flood(self, x, y, old, new):
seed_pos = (x, y)
if old == new:
return
stack = []
w, h = MAP_W, MAP_H
max_x = max_y = 0
min_x = w
min_y = h
stack.append(seed_pos)
iterations = 0
while(stack):
iterations += 1
x, y = stack.pop()
y1 = y
while y1 >= 0 and self.gamemap[x][y1].fval == old:
y1 -= 1
y1 += 1
spanLeft = spanRight = False
inner_iterations = 0
while(y1 < h and self.gamemap[x][y1].fval == old):
inner_iterations += 1
self.gamemap[x][y1].fval = new
min_x = min(min_x, x)
max_x = max(max_x, x)
min_y = min(min_y, y1)
max_y = max(max_y, y1)
if not spanLeft and x > 0 and self.gamemap[x - 1]\
[y1].fval == old:
stack.append((x - 1, y1))
spanLeft = True
elif spanLeft and x > 0 and self.gamemap[x - 1]\
[y1].fval != old:
spanLeft = False
if not spanRight and x < w - 1 and self.gamemap[x + 1]\
[y1].fval == old:
stack.append((x + 1, y1))
spanRight = True
elif spanRight and x < w - 1 and self.gamemap\
[x + 1][y1].fval != old:
spanRight = False
y1 += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment