Guest User

Untitled

a guest
Nov 15th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. import random
  2.  
  3. #generate a grid
  4. grid_width = 10
  5. grid_height = 10
  6. grid_data = dict()
  7. print_cache = dict()
  8. total_grid_size = grid_height * grid_width
  9.  
  10. #pick a random number between 2 and the number of grids cells
  11. total_filled_cells = random.randint(2, total_grid_size)
  12. filled_cell_list = random.sample(range(total_grid_size), total_filled_cells)
  13.  
  14. #display the grid for us to look at
  15. cell_num = 0
  16. for y in range(grid_height):
  17. grid_data[y] = dict()
  18. for x in range(grid_width):
  19. if cell_num in filled_cell_list:
  20. grid_data[y][x] = True
  21. print("x", end =" "),
  22. else:
  23. grid_data[y][x] = False
  24. print("o", end =" "),
  25.  
  26. cell_num += 1
  27. print("")
  28.  
  29. #show the data struct that we have
  30. #print(grid_data)
  31.  
  32. #find all the connected grids
  33. test_cell_num = 0
  34. for y in range(grid_height):
  35. for x in range(grid_width):
  36.  
  37. #only test the points we already care about
  38. if grid_data.get(y).get(x) is False:
  39. test_cell_num += 1
  40. continue
  41.  
  42. my_loc = [y, x]
  43.  
  44. # make a list of who to test
  45. test_list = list()
  46. if x > 0: # add test for to the left
  47. test_list.append([y, x-1])
  48. if x < grid_width-1: # add test for to the right
  49. test_list.append([y, x+1])
  50. if y > 0: # add test for above
  51. test_list.append([y-1, x])
  52. if y < grid_height-1: # add test for below
  53. test_list.append([y+1, x])
  54.  
  55. #print the points we want to test
  56. #print("{} -> {}".format(my_loc, test_list))
  57.  
  58. for test_point in test_list:
  59. 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:
  60.  
  61. #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
  62. print_cache['{},{} -> {},{}'.format(test_point[0], test_point[1], y, x)] = True
  63. print_cache['{},{} -> {},{}'.format(y, x, test_point[0], test_point[1])] = True
  64.  
  65. print("{} is touching {}".format(my_loc, test_point))
  66.  
  67. test_cell_num += 1
  68.  
  69. #print(print_cache)
Add Comment
Please, Sign In to add comment