Changeset 149:2a1064fae608


Ignore:
Timestamp:
Sep 3, 2009, 8:12:54 PM (11 years ago)
Author:
Neil Muller <drnlmuller@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@150
Message:

More memory for the foxes, to ensure we avoid short loops

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/animal.py

    r146 r149  
    177177        self.safe = False
    178178        self.closest = None
     179        self.last_steps = []
    179180
    180181    def _cost_tile(self, pos, gameboard):
     
    275276        gameboard.remove_chicken(chicken)
    276277        self.hunting = False
     278        self.last_steps = [] # Forget history here
    277279
    278280    def _update_pos(self, gameboard, new_pos):
    279281        """Update the position, making sure we don't step on other foxes"""
    280         final_pos = new_pos
    281282        if new_pos == self.pos:
    282283            # We're not moving, so we can skip all the checks
    283             return final_pos
    284         blocked = False
     284            return new_pos
     285        final_pos = new_pos
     286        blocked = final_pos in self.last_steps
    285287        moves = [Position(x, y) for x in range(self.pos.x-1, self.pos.x + 2)
    286288                for y in range(self.pos.y-1, self.pos.y + 2)
    287                 if (x,y) != (0,0)]
     289                if Position(x,y) != self.pos and \
     290                        Position(x, y) not in self.last_steps]
    288291        for fox in gameboard.foxes:
    289             if fox is not self and fox.pos == new_pos:
     292            if fox is not self and fox.pos == final_pos:
    290293                blocked = True
    291294            if fox.pos in moves:
     
    300303                    min_cost = cost
    301304                    final_pos = poss
     305        if not final_pos:
     306            # No good choice, so stay put
     307            return self.pos
    302308        if gameboard.in_bounds(final_pos):
    303309            this_tile = gameboard.tv.get(final_pos.to_tuple())
     
    310316            self._dig(final_pos)
    311317            return self.pos
     318        self.last_steps.append(final_pos)
     319        if len(self.last_steps) > 3:
     320            self.last_steps.pop(0)
    312321        return final_pos
    313322
     
    371380        if self.chickens_eaten > 2:
    372381            self.hunting = False
     382        self.last_steps = []
    373383
    374384def visible(watcher, watchee):
Note: See TracChangeset for help on using the changeset viewer.