Exemplo n.º 1
0
    def _collided(self, snake: Snake) -> bool:
        """
        Prüft, ob die Schlange mit einer Wand oder einer anderen Schlange kollidiert ist
        :param snake: Schlange, die geprüft wird
        :return: True, wenn die Schlange kollidiert
        """

        snake_head = snake.get_head()

        hit_wall = False
        for game_object in self.objects:
            if Game.is_obstacle(game_object):
                if game_object.same_position(snake_head):
                    hit_wall = True

        hit_snake = False
        for s in self.snakes:
            for s_body_idx, s_body in enumerate(s.body):
                if s_body.same_position(snake_head):
                    if s_body_idx != 0:
                        # wenn die Snake eine andere Snake trifft
                        hit_snake = True
                    else:
                        # Wenn sich zwei Köpfe treffen, gewinnt die längere
                        if snake != s and len(snake.body) <= len(s.body):
                            hit_snake = True

        return hit_wall or hit_snake
Exemplo n.º 2
0
    def feel_busy(self, snake: Snake, game: Game, grid_map: GridMap):

        possible_actions = snake.possible_actions()
        head = snake.get_head()

        if possible_actions is None:
            return None

        actions_without_obstacle = []

        for action in possible_actions:
            neighbor = grid_map.get_neighbor(head.x, head.y, action, game.height, game.width)

            if neighbor is None:
                continue

            if not Game.is_obstacle(neighbor):
                actions_without_obstacle.append(action)

        if len(actions_without_obstacle) > 0:
            return np.random.choice(actions_without_obstacle)
        else:
            return None
Exemplo n.º 3
0
    def _ate_fruit(self, snake: Snake) -> bool:
        """
        Prüft, ob die Schlange eine Frucht gegessen hat
        :param snake: Schlange, die geprüft wird
        :return: True, wenn eine Frucht gegessen wurde
        """
        ate_fruit = False
        snake_head = snake.get_head()

        for fruit in self.get_fruits():
            if fruit.same_position(snake_head):
                self.objects.remove(fruit)
                ate_fruit = True

        return ate_fruit
Exemplo n.º 4
0
    def _place_snakes_randomly(self, nb_snakes: int):
        """
        Platziert Schlangen an einer zufälligen Position auf dem Spielfeld
        :param nb_snakes: Anzahl der zu platzierenden Schlangen
        :return:
        """
        padding = 1
        for _ in range(nb_snakes):
            field = None
            while not self._is_available(field):
                x_position = np.random.randint(padding, self.width - padding)
                y_position = np.random.randint(padding, self.height - padding)
                field = Position(x_position, y_position)

            snake = Snake(field.x, field.y)
            self.snakes.append(snake)