def move(self): self.prev_steps.append(self.position) if algorithms.see_each_other(self.position, f.position, position_matrix): self.fox_last_seen.append(f.position) else: if len(self.fox_last_seen) > 0: self.fox_last_seen.append(self.fox_last_seen[-1]) else: self.fox_last_seen.append(None) if self.fox_last_seen[-1] != None: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue if len(self.prev_steps ) > 1 and curr_position == self.prev_steps[-2]: continue if algorithms.distance(f.position, curr_position) == 1: continue if number_of_bad_neighbours(curr_position) == 3: continue dist_from_fox = abs(curr_position[0] - self.fox_last_seen[-1] [0]) + abs(curr_position[1] - self.fox_last_seen[-1][1]) curr_cost = algorithms.f(12 - dist_from_fox) curr_cost += 5 * algorithms.f( abs(curr_position[0] - 12)) + algorithms.f( abs(curr_position[1] - 12)) curr_cost -= 10 * self.number_of_good_bushes() if self.fox_last_seen[-1] != None: if not algorithms.see_each_other(curr_position, self.fox_last_seen[-1], position_matrix): curr_cost -= 100 heapq.heappush(options, (curr_cost, curr_position)) if options == []: self.position = self.prev_steps[-2] else: self.position = heapq.heappop(options)[1] else: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue if number_of_bad_neighbours(curr_position) == 3: continue if curr_position == self.position: continue options.append(curr_position) self.position = random.choice(options) self.turn += 1
def move(self): ''' if (r.position[1], self.position[1]) in {(0, 1), (24, 23)} and abs(self.position[0] - r.position[0]) == 1: self.position = r.position if (r.position[0], self.position[0]) in {(0, 1), (24, 23)} and abs(self.position[1] - r.position[1]) == 1: self.position = r.position ''' if algorithms.see_each_other(self.position, r.position, position_matrix): path = algorithms.minimal_distance(self.position, r.position, position_matrix) self.position = path[-1] self.rabbit_last_seen = r.position elif self.rabbit_last_seen != None and self.rabbit_last_seen != self.position: path = algorithms.minimal_distance(self.position, r.position, position_matrix) self.position = path[-1] else: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue options.append(curr_position) self.position = random.choice(options)
def move(self): self.prev_steps.append(self.position) p = random.randrange(0, 1000) if number_of_steps > 10 and self.position == self.prev_steps[-9]: p = 0 if p > 10 and algorithms.see_each_other(self.position, r.position, position_matrix): path = algorithms.minimal_distance(self.position, r.position, position_matrix) self.position = path[-1] self.rabbit_last_seen = r.position elif p > 10 and self.rabbit_last_seen != None and self.rabbit_last_seen != self.position: path = algorithms.minimal_distance(self.position, r.position, position_matrix) self.position = path[-1] else: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue if len(self.prev_steps ) > 1 and curr_position == self.prev_steps[-2]: continue options.append(curr_position) self.position = random.choice(options)
def move(self): if algorithms.see_each_other(self.position, f.position, position_matrix): self.fox_last_seen = f.position if self.fox_last_seen != None: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue if curr_position == self.prev_position: continue if algorithms.distance( f.position, curr_position) == 1 or curr_position == f.position: continue curr_cost = algorithms.f( 12 - (abs(curr_position[0] - self.fox_last_seen[0]) + abs(curr_position[1] - self.fox_last_seen[1]))) curr_cost += algorithms.f(curr_position[0] - 12) + algorithms.f(curr_position[1] - 12) if curr_position[0] in [0, 1, 24, 23] or curr_position[0] in [ 0, 1, 24, 23 ]: curr_cost += 30 heapq.heappush(options, (curr_cost, curr_position)) tmp = self.prev_position self.prev_position = self.position if options == []: self.position = tmp else: self.position = heapq.heappop(options)[1] else: options = [] for (dx, dy) in [(0, 1), (1, 0), (-1, 0), (0, -1)]: curr_position = (self.position[0] + dx, self.position[1] + dy) if outOffBoundaries(curr_position) or isCollision( curr_position): continue options.append(curr_position) self.position = random.choice(options) self.turn += 1