Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from util.executor_exceptions import SolutionDeathError
- from challenge import Challenge
- CONSOLE = None
- class Pig:
- def __init__(self, identifier):
- self._identifier = identifier
- self.journey_length = 0
- class PigFactory:
- def __init__(self):
- self.curr_pig = 1
- def gen_pig(self):
- pig = Pig(self.curr_pig)
- self.curr_pig += 1
- return pig
- pigfactory = PigFactory()
- class Farm:
- def __init__(self, pigs_held, desired_pigs, distance):
- self._pigs = self._gen_pigs(pigs_held)
- self._desired_pigs = desired_pigs
- self.distance_to_next = distance
- def _gen_pigs(self, pigs_held):
- pig_list = []
- for i in range(pigs_held):
- pig_list.append(pigfactory.gen_pig())
- return pig_list
- def get_order(self):
- return len(self._pigs) - self._desired_pigs
- def get_pig_count(self):
- CONSOLE.log("Count: {}".format(len(self._pigs)))
- return len(self._pigs)
- def unload_pig(self):
- return self._pigs.pop(0) if len(self._pigs) > 0 else None
- def load_pig(self, pig):
- self._pigs.append(pig)
- def are_pigs_too_old(self):
- return len([pig for pig in self._pigs if pig.journey_length >= 3]) > 0
- class RoadMap:
- def __init__(self, farm_list):
- self.farms = []
- self.counter = 0
- for farm_data in farm_list:
- self.farms.append(self._gen_farm(farm_data))
- def _gen_farm(self, farm_data):
- farm = Farm(farm_data['pigs_held'], farm_data['desired_pigs'], farm_data['distance_to_next'])
- return farm
- def next_farm(self):
- CONSOLE.log("Next farm called")
- self.counter += 1
- return self.farms[self.counter]
- def get_farm(self, index):
- return self.farms[index]
- def first_farm(self): # Replace this with get_farm(0)
- return self.farms[0]
- def has_next(self):
- return self.counter + 1 < len(self.farms)
- class Truck:
- def __init__(self, capacity):
- self._pigs = []
- self.capacity = capacity
- def load_pig(self, pig):
- if len(self._pigs) < self.capacity:
- self._pigs.append(pig)
- def unload_pig(self, identifier):
- pig = self.get_pig(identifier)
- if pig is not None:
- self._pigs.remove(pig)
- return pig
- else:
- return None
- def get_pig(self, identifier):
- filtered = [pig for pig in self._pigs if pig._identifier == identifier]
- if(len(filtered) > 0):
- return filtered[0]
- else:
- return None
- def increase_pig_journey_length(self, delta):
- for pig in self._pigs:
- pig.journey_length += delta
- class GameManager:
- def __init__(self, truck, roadmap):
- self.truck = truck
- self.roadmap = roadmap
- self.curr_farm = self.roadmap.first_farm()
- def get_truck_capacity(self):
- return self.truck.capacity
- def get_farm(self, index):
- return self.roadmap.get_farm(index)
- def unload_pig(self, identifier):
- pig = self.truck.unload_pig(identifier)
- if pig is not None:
- self.curr_farm.load_pig(pig)
- def get_farm_order(self):
- return self.curr_farm.get_order()
- def load_pig(self):
- pig = self.curr_farm.unload_pig()
- self.truck.load_pig(pig)
- return pig._identifier
- def get_pig_trip_length(self, identifier):
- pig = self.truck.get_pig(identifier)
- return pig.journey_length if pig is not None else -1
- def get_next_farm_distance(self):
- return self.curr_farm.distance_to_next
- def update(self):
- distance = self.curr_farm.distance_to_next
- self.truck.increase_pig_journey_length(distance)
- self.curr_farm = self.roadmap.next_farm()
- def is_finished(self):
- return not self.roadmap.has_next()
- def get_score_for_curr_farm(self):
- score = 0
- if self.curr_farm.get_order() == 0:
- score += 1
- if not self.curr_farm.are_pigs_too_old():
- score += 1
- return score
- class ChallengeImpl(Challenge):
- def __init__(self, config, canvas, console, solutions, hidden):
- self.canvas = canvas
- self.solution = solutions[0]
- self.hidden = hidden
- self.height = 16
- self.width = 32
- self.counter = 0
- global CONSOLE
- CONSOLE = console
- roadmap = RoadMap(config['farms'])
- truck = Truck(config['capacity'])
- self.gamemanager = GameManager(truck, roadmap)
- self.farm_scores = []
- self._driving = False
- self._draw_background()
- self._sprite = self.canvas.new_bitmap(0.452, 0.816, "Truck.png", x = 2, y = 1.5, rotation=90, z_index=200)
- self._farm_counters = [
- 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),
- 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),
- 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),
- 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),
- 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)
- ]
- def _update_sprite(self):
- next_farm = self.farm_sprites[0]
- curr_y = self._sprite.get_y()
- curr_x = self._sprite.get_x()
- dest_y = next_farm.get_y()
- dest_x = next_farm.get_x()
- if (curr_y == dest_y) and (dest_x == curr_x):
- self._driving = False
- if len(self.farm_sprites) > 0: self.farm_sprites.pop(0)
- else:
- if(abs(dest_y - curr_y) < 1 and abs(dest_y - curr_y) > 0):
- self._sprite.move(y = dest_y - curr_y)
- elif(abs(dest_x - curr_x) < 1 and abs(dest_x - curr_x) > 0):
- self._sprite.move(x = dest_x - curr_x)
- else:
- if dest_y > curr_y:
- self._sprite.set_rotation(180)
- self._sprite.move(y = 1)
- elif dest_y < curr_y:
- self._sprite.set_rotation(0)
- self._sprite.move(y= - 1)
- elif dest_x > curr_x:
- self._sprite.set_rotation(90)
- self._sprite.move(x= 1)
- elif dest_x < curr_x:
- self._sprite.set_rotation(270)
- self._sprite.move(x=-1)
- def _update_counts(self):
- for i in range(len(self._farm_counters)):
- count_canvas_text = self._farm_counters[i]
- count_val = self.gamemanager.get_farm(i).get_pig_count()
- count_canvas_text.set_text("{}".format(count_val))
- def _draw_background(self):
- self.canvas.new_bitmap(3, 2, "Farm.png", x = 2, y = 1.5)
- self.farm_sprites = [
- self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width - 2, y = 1.5),
- self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width - 2, y = self.height - 1.5),
- self.canvas.new_bitmap(3, 2, "Farm.png", x = 2, y = self.height - 1.5),
- self.canvas.new_bitmap(3, 2, "Farm.png", x = self.width / 2, y = self.height / 2)
- ]
- x_road_units = self.width - 7
- for i in range(x_road_units):
- self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 4 + i, y = 1.5)
- for i in range(x_road_units):
- self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 4 + i, y = self.height - 1.5)
- y_road_units = self.height - 5
- for i in range(y_road_units):
- self.canvas.new_bitmap(1, 1, "RoadNS.png", x = self.width - 2, y = 3 + i)
- for i in range(int(y_road_units / 2)):
- self.canvas.new_bitmap(1, 1, "RoadNS.png", x = 2, y = self.height - (3 + i))
- self.canvas.new_bitmap(1, 1, "RoadSE.png", x = 2, y = self.height - 8)
- for i in range(int(x_road_units / 2)):
- i += 1
- self.canvas.new_bitmap(1, 1, "RoadWE.png", x = 2 + i, y = self.height - 8)
- def split_step(self):
- self.canvas.finish_step()
- def finished(self):
- return self.gamemanager.is_finished()
- def step(self):
- if self._driving:
- self._update_sprite()
- if not self._driving:
- self.solution.update()
- self.gamemanager.update()
- score = self.gamemanager.get_score_for_curr_farm()
- self.farm_scores.append(score)
- self._update_counts()
- self._driving = True
- self.counter += 1
- def score(self):
- return [10]
- def get_truck_capacity(self):
- return self.gamemanager.get_truck_capacity()
- def unload_pig(self, identifier):
- return self.gamemanager.unload_pig(identifier)
- def get_farm_order(self):
- return self.gamemanager.get_farm_order()
- def load_pig(self):
- return self.gamemanager.load_pig()
- def get_pig_trip_length(self, identifier):
- return self.gamemanager.get_pig_trip_length(identifier)
- def get_next_farm_distance(self):
- return self.gamemanager.get_next_farm_distance()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement