Guest User

Untitled

a guest
Apr 24th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. import turtle
  2.  
  3. PART_OF_PATH = 'O'
  4. TRIED = '.'
  5. OBSTACLE = '+'
  6. DEAD_END = '-'
  7.  
  8. class Maze:
  9. def __init__(self, maze_file_name):
  10. rows_in_maze = 0
  11. columns_in_maze = 0
  12. self.maze_list = []
  13. maze_file = open(maze_file_name,'r')
  14. rows_in_maze = 0
  15. for line in maze_file:
  16. row_list = []
  17. col = 0
  18. for ch in line[: -1]:
  19. row_list.append(ch)
  20. if ch == 'S':
  21. self.start_row = rows_in_maze
  22. self.start_col = col
  23. col = col + 1
  24. rows_in_maze = rows_in_maze + 1
  25. self.maze_list.append(row_list)
  26. columns_in_maze = len(row_list)
  27.  
  28. self.rows_in_maze = rows_in_maze
  29. self.columns_in_maze = columns_in_maze
  30. self.x_translate = - columns_in_maze / 2
  31. self.y_translate = rows_in_maze / 2
  32. self.t = turtle.Turtle()
  33. self.t.shape('turtle')
  34. self.wn = turtle.Screen()
  35. self.wn.setworldcoordinates(- (columns_in_maze - 1) / 2 - .5,
  36. - (rows_in_maze - 1) / 2 - .5,
  37. (columns_in_maze - 1) / 2 + .5,
  38. (rows_in_maze - 1) / 2 + .5)
  39.  
  40. def draw_maze(self):
  41. self.t.speed(10)
  42. for y in range(self.rows_in_maze):
  43. for x in range(self.columns_in_maze):
  44. if self.maze_list[y][x] == OBSTACLE:
  45. self.draw_centered_box(x + self.x_translate,
  46. - y + self.y_translate, 'orange')
  47. self.t.color('black')
  48. self.t.fillcolor('blue')
  49.  
  50. def draw_centered_box(self, x, y, color):
  51. self.t.up()
  52. self.t.goto(x - .5, y - .5)
  53. self.t.color(color)
  54. self.t.fillcolor(color)
  55. self.t.setheading(90)
  56. self.t.down()
  57. self.t.begin_fill()
  58. for i in range(4):
  59. self.t.forward(1)
  60. self.t.right(90)
  61. self.t.end_fill()
  62.  
  63. def move_turtle(self, x, y):
  64. self.t.up()
  65. self.t.setheading(self.t.towards(x + self.x_translate,
  66. - y + self.y_translate))
  67. self.t.goto(x + self.x_translate, - y + self.y_translate)
  68.  
  69. def drop_bread_crumb(self, color):
  70. self.t.dot(10, color)
  71.  
  72. def update_position(self, row, col, val=None):
  73. if val:
  74. self.maze_list[row][col] = val
  75. self.move_turtle(col, row)
  76.  
  77. if val == PART_OF_PATH:
  78. color = 'green'
  79. elif val == OBSTACLE:
  80. color = 'red'
  81. elif val == TRIED:
  82. color = 'black'
  83. elif val == DEAD_END:
  84. color = 'red'
  85. else:
  86. color = None
  87.  
  88. if color:
  89. self.drop_bread_crumb(color)
  90.  
  91. def is_exit(self, row, col):
  92. return (row == 0 or
  93. row == self.rows_in_maze - 1 or
  94. col == 0 or
  95. col == self.columns_in_maze - 1)
  96.  
  97. def __getitem__(self,idx):
  98. return self.maze_list[idx]
  99.  
  100. def search_from(maze, start_row, start_column):
  101. # try each of four directions from this point until we find a
  102. # way out.
  103. # base Case return values:
  104. # 1. We have run into an obstacle, return false
  105. maze.update_position(start_row, start_column)
  106. if maze[start_row][start_column] == OBSTACLE :
  107. return False
  108. # 2. We have found a square that has already been explored
  109. if maze[start_row][start_column] == TRIED or \
  110. maze[start_row][start_column] == DEAD_END:
  111. return False
  112. # 3. We have found an outside edge not occupied by an obstacle
  113. if maze.is_exit(start_row,start_column):
  114. maze.update_position(start_row, start_column, PART_OF_PATH)
  115. return True
  116. maze.update_position(start_row, start_column, TRIED)
  117. # Otherwise, use logical short circuiting to try each direction
  118. # in turn (if needed)
  119. found = search_from(maze, start_row-1, start_column) or \
  120. search_from(maze, start_row+1, start_column) or \
  121. search_from(maze, start_row, start_column-1) or \
  122. search_from(maze, start_row, start_column+1)
  123. if found:
  124. maze.update_position(start_row, start_column, PART_OF_PATH)
  125. else:
  126. maze.update_position(start_row, start_column, DEAD_END)
  127. return found
  128.  
  129. my_maze = Maze('maze2.txt')
  130. my_maze.draw_maze()
  131. my_maze.update_position(my_maze.start_row, my_maze.start_col)
  132. search_from(my_maze, my_maze.start_row, my_maze.start_col)
Add Comment
Please, Sign In to add comment