示例#1
0
            if self.grid[self.my_pos + self.grid.DIRECTIONS[d]] != 0:
                start_dir_no = i + 1 + (1 if self.unhug else 0)

        if start_dir_no is None:
            return 'LEFT'  # whatever

        full_bfs = self.grid.bfs_probe(self.my_pos, limit=self.depth_limit)

        options = {}
        for i in xrange(start_dir_no, start_dir_no + 4):
            direction = directions[i % 4]
            offset = self.grid.DIRECTIONS[direction]
            new_pos = self.my_pos + offset
            if self.grid[new_pos] != 0: continue
            pr = self.grid.bfs_probe(new_pos, limit=self.depth_limit)
            if pr.empty_count >= full_bfs.empty_count * self.threshold / 100.0:
                return direction
            else:
                options[pr.empty_count] = direction

        if options:
            return options[max(options)]
        else:
            return '!'

    go = go_hug


if __name__ == '__main__':
    run_ai(AIHugger)
示例#2
0
        if lists:
            dirs = [d for d in lists[0] if d in dirs]
        return dirs

    def decide(self, empty_count, heads):
        """Return the instruction for the next move.

        The instructions are:
            * -1 - turn left,
            * 0 - go straight,
            * 1 - turn right.

        This is an example which implements hugger with enemy avoidance.
        """
        open_dirs = self.filter_dirs_gt(8, empty_count * 0.8)
        if open_dirs:
            return open_dirs
        else:
            close_enemies = self.filter_dirs_lt(3, 2)
            no_close_enemies = self.invert_dirs(close_enemies)
            sorted_by_volume = self.sort_dirs(0, 0, 1, 0, 0, 0, 0, 0, 1)
            sbv_nce = self.intersect_dirs(sorted_by_volume, no_close_enemies)
            if sbv_nce:
                return sbv_nce
            else:
                return sorted_by_volume


if __name__ == '__main__':
    run_ai(AIGBase)
示例#3
0
    def __init__(self):
        super(AIMiniMaxer, self).__init__()
        self.add_param('max_layers', 'm',
                help='Number of minimax layers.')
        self.add_param('max_layer_size', 'y',
                help='Max number of states in the layer.')

    def can_see_others(self):
        """Return True if we can reach other players."""
        self.full_bfs = self.grid.bfs_probe(self.my_pos)
        for i in xrange(4):
            if self.grid.head_of(i) in self.full_bfs.objects:
                return True
        return False

    def go(self):
        """Act depending if we see others."""
        if self.can_see_others():
            mm = MiniMax(self.grid, self.players[self.my_number],
                    full_bfs=self.full_bfs)
            weight, move = mm.find_best_move(max_layers=self.max_layers,
                    max_layer_size=self.max_layer_size)
            mm.unlink_states()
            if weight > 0:
                return move
        return self.go_wander()


if __name__ == '__main__':
    run_ai(AIMiniMaxer)
示例#4
0
                help='Relative weight of maximal distance.')
        self.add_param('obstacle_fear', 'o',
                help='Relative weight of distance to closest obstacle.')
        self.add_param('space_love', 's',
                help='Relative weight of amount of space.')

    def go_wander(self):
        """Find the direction with least interference."""
        options = {}
        for dir, offset in self.grid.DIRECTIONS.items():
            new_pos = self.my_pos + offset
            if self.grid[new_pos] != 0: continue
            pr = self.grid.bfs_probe(new_pos, limit=self.depth_limit)
            weight = pr.max_distance * self.distance_love
            if weight > 0:
                weight += self.space_love * pr.empty_count
                if pr.closest_obstacle_d is not None:
                    weight += self.obstacle_fear * pr.closest_obstacle_d
                options[weight] = dir

        if options:
            return options[max(options)]
        else:
            return '!'

    go = go_wander


if __name__ == '__main__':
    run_ai(AIWanderer)
示例#5
0
        """Look for good straight directions."""
        options = {}
        for dir, offset in self.grid.DIRECTIONS.items():
            pr = self.grid.ray_probe(self.my_pos, offset, self.ray_width)
            weight = pr.max_distance
            weight += pr.closest_obstacle_d * 3
            options[weight] = dir

        self.direction = options[max(options)]

    def go_straight(self):
        """Go straight to the farthest open side."""
        self.choose_direction()
        if self.check_direction():
            return self.direction
        else:
            return self.go_wander()

    def go(self):
        """Act depending on the phase of the game."""
        self.phase = self.get_phase()

        if self.phase in [self.OPEN, self.POCKET]:
            return self.go_straight()
        else:
            return self.go_wander()


if __name__ == '__main__':
    run_ai(AICrosser)
示例#6
0
        if lists:
            dirs = [d for d in lists[0] if d in dirs]
        return dirs

    def decide(self, empty_count, heads):
        """Return the instruction for the next move.

        The instructions are:
            * -1 - turn left,
            * 0 - go straight,
            * 1 - turn right.

        This is an example which implements hugger with enemy avoidance.
        """
        open_dirs = self.filter_dirs_gt(8, empty_count * 0.8)
        if open_dirs:
            return open_dirs
        else:
            close_enemies = self.filter_dirs_lt(3, 2)
            no_close_enemies = self.invert_dirs(close_enemies)
            sorted_by_volume = self.sort_dirs(0, 0, 1, 0, 0, 0, 0, 0, 1)
            sbv_nce = self.intersect_dirs(sorted_by_volume, no_close_enemies)
            if sbv_nce:
                return sbv_nce
            else:
                return sorted_by_volume


if __name__ == '__main__':
    run_ai(AIGBase)