예제 #1
0
 def do_extra_step(self):
     #With a chance of PSWAY, this walker is "pushed" to another cell
     chance = np.random.randint(1, 101)
     if chance <= self.psway:
         #Now choose a random cell
         direction = np.random.randint(1, 9)
         neighbour = Grid.neighbour_pos_in_direction(self.pos, direction)
         if self.grid.is_empty(neighbour):
             self.set_pos(neighbour)
예제 #2
0
    def find_empty_cell(self, direction, rot):
        """ checks if self.pos + (direction +- rot) is an empty cell.
        If only one is empty, return the direction and position for it
        If both are empty, decide randomly which one to use.
        If none is empty, return None

        @return
        A (direction, position) tuple for the resulting cell found or (Direction.NONE, None),
        if all concerned cells are occupied.
        """

        # if direction == Direction.NONE:
        #     # if we don't have
        #     return Direction.NONE, None

        pdir = rotate(direction, +rot)
        mdir = rotate(direction, -rot)

        ppos = Grid.neighbour_pos_in_direction(self.pos, pdir)
        mpos = Grid.neighbour_pos_in_direction(self.pos, mdir)

        pempty = self.grid.is_empty(ppos)
        mempty = self.grid.is_empty(mpos)

        pres = (pdir, ppos)
        mres = (mdir, mpos)

        if pempty and mempty:
            # both are free, decide randomly
            return random.choice([pres, mres])
        elif pempty:
            return pres
        elif mempty:
            return mres
        else:
            # none is empty
            return Direction.NONE, None
예제 #3
0
    def try_cell(self, direction):
        target_dir = direction
        target_pos = Grid.neighbour_pos_in_direction(self.pos, direction)
        #Cell is empty move to it in direction
        if self.grid.is_empty(target_pos):
            return target_dir, target_pos

        if target_dir == Direction.NONE:
            return target_dir, None

        #Otherwise try surrounding fields
        for t in (45, 90):
            target_dir, target_pos = self.find_empty_cell(direction, t)

            # if we can move in the checked directions
            if target_pos is not None:
                return target_dir, target_pos

        #No valid position found
        return Direction.NONE, None