def follow_arrow (grid, ant): radius = ant.get_radius() orientation = ant.get_orientation() location = ant.get_location() old_location = grid[location] grid[location] = (old_location[0], EMPTY, old_location[2]) if grid[location][0] == UP_SYM: # print "up" ant.set_location(radius[(9 - 2*(orientation)) % 8]) force_orientation(ant,UP) elif grid[location][0] == RIGHT_SYM: # print "right" ant.set_location(radius[(11 - 2*(orientation)) % 8]) force_orientation(ant,RIGHT) elif grid[location][0] == DOWN_SYM: # print "down" ant.set_location(radius[(13 - 2*(orientation)) % 8]) force_orientation(ant,DOWN) elif grid[location][0] == LEFT_SYM: # print "left" ant.set_location(radius[(7 - 2*(orientation)) % 8]) force_orientation(ant,LEFT) new_location = ant.get_location() # print "new location is ", new_location grid[new_location] = (grid[new_location][0], ANT, grid[new_location][2])
def move(grid, ant, side, pheromone): radius = ant.get_radius() location = ant.get_location() symbol = ant.get_symbol() old_location = grid[location] grid[location] = (old_location[0], EMPTY, old_location[2]) new_location = radius[2*side + 1] ant.set_location(new_location) set_orientation(ant,side) if pheromone == EMPTY: grid[new_location] = (grid[new_location][0], ANT, grid[new_location][2]) else: grid[new_location] = (pheromone, ANT, symbol)
def step(self, ant): if self.is_ant_in_radius(ant): return 1 ant_pheromone = self.is_pheromone_in_radius(ant) if ant.get_state() == NOT_FOUND and ant_pheromone != 0: # print "ant pheromone", ant_pheromone if ant.get_ID() > int(self[ant_pheromone[0]][2]): # print "master" ant.set_state(FOUND_PHEROMONE_MASTER) side = (ant_pheromone[1]-1)/2 # print side move(self, ant, side, EMPTY) else: # print "servant" ant.set_state(FOUND_PHEROMONE_SERVANT) follow_arrow(self, ant) elif ant.get_state() == NOT_FOUND: if self.dead_end(ant): # set_orientation(ant, DOWN) follow_arrow(self, ant) elif self.is_empty(ant, RIGHT): move(self, ant, RIGHT, get_back_pheromone(ant,RIGHT)) elif self.is_obstacle(ant, RIGHT) and self.is_empty(ant, UP): # circle_obstacle(self, ant, RIGHT) move(self, ant, UP, get_back_pheromone(ant, UP)) elif self.is_obstacle(ant, UP): move(self, ant, LEFT, get_back_pheromone(ant, LEFT)) elif self.is_empty(ant, UP): move(self, ant, UP, get_back_pheromone(ant,UP)) elif self.is_empty(ant, LEFT): move(self, ant, LEFT, get_back_pheromone(ant,LEFT)) else: print "problem?" elif ant.get_state() == FOUND_PHEROMONE_MASTER: location = ant.get_location() if self[location][0] == "H": ant.set_state(FOUND_BASE) else: follow_arrow(self, ant) #follow trail to other base elif ant.get_state() == FOUND_PHEROMONE_SERVANT: follow_arrow(self, ant) # print "following" #follow trail to base return 0