Beispiel #1
0
    def is_valid_move(self, xy, direction, distance, colour):

        if colour != self.board.get_colour(xy):
            return False

        xy2 = dir_to_xy(xy, direction, distance)

        return tokens.is_valid_move(self.board, xy, xy2)
Beispiel #2
0
    def move_token(self, n, xy1, xy2, check_valid=True):
        if check_valid and not tokens.is_valid_move(self, xy1, xy2):
            raise Exception("This is not a valid move.")
        else:
            new_size = n
            if not self.is_cell_empty(xy2):
                new_size = new_size + self.get_size(xy2)

            self.place_token(self.get_colour(xy1), new_size, xy2)
            self.remove_token(n, xy1)
Beispiel #3
0
    def is_comp_valid_move(self, move):

        action_type = move[0]

        if action_type == "BOOM":
            xy1 = move[1]
            piece = self.game.board.get_token(xy1)
            if piece[0] == self.player and not self.game.board.is_cell_empty(xy1):
                return True
        else:
            n = move[1]
            xy1 = move[2]
            xy2 = move[3]

            if not self.game.board.is_cell_empty(xy1):
                piece = self.game.board.get_token(xy1)
                if tokens.is_valid_move(self.game.board, xy1, xy2):
                    if piece[0] == self.player and piece[1] >= n:
                        return True
        return False
Beispiel #4
0
    def two_enemy_endgame(self, game_state, simulations, search_depth,
                          trade_threshold):
        enemy_stacks = len(game_state[self.other])

        # Check if enemy can kill us or draw
        for piece in game_state[self.other]:
            temp_game = game.Game(game_state)
            temp_game.boom((piece[1], piece[2]), self.other)
            home_a = features.count_pieces(
                temp_game.get_game_state()[self.player])
            away_a = features.count_pieces(
                temp_game.get_game_state()[self.other])

            if not temp_game.get_game_state()[
                    self.player] or home_a - away_a < trade_threshold:
                strategy = self.monte_carlo(game_state, simulations,
                                            search_depth)
                return strategy

        # One stack
        if enemy_stacks == 1:
            enemy = game_state[self.other][0]
            enemy_xy = enemy[1], enemy[2]

            for piece in game_state[self.player]:
                temp_game = game.Game(game_state)
                temp_game.boom((piece[1], piece[2]), self.player)

                if temp_game.get_game_state(
                )[self.player] and not temp_game.get_game_state()[self.other]:
                    return None, (piece[1], piece[2]), "Boom", None

            ally = self.closest_npiece(game_state, 2, self.player, enemy_xy)
            if ally is None:
                return self.make_stack(game_state)

            ally_xy = ally[1], ally[2]

            width = enemy_xy[0] - ally_xy[0]
            height = enemy_xy[1] - ally_xy[1]

            if width == 0 and abs(height) <= ally[0]:
                return self.go_there(
                    2, ally, (enemy_xy[0], enemy_xy[1] - np.sign(height)))
            if height == 0 and abs(width) <= ally[0]:
                return self.go_there(
                    2, ally, (enemy_xy[0] - np.sign(width), enemy_xy[1]))

            enemy_corner_xy = self.get_nearest_corner(ally_xy, enemy_xy)
            move = self.go_there(2, ally, enemy_corner_xy)

            if tokens.is_valid_move(self.game.board, move[1],
                                    game.dir_to_xy(move[1], move[2], move[3])):
                return move
            else:
                return self.monte_carlo(game_state, simulations, search_depth)

        # Two seperate stacks
        else:
            return self.trade_tokens(game_state, simulations, search_depth,
                                     trade_threshold)