Skip to content

Instantly share code, notes, and snippets.

@lvngd
Created October 11, 2020 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lvngd/c80cc821dbf9eb97595d3ae8769c1d0b to your computer and use it in GitHub Desktop.
Save lvngd/c80cc821dbf9eb97595d3ae8769c1d0b to your computer and use it in GitHub Desktop.
import random
m =[[2,3,3,3,3,3,4,4,4,4,4,4],
[2,3,3,3,3,3,4,4,4,4,4,4],
[3,3,3,1,3,3,4,1,4,4,0,4],
[3,3,1,1,1,3,1,1,1,4,4,4],
[3,1,1,1,1,1,1,1,1,1,4,4],
[3,1,1,0,0,0,0,0,1,1,4,4],
[3,1,1,0,0,0,0,0,1,1,4,4],
[2,1,1,1,1,0,0,0,1,1,2,2],
[2,1,1,1,0,0,0,0,1,2,2,2],
[2,2,1,1,1,0,0,2,2,2,1,0],
[2,2,2,1,1,0,0,2,2,2,1,1],
[2,2,2,2,2,2,2,2,2,0,1,0]
]
def flood_recursive(matrix):
width = len(matrix)
height = len(matrix[0])
def fill(x,y,start_color,color_to_update):
#if the square is not the same color as the starting point
if matrix[x][y] != start_color:
return
#if the square is not the new color
elif matrix[x][y] == color_to_update:
return
else:
#update the color of the current square to the replacement color
matrix[x][y] = color_to_update
neighbors = [(x-1,y),(x+1,y),(x-1,y-1),(x+1,y+1),(x-1,y+1),(x+1,y-1),(x,y-1),(x,y+1)]
for n in neighbors:
if 0 <= n[0] <= width-1 and 0 <= n[1] <= height-1:
fill(n[0],n[1],start_color,color_to_update)
start_x = random.randint(0,width-1)
start_y = random.randint(0,height-1)
start_color = matrix[start_x][start_y]
fill(start_x,start_y,start_color,9)
return matrix
flood_recursive(m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment