Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- #generate a grid
- grid_width = 10
- grid_height = 10
- grid_data = dict()
- print_cache = dict()
- total_grid_size = grid_height * grid_width
- #pick a random number between 2 and the number of grids cells
- total_filled_cells = random.randint(2, total_grid_size)
- filled_cell_list = random.sample(range(total_grid_size), total_filled_cells)
- #display the grid for us to look at
- cell_num = 0
- for y in range(grid_height):
- grid_data[y] = dict()
- for x in range(grid_width):
- if cell_num in filled_cell_list:
- grid_data[y][x] = True
- print("x", end =" "),
- else:
- grid_data[y][x] = False
- print("o", end =" "),
- cell_num += 1
- print("")
- #show the data struct that we have
- #print(grid_data)
- #find all the connected grids
- test_cell_num = 0
- for y in range(grid_height):
- for x in range(grid_width):
- #only test the points we already care about
- if grid_data.get(y).get(x) is False:
- test_cell_num += 1
- continue
- my_loc = [y, x]
- # make a list of who to test
- test_list = list()
- if x > 0: # add test for to the left
- test_list.append([y, x-1])
- if x < grid_width-1: # add test for to the right
- test_list.append([y, x+1])
- if y > 0: # add test for above
- test_list.append([y-1, x])
- if y < grid_height-1: # add test for below
- test_list.append([y+1, x])
- #print the points we want to test
- #print("{} -> {}".format(my_loc, test_list))
- for test_point in test_list:
- if print_cache.get('{},{} -> {},{}'.format(y, x, test_point[0], test_point[1])) is None and grid_data.get(test_point[0]).get(test_point[1]) is True:
- #write the current direction and the alt direction to the print cache so we dont see it again we probly dont need current direction just tossed it in
- print_cache['{},{} -> {},{}'.format(test_point[0], test_point[1], y, x)] = True
- print_cache['{},{} -> {},{}'.format(y, x, test_point[0], test_point[1])] = True
- print("{} is touching {}".format(my_loc, test_point))
- test_cell_num += 1
- #print(print_cache)
Add Comment
Please, Sign In to add comment