msn0981

Untitled

Jul 23rd, 2015
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. from queue import *
  2. from array import array
  3. path = Queue(maxsize = 0)
  4. realpath = []
  5. class MickeyMove():
  6. def __init__(self, map,x_start , y_start):
  7. print("create mickey maze")
  8. self.x_start = x_start
  9. self.y_start = y_start
  10. self.path = Queue(maxsize=0)
  11. self.maze_map = map
  12.  
  13. @classmethod
  14. def checkMove(self, x_nextPos, y_nextPos):
  15. maze = self.maze_map
  16. ch_lis = []
  17. N_x = x_nextPos+1
  18. N_y = y_nextPos
  19. N = []
  20. N.append(N_x)
  21. N.append(N_y)
  22. S_x = x_nextPos-1
  23. S_y = y_nextPos
  24. S = []
  25. S.append(S_x)
  26. S.append(S_y)
  27. E_x = x_nextPos
  28. E_y = y_nextPos+1
  29. E = []
  30. E.append(E_x)
  31. E.append(E_y)
  32. W_x = x_nextPos
  33. W_y = y_nextPos-1
  34. W = []
  35. W.append(W_x)
  36. W.append(W_y)
  37. if maze[x_nextPos+1][y_nextPos] == 0:
  38. ch_lis.append(N)
  39. if maze[x_nextPos-1][y_nextPos] == 0:
  40. ch_lis.append(S)
  41. if maze[x_nextPos][y_nextPos+1] == 0:
  42. ch_lis.append(E)
  43. if maze[x_nextPos][y_nextPos-1] == 0:
  44. ch_lis.append(W)
  45. return ch_lis
  46.  
  47. @classmethod
  48. def checkDeadend(self,x_move,y_move):
  49. maze = self.maze_map
  50. dead_list = []
  51. x_dead = x_move
  52. y_dead = y_move
  53. N = x_dead+1,y_dead
  54. S = x_dead-1,y_dead
  55. E = x_dead,y_dead+1
  56. W = x_dead,y_dead-1
  57. if maze[x_dead+1][y_dead] == 2:
  58. dead_list.append(2)
  59. if maze[x_dead-1][y_dead] == 2:
  60. dead_list.append(2)
  61. if maze[x_dead][y_dead+1] == 2:
  62. dead_list.append(2)
  63. if maze[x_dead][y_dead-1] == 2:
  64. dead_list.append(2)
  65. if dead_list.count(2) >= 3:
  66. back = path.get()
  67. lastfork = back[1]
  68. laststep = back[0]
  69. if lastfork == False:
  70. self.move(laststep[0][0],laststep[0][1])
  71. else:
  72. self.move(lastfork[0][0],lastfork[0][1])
  73. else:
  74. return
  75.  
  76. @classmethod
  77. def getMaze(self,val):
  78. self.maze_map = val
  79. @classmethod
  80. def getPath(self):
  81. self.path = Queue(maxsize = 0)
  82.  
  83. @classmethod
  84. def move(self, x_move, y_move):
  85. #print(x_move, y_move)
  86. maze = self.maze_map
  87. alist = []
  88. go = self.choose(x_move,y_move)
  89. alist.append(go)
  90. print("2.go:",go[0],go[1])
  91. self.checkDeadend(go[0],go[1])
  92. #print("go:",go)
  93. self.recStep(x_move,y_move,go)
  94. self.actStep(x_move, y_move)
  95. print("\n")
  96. self.move(go[0],go[1])
  97.  
  98. @classmethod
  99. def recStep(self, x_recmovestep, y_recmovestep, go):
  100. array = []
  101. array.append((x_recmovestep,y_recmovestep))
  102. array.append(self.checkMove(x_recmovestep,y_recmovestep))
  103. array.append(go)
  104. allfork = array[1]
  105. havemoved = array[2]
  106. print("array",array)
  107. print("remove",havemoved)
  108. if len(array) == 2:
  109. path.put(array)
  110. else:
  111. allfork.remove(havemoved)
  112. path.put(array)
  113. print ("3.rec",array)
  114.  
  115. @classmethod
  116. def actStep(self,x_moved,y_moved):
  117. paath = []
  118. paath.append(x_moved)
  119. paath.append(y_moved)
  120. realpath.append(paath)
  121. print("4.real",realpath)
  122. return realpath
  123.  
  124. @classmethod
  125. def choose(self,x_fork,y_fork):
  126. ways = self.checkMove(x_fork,y_fork)
  127. choosefork = self.random(ways)
  128. print(ways)
  129. if realpath.count(choosefork) == 1:
  130. self.choose(x_fork,y_fork)
  131. else:
  132. #print("1.chofork:",choosefork)
  133. return choosefork
  134.  
  135. def setMaze(self,maze):
  136. self.maze_map = maze
  137.  
  138. @classmethod
  139. def random(self,list):
  140. from random import choice
  141. print(choice(list))
  142. return choice(list)
  143.  
  144. @classmethod
  145. def checkGoal(self,x_goal,y_goal):
  146. maze = self.maze_map
  147. x_Mazeboun = len(maze[0])
  148. y_Mazeboun = len(maze)
  149. if maze[x_goal][y_Mazeboun-1] == 0 or maze[x_goal][0] ==0 or maze[0][y_goal] == 0 or maze[x_Mazeboun-1][y_goal] == 0:
  150. return True
  151. else:
  152. return False
  153.  
  154. @classmethod
  155. def printMaze(self, maze_map):
  156. print("in maze")
  157. for row in maze_map:
  158. for val in row:
  159. print(val,end=" ")
  160. print(end="\n")
  161.  
  162.  
  163.  
  164. def main():
  165. maze =[
  166. [2, 2, 2, 2, 2, 2, 2],
  167. [2, 0, 0, 0, 0, 0, 2],
  168. [2, 0, 2, 0, 2, 0, 2],
  169. [2, 0, 0, 0, 0, 2, 2],
  170. [2, 2, 0, 2, 0, 2, 2],
  171. [2, 0, 0, 0, 0, 0, 0],
  172. [2, 2, 2, 2, 2, 2, 2]]
  173.  
  174. m = MickeyMove(maze, 1,1)
  175. m.getMaze(maze)
  176. m.move(1,1)
  177. if __name__ == "__main__":
  178. Si = 1
  179. Sj = 1
  180. main()
Advertisement
Add Comment
Please, Sign In to add comment