def handle(self, memory, current_location): print("Checking if we can eat this plant.") response = Communicator.send_action('sense', []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) return result = response['result'] memory.update_squares(result) square = memory.get_square(current_location) if square['plant'] is None: print("No plant! No plant! Where is my delicious plant!") print("I have to plant another one :/") self._plant() return if not square['plant']['matured']: print("This plant is not matured, have to wait more.") return print("Eating this yummy plant!") response = Communicator.send_action('eat', []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) print("Planting another one here.") self._plant()
def handle(self, memory): while True: response = Communicator.send_action("sense", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) break # Updating the memory with these new data. result = response['result'] memory.update_squares(result) if memory.get_nearest_water() is not None: print("I found water on", memory.get_nearest_water()) return print("No water yet, moving on ", self._current_direction, "direction.") response = Communicator.send_action("move", [self._current_direction]) if response['status'] == 500: if response['error_code'] in ("InvalidLocationError", "LocationIsBlockedError"): self._current_direction = self._next_direction[ self._current_direction] print("Changing direction to", self._current_direction)
def run_til_die(self): '''Runs in a loop until the robot dies.''' # The algorithm: # 1) Find water. # 2) Take water and get back to the nearest soil. # 3) Plant three corps. # 4) Water corps. # 5) If honor is enough, give birth to another robot. # 6) Continue from 2 response = Communicator.send_action("info", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) return self._memory.memorize_world_info(response['result']) print("I learned these about this world:", response['result']) print("Let's explore the world!") while True: # Checking the status. response = Communicator.send_action("status", []) if response['status'] == 500: # Error occured. if response['error_code'] == 'AuthenticationFailedError': print("Seems that I'm dead. Goodbye beautiful world.") break else: print("Unexpected error:", response['error_code'], ":", response['error_message']) break status = response['result'] print("My current status is:", status) if (status['location'] == self._memory.get_first_plant_location() or status['location'] == self._memory.get_second_plant_location()): # Trying to eat this plant. EatingHandler().handle(self._memory, status['location']) if status['honor'] >= self._memory.get_birth_required_honor(): print("Enough honor, let's giving birth to a child!") GiveBirthHandler().handle(self._memory) elif self._memory.get_nearest_water() is None: print("I still don't know any water. Let's find some!") WaterFindingHandler().handle(self._memory) elif not status['has_water']: print("No water on hand. Going to pick some.") WaterPickingHandler().handle(self._memory, status['location']) elif status['has_water'] and self._memory.get_first_plant_location() is None: print("I still didn't plant anything. Going to plant some corps.") PlantingHandler().handle(self._memory, status['location']) elif status['has_water'] and self._memory.get_first_plant_location() is not None: print("Going to water my plants.") WateringHandler().handle(self._memory, status['location'])
def move(from_location, to_location): '''Moves the robot from the specified location to the specified location. Locations should be strings in form "x,y". ''' to_location_x, to_location_y = to_location.split(',') to_location_x = int(to_location_x) to_location_y = int(to_location_y) from_location_x, from_location_y = from_location.split(',') from_location_x = int(from_location_x) from_location_y = int(from_location_y) print("Current location:", from_location) print("Moving to location:", to_location_x, ",", to_location_y) while from_location_x != to_location_x or from_location_y != to_location_y: direction = "" # We also update the current location, so we don't need asking server and losing energy. if from_location_y < to_location_y: direction += "S" from_location_y += 1 if from_location_y > to_location_y: direction += "N" from_location_y -= 1 if from_location_x < to_location_x: direction += "E" from_location_x += 1 if from_location_x > to_location_x: direction += "W" from_location_x -= 1 print("Moving", direction) response = Communicator.send_action("move", [direction]) if response['status'] == 500: if response['error_code'] == "LocationIsBlockedError": while True: print( "The path is blocked! But I'm not smart enough to pass a blocked object :/" ) # Waiting a little, maybe there's a robot or something on the way. time.sleep(0.5) response = Communicator.send_action("move", [direction]) if not (response['status'] == 500 and response['error_code'] == "LocationIsBlockedError"): # Path cleared, or there's a new error. break else: print("Unexpected error:", response['error_code'], ":", response['error_message'])
def move(from_location, to_location): '''Moves the robot from the specified location to the specified location. Locations should be strings in form "x,y". ''' to_location_x, to_location_y = to_location.split(',') to_location_x = int(to_location_x) to_location_y = int(to_location_y) from_location_x, from_location_y = from_location.split(',') from_location_x = int(from_location_x) from_location_y = int(from_location_y) print("Current location:", from_location) print("Moving to location:", to_location_x, ",", to_location_y) while from_location_x != to_location_x or from_location_y != to_location_y: direction = "" # We also update the current location, so we don't need asking server and losing energy. if from_location_y < to_location_y: direction += "S" from_location_y += 1 if from_location_y > to_location_y: direction += "N" from_location_y -= 1 if from_location_x < to_location_x: direction += "E" from_location_x += 1 if from_location_x > to_location_x: direction += "W" from_location_x -= 1 print("Moving", direction) response = Communicator.send_action("move", [direction]) if response['status'] == 500: if response['error_code'] == "LocationIsBlockedError": while True: print("The path is blocked! But I'm not smart enough to pass a blocked object :/") # Waiting a little, maybe there's a robot or something on the way. time.sleep(0.5) response = Communicator.send_action("move", [direction]) if not (response['status'] == 500 and response['error_code'] == "LocationIsBlockedError"): # Path cleared, or there's a new error. break else: print("Unexpected error:", response['error_code'], ":", response['error_message'])
def handle(self, memory, current_location): print("Moving to the latest known soil.") helpers.move.move(current_location, memory.get_nearest_soil()) print("Checking current location.") response = Communicator.send_action("status", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) status = response['result'] print("Now I'm at the", status['location'], "location. Planting something.") response = Communicator.send_action("plant", []) if response['status'] == 500: if response['error_code'] == 'AlreadyPlantError': print("Strange, but there's already a plant on", status['location']) memory.store_first_plant_location(status['location']) else: print("Unexpected error:", response['error_code'], ":", response['error_message']) else: memory.store_first_plant_location(status['location']) print("Trying to find other soils nearby, and planting on them too.") near_soil = self._find_nearby_soil(memory, status['location']) if near_soil is None: print("Couldn't find any other soils around.") return helpers.move.move(status['location'], near_soil) print("Planting on the founded soil.") response = Communicator.send_action("plant", []) if response['status'] == 500: if response['error_code'] == 'AlreadyPlantError': print("Strange, but there's already a plant on", status['location']) memory.store_second_plant_location(status['location']) else: print("Unexpected error:", response['error_code'], ":", response['error_message']) else: memory.store_second_plant_location(status['location'])
def handle(self, memory, current_location): print("Moving to the latest known water location.") helpers.move.move(current_location, memory.get_nearest_water()) print("I'm reached the water location. Picking some water.") response = Communicator.send_action("pick_water", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message'])
def handle(self, memory): while True: response = Communicator.send_action("sense", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) break # Updating the memory with these new data. result = response['result'] memory.update_squares(result) if memory.get_nearest_water() is not None: print("I found water on", memory.get_nearest_water()) return print("No water yet, moving on ", self._current_direction, "direction.") response = Communicator.send_action("move", [self._current_direction]) if response['status'] == 500: if response['error_code'] in ("InvalidLocationError", "LocationIsBlockedError"): self._current_direction = self._next_direction[self._current_direction] print("Changing direction to", self._current_direction)
def handle(self, memory): response = Communicator.send_action("give_birth", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) new_password = response['result'] print("Come, come, my little sweetie!") current_module_directory = os.path.abspath(os.path.dirname(sys.modules[__name__].__file__)) main_file = os.path.join(current_module_directory, '..', 'simple_bot.py') # Starting the child process. We set its stdout and stderr to ours, so the child can write # logs to terminal too. child_process = subprocess.Popen(["python3", main_file, new_password], stdout=sys.stdout, stderr=sys.stderr) print("My child came to life with PID", child_process.pid)
def handle(self, memory, current_location): if WateringHandler.LAST_WATERED == "first": plant_to_water = memory.get_second_plant_location() if plant_to_water is None: # There maybe no second plant. plant_to_water = memory.get_first_plant_location() WateringHandler.LAST_WATERED = "second" else: plant_to_water = memory.get_first_plant_location() print("Going to water", WateringHandler.LAST_WATERED, "plant.") helpers.move.move(current_location, plant_to_water) print("I reached the plant. Watering...") response = Communicator.send_action("water", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message'])
def handle(self, memory): response = Communicator.send_action("give_birth", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) new_password = response['result'] print("Come, come, my little sweetie!") current_module_directory = os.path.abspath( os.path.dirname(sys.modules[__name__].__file__)) main_file = os.path.join(current_module_directory, '..', 'simple_bot.py') # Starting the child process. We set its stdout and stderr to ours, so the child can write # logs to terminal too. child_process = subprocess.Popen(["python3", main_file, new_password], stdout=sys.stdout, stderr=sys.stderr) print("My child came to life with PID", child_process.pid)
def run_til_die(self): '''Runs in a loop until the robot dies.''' # The algorithm: # 1) Find water. # 2) Take water and get back to the nearest soil. # 3) Plant three corps. # 4) Water corps. # 5) If honor is enough, give birth to another robot. # 6) Continue from 2 response = Communicator.send_action("info", []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message']) return self._memory.memorize_world_info(response['result']) print("I learned these about this world:", response['result']) print("Let's explore the world!") while True: # Checking the status. response = Communicator.send_action("status", []) if response['status'] == 500: # Error occured. if response['error_code'] == 'AuthenticationFailedError': print("Seems that I'm dead. Goodbye beautiful world.") break else: print("Unexpected error:", response['error_code'], ":", response['error_message']) break status = response['result'] print("My current status is:", status) if (status['location'] == self._memory.get_first_plant_location() or status['location'] == self._memory.get_second_plant_location()): # Trying to eat this plant. EatingHandler().handle(self._memory, status['location']) if status['honor'] >= self._memory.get_birth_required_honor(): print("Enough honor, let's giving birth to a child!") GiveBirthHandler().handle(self._memory) elif self._memory.get_nearest_water() is None: print("I still don't know any water. Let's find some!") WaterFindingHandler().handle(self._memory) elif not status['has_water']: print("No water on hand. Going to pick some.") WaterPickingHandler().handle(self._memory, status['location']) elif status['has_water'] and self._memory.get_first_plant_location( ) is None: print( "I still didn't plant anything. Going to plant some corps." ) PlantingHandler().handle(self._memory, status['location']) elif status['has_water'] and self._memory.get_first_plant_location( ) is not None: print("Going to water my plants.") WateringHandler().handle(self._memory, status['location'])
def _plant(self): response = Communicator.send_action('plant', []) if response['status'] == 500: print("Unexpected error:", response['error_code'], ":", response['error_message'])