Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.78 KB | None | 0 0
  1. from util.executor_exceptions import SolutionDeathError
  2. from challenge import Challenge
  3.  
  4. CONSOLE = None
  5.  
  6. class Pig:
  7. def __init__(self, identifier):
  8. self._identifier = identifier
  9. self.journey_length = 0
  10.  
  11. class PigFactory:
  12. def __init__(self):
  13. self.curr_pig = 1
  14.  
  15. def gen_pig(self):
  16. pig = Pig(self.curr_pig)
  17. self.curr_pig += 1
  18. return pig
  19.  
  20. pigfactory = PigFactory()
  21.  
  22. class Farm:
  23. def __init__(self, pigs_held, desired_pigs, distance):
  24. self._pigs = self._gen_pigs(pigs_held)
  25. self._desired_pigs = desired_pigs
  26. self.distance_to_next = distance
  27.  
  28. def _gen_pigs(self, pigs_held):
  29. pig_list = []
  30.  
  31. for i in range(pigs_held):
  32. pig_list.append(pigfactory.gen_pig())
  33.  
  34. return pig_list
  35.  
  36. def get_order(self):
  37. return len(self._pigs) - self._desired_pigs
  38.  
  39. def get_pig_count(self):
  40. CONSOLE.log("Count: {}".format(len(self._pigs)))
  41. return len(self._pigs)
  42.  
  43. def unload_pig(self):
  44. return self._pigs.pop(0) if len(self._pigs) > 0 else None
  45.  
  46. def load_pig(self, pig):
  47. self._pigs.append(pig)
  48.  
  49. def are_pigs_too_old(self):
  50. return len([pig for pig in self._pigs if pig.journey_length >= 3]) > 0
  51.  
  52.  
  53. class RoadMap:
  54. def __init__(self, farm_list):
  55. self.farms = []
  56. self.counter = 0
  57.  
  58. for farm_data in farm_list:
  59. self.farms.append(self._gen_farm(farm_data))
  60.  
  61. def _gen_farm(self, farm_data):
  62. farm = Farm(farm_data['pigs_held'], farm_data['desired_pigs'], farm_data['distance_to_next'])
  63. return farm
  64.  
  65. def next_farm(self):
  66. CONSOLE.log("Next farm called")
  67. self.counter += 1
  68. return self.farms[self.counter]
  69.  
  70. def get_farm(self, index):
  71. return self.farms[index]
  72.  
  73. def first_farm(self): # Replace this with get_farm(0)
  74. return self.farms[0]
  75.  
  76. def has_next(self):
  77. return self.counter + 1 < len(self.farms)
  78.  
  79. class Truck:
  80. def __init__(self, capacity):
  81. self._pigs = []
  82. self.capacity = capacity
  83.  
  84. def load_pig(self, pig):
  85. if len(self._pigs) < self.capacity:
  86. self._pigs.append(pig)
  87.  
  88. def unload_pig(self, identifier):
  89. pig = self.get_pig(identifier)
  90. if pig is not None:
  91. self._pigs.remove(pig)
  92. return pig
  93. else:
  94. return None
  95.  
  96. def get_pig(self, identifier):
  97. filtered = [pig for pig in self._pigs if pig._identifier == identifier]
  98. if(len(filtered) > 0):
  99. return filtered[0]
  100. else:
  101. return None
  102.  
  103. def increase_pig_journey_length(self, delta):
  104. for pig in self._pigs:
  105. pig.journey_length += delta
  106.  
  107. class GameManager:
  108. def __init__(self, truck, roadmap):
  109. self.truck = truck
  110. self.roadmap = roadmap
  111. self.curr_farm = self.roadmap.first_farm()
  112.  
  113. def get_truck_capacity(self):
  114. return self.truck.capacity
  115.  
  116. def get_farm(self, index):
  117. return self.roadmap.get_farm(index)
  118.  
  119. def unload_pig(self, identifier):
  120. pig = self.truck.unload_pig(identifier)
  121. if pig is not None:
  122. self.curr_farm.load_pig(pig)
  123.  
  124. def get_farm_order(self):
  125. return self.curr_farm.get_order()
  126.  
  127. def load_pig(self):
  128. pig = self.curr_farm.unload_pig()
  129. self.truck.load_pig(pig)
  130. return pig._identifier
  131.  
  132. def get_pig_trip_length(self, identifier):
  133. pig = self.truck.get_pig(identifier)
  134. return pig.journey_length if pig is not None else -1
  135.  
  136. def get_next_farm_distance(self):
  137. return self.curr_farm.distance_to_next
  138.  
  139. def update(self):
  140. distance = self.curr_farm.distance_to_next
  141. self.truck.increase_pig_journey_length(distance)
  142. self.curr_farm = self.roadmap.next_farm()
  143.  
  144. def is_finished(self):
  145. return not self.roadmap.has_next()
  146.  
  147. def get_score_for_curr_farm(self):
  148. score = 0
  149. if self.curr_farm.get_order() == 0:
  150. score += 1
  151. if not self.curr_farm.are_pigs_too_old():
  152. score += 1
  153. return score
  154.  
  155.  
  156. class ChallengeImpl(Challenge):
  157. def __init__(self, config, canvas, console, solutions, hidden):
  158. self.canvas = canvas
  159. self.solution = solutions[0]
  160. self.hidden = hidden
  161. self.height = 16
  162. self.width = 32
  163. self.counter = 0
  164.  
  165. global CONSOLE
  166. CONSOLE = console
  167.  
  168. roadmap = RoadMap(config['farms'])
  169. truck = Truck(config['capacity'])
  170.  
  171. self.gamemanager = GameManager(truck, roadmap)
  172.  
  173. self.farm_scores = []
  174. self._driving = False
  175.  
  176. self._draw_background()
  177. self._sprite = self.canvas.new_bitmap(0.452, 0.816, "Truck.png", x = 2, y = 1.5, rotation=90, z_index=200)
  178. self._farm_counters = [
  179. self.canvas.new_text("0", 1.5, 'black', x=1.5, y=1.5, text_align='center', scale_x=1, scale_y=1, opacity=1, rotation=0, z_index=0),
  180. self.canvas.new_text("0", 1.5, 'black', x=29.5, y=1.5, text_align='center', scale_x=1, scale_y=1, opacity=1, rotation=0, z_index=0),
  181. self.canvas.new_text("0", 1.5, 'black', x=29.5, y=14.5, text_align='center', scale_x=1, scale_y=1, opacity=1, rotation=0, z_index=0),
  182. self.canvas.new_text("0", 1.5, 'black', x=1.5, y=14.5, text_align='center', scale_x=1, scale_y=1, opacity=1, rotation=0, z_index=0),
  183. self.canvas.new_text("0", 1.5, 'black', x=15.5, y=8, text_align='center', scale_x=1, scale_y=1, opacity=1, rotation=0, z_index=0)
  184. ]
  185.  
  186. def _update_sprite(self):
  187. next_farm = self.farm_sprites[0]
  188.  
  189. curr_y = self._sprite.get_y()
  190. curr_x = self._sprite.get_x()
  191.  
  192. dest_y = next_farm.get_y()
  193. dest_x = next_farm.get_x()
  194.  
  195. if (curr_y == dest_y) and (dest_x == curr_x):
  196. self._driving = False
  197. if len(self.farm_sprites) > 0: self.farm_sprites.pop(0)
  198. else:
  199. if(abs(dest_y - curr_y) < 1 and abs(dest_y - curr_y) > 0):
  200. self._sprite.move(y = dest_y - curr_y)
  201. elif(abs(dest_x - curr_x) < 1 and abs(dest_x - curr_x) > 0):
  202. self._sprite.move(x = dest_x - curr_x)
  203. else:
  204. if dest_y > curr_y:
  205. self._sprite.set_rotation(180)
  206. self._sprite.move(y = 1)
  207. elif dest_y < curr_y:
  208. self._sprite.set_rotation(0)
  209. self._sprite.move(y= - 1)
  210. elif dest_x > curr_x:
  211. self._sprite.set_rotation(90)
  212. self._sprite.move(x= 1)
  213. elif dest_x < curr_x:
  214. self._sprite.set_rotation(270)
  215. self._sprite.move(x=-1)
  216.  
  217. def _update_counts(self):
  218. for i in range(len(self._farm_counters)):
  219. count_canvas_text = self._farm_counters[i]
  220. count_val = self.gamemanager.get_farm(i).get_pig_count()
  221.  
  222. count_canvas_text.set_text("{}".format(count_val))
  223.  
  224. def _draw_background(self):
  225. self.canvas.new_bitmap(3, 2, "Farm.png", x = 2, y = 1.5)
  226.  
  227. self.farm_sprites = [
  228. self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width - 2, y = 1.5),
  229. self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width - 2, y = self.height - 1.5),
  230. self.canvas.new_bitmap(3, 2, "Farm.png", x = 2, y = self.height - 1.5),
  231. self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width / 2, y = self.height / 2)
  232. ]
  233.  
  234. x_road_units = self.width - 7
  235.  
  236. for i in range(x_road_units):
  237. self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 4 + i, y = 1.5)
  238.  
  239. for i in range(x_road_units):
  240. self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 4 + i, y = self.height - 1.5)
  241.  
  242. y_road_units = self.height - 5
  243.  
  244. for i in range(y_road_units):
  245. self.canvas.new_bitmap(1, 1, "RoadNS.png", x = self.width - 2, y = 3 + i)
  246.  
  247. for i in range(int(y_road_units / 2)):
  248. self.canvas.new_bitmap(1, 1, "RoadNS.png", x = 2, y = self.height - (3 + i))
  249.  
  250. self.canvas.new_bitmap(1, 1, "RoadSE.png", x = 2, y = self.height - 8)
  251.  
  252. for i in range(int(x_road_units / 2)):
  253. i += 1
  254. self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 2 + i, y = self.height - 8)
  255.  
  256. def split_step(self):
  257. self.canvas.finish_step()
  258.  
  259. def finished(self):
  260. return self.gamemanager.is_finished()
  261.  
  262. def step(self):
  263. if self._driving:
  264. self._update_sprite()
  265.  
  266. if not self._driving:
  267. self.solution.update()
  268. self.gamemanager.update()
  269.  
  270. score = self.gamemanager.get_score_for_curr_farm()
  271. self.farm_scores.append(score)
  272. self._update_counts()
  273.  
  274. self._driving = True
  275.  
  276. self.counter += 1
  277.  
  278. def score(self):
  279. return [10]
  280.  
  281. def get_truck_capacity(self):
  282. return self.gamemanager.get_truck_capacity()
  283.  
  284. def unload_pig(self, identifier):
  285. return self.gamemanager.unload_pig(identifier)
  286.  
  287. def get_farm_order(self):
  288. return self.gamemanager.get_farm_order()
  289.  
  290. def load_pig(self):
  291. return self.gamemanager.load_pig()
  292.  
  293. def get_pig_trip_length(self, identifier):
  294. return self.gamemanager.get_pig_trip_length(identifier)
  295.  
  296. def get_next_farm_distance(self):
  297. return self.gamemanager.get_next_farm_distance()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement