Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.56 KB | None | 0 0
  1. from modules.searching_framework import Problem, breadth_first_graph_search, astar_search, uniform_cost_search
  2. from typing import List, Tuple
  3. from enum import Enum
  4. import sys
  5. import bisect
  6.  
  7.  
  8. class Direction(Enum):
  9. UP = 1
  10. RIGHT = 2
  11. DOWN = 3
  12. LEFT = 4
  13.  
  14.  
  15. class Snake:
  16. def __init__(self, s, red: List[Tuple[int, int]], green: List[Tuple[int, int]], direction: Direction, head,
  17. steps_till_here: int):
  18. self.snake = s
  19. self.red = red
  20. self.green = green
  21. self.direction = direction
  22. self.head = head
  23. self.steps_till_here = steps_till_here
  24.  
  25. @classmethod
  26. def from_self(cls, other: 'Snake'):
  27. snake_copy = other.snake[:]
  28. red = other.red[:]
  29. green = other.green[:]
  30. return Snake(snake_copy, red, green, other.direction, other.head, other.steps_till_here)
  31.  
  32. def change_direction_left(self):
  33. if self.direction == Direction.UP:
  34. self.direction = Direction.LEFT
  35. if self.direction == Direction.LEFT:
  36. self.direction = Direction.DOWN
  37. if self.direction == Direction.DOWN:
  38. self.direction = Direction.RIGHT
  39. if self.direction == Direction.RIGHT:
  40. self.direction = Direction.UP
  41.  
  42. def change_direction_right(self):
  43. if self.direction == Direction.UP:
  44. self.direction = Direction.RIGHT
  45. if self.direction == Direction.RIGHT:
  46. self.direction = Direction.DOWN
  47. if self.direction == Direction.DOWN:
  48. self.direction = Direction.LEFT
  49. if self.direction == Direction.LEFT:
  50. self.direction = Direction.UP
  51.  
  52. def check_obstacles_hit(self, prospect_head):
  53. if not (0, 0) <= prospect_head < (10, 10):
  54. return True
  55. if prospect_head in self.snake[:-1]:
  56. return True
  57. if prospect_head in self.red:
  58. return True
  59. return False
  60.  
  61. def peek_straight(self):
  62. if self.direction == Direction.UP:
  63. self.head = (self.snake[-1][0] - 1, self.snake[-1][1])
  64. return self.check_obstacles_hit(self.head)
  65. if self.direction == Direction.DOWN:
  66. self.head = (self.snake[-1][0] + 1, self.snake[-1][1])
  67. return self.check_obstacles_hit(self.head)
  68. if self.direction == Direction.RIGHT:
  69. self.head = (self.snake[-1][0], self.snake[-1][1] + 1)
  70. return self.check_obstacles_hit(self.head)
  71. if self.direction == Direction.LEFT:
  72. self.head = (self.snake[-1][0], self.snake[-1][1] - 1)
  73. return self.check_obstacles_hit(self.head)
  74.  
  75. def peek_left(self):
  76. if self.direction == Direction.UP:
  77. self.head = (self.snake[-1][0], self.snake[-1][1] - 1)
  78. return self.check_obstacles_hit(self.head)
  79. if self.direction == Direction.DOWN:
  80. self.head = (self.snake[-1][0], self.snake[-1][1] + 1)
  81. return self.check_obstacles_hit(self.head)
  82. if self.direction == Direction.RIGHT:
  83. self.head = (self.snake[-1][0] - 1, self.snake[-1][1])
  84. return self.check_obstacles_hit(self.head)
  85. if self.direction == Direction.LEFT:
  86. self.head = (self.snake[-1][0] + 1, self.snake[-1][1])
  87. return self.check_obstacles_hit(self.head)
  88.  
  89. def peek_right(self):
  90. if self.direction == Direction.UP:
  91. self.head = (self.snake[-1][0], self.snake[-1][1] + 1)
  92. return self.check_obstacles_hit(self.head)
  93. if self.direction == Direction.DOWN:
  94. self.head = (self.snake[-1][0], self.snake[-1][1] - 1)
  95. return self.check_obstacles_hit(self.head)
  96. if self.direction == Direction.RIGHT:
  97. self.head = (self.snake[-1][0] + 1, self.snake[-1][1])
  98. return self.check_obstacles_hit(self.head)
  99. if self.direction == Direction.LEFT:
  100. self.head = (self.snake[-1][0] - 1, self.snake[-1][1])
  101. return self.check_obstacles_hit(self.head)
  102.  
  103. def green_eaten(self):
  104. for g in self.green:
  105. if g == self.head:
  106. self.green.remove(g)
  107. return True
  108. return False
  109.  
  110. def move(self):
  111. if self.green_eaten():
  112. new_snake = self.snake[0:]
  113. else:
  114. new_snake = self.snake[1:]
  115. new_snake.append(self.head)
  116. self.snake = new_snake
  117. self.steps_till_here += 1
  118.  
  119. def all_green_eaten(self):
  120. return len(self.green) == 2 # 0
  121.  
  122. def score(self):
  123. return self.steps_till_here
  124.  
  125. def get_snake_head(self):
  126. return self.snake[-1]
  127.  
  128. def __eq__(self, other):
  129. return isinstance(other, Snake) and \
  130. self.snake == other.snake and \
  131. self.green == other.green and \
  132. self.direction == other.direction and \
  133. self.head == other.head and \
  134. self.steps_till_here == other.steps_till_here
  135.  
  136. def __lt__(self, other):
  137. return self.score() < other.score()
  138.  
  139. def __gt__(self, other):
  140. return self.score() > other.score()
  141.  
  142. def __hash__(self):
  143. return hash(self.score())
  144.  
  145.  
  146. class SnakeProblem(Problem):
  147.  
  148. def __init__(self, initial: Snake):
  149. super().__init__(initial, None)
  150.  
  151. def goal_test(self, state):
  152. return state.all_green_eaten()
  153.  
  154. def successor(self, state):
  155. successors = dict()
  156.  
  157. straight = Snake.from_self(state)
  158. if not straight.peek_straight():
  159. straight.move()
  160. successors['ContinueStraight'] = straight
  161.  
  162. left = Snake.from_self(state)
  163. if not left.peek_left():
  164. left.move()
  165. left.change_direction_left()
  166. successors['TurnLeft'] = left
  167.  
  168. right = Snake.from_self(state)
  169. if not right.peek_right():
  170. right.move()
  171. right.change_direction_right()
  172. successors['TurnRight'] = right
  173.  
  174. return successors
  175.  
  176. def actions(self, state):
  177. return self.successor(state).keys()
  178.  
  179. def result(self, state, action):
  180. possible = self.successor(state)
  181. return possible[action]
  182.  
  183. def value(self):
  184. pass
  185.  
  186.  
  187. if __name__ == '__main__':
  188. n = int(input())
  189. green_apples = [tuple(map(int, input().split(','))) for _ in range(n)]
  190. m = int(input())
  191. red_apples = [tuple(map(int, input().split(','))) for _ in range(m)]
  192.  
  193. snake = Snake([(0, 0), (2, 0), (3, 0)], red_apples, green_apples, Direction.DOWN, (3, 0), 0)
  194. snakeProblem = SnakeProblem(snake)
  195.  
  196. answer = breadth_first_graph_search(snakeProblem)
  197. # answer = uniform_cost_search(snakeProblem)
  198. print(answer.solution())
  199. print(answer.solve().get_snake_head())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement