Example #1
0
            def search(state, alpha, beta):
                if (time() - start_time) >= self.max_time:
                    return -1

                player = State.opponent(state.player)
                moves = state.board.legal_moves(player)

                if state.depth == plies or len(moves) == 0:
                    self.evaluate(state)
                else:
                    for move in moves:
                        next_board = deepcopy(state.board)
                        next_board.make_move(move[0], move[1], player)
                        next_state = GameState(next_board,
                                               parent=state,
                                               player=player,
                                               depth=state.depth+1,
                                               move=move)
                        ret = search(next_state, alpha, beta)
                        if ret == -1:
                            return ret

                        if state.player is State.black:
                            alpha = max(alpha, next_state.score)
                            state.score = alpha
                        else:
                            beta = min(beta, next_state.score)
                            state.score = beta

                        if alpha >= beta:
                            break

                        state.children[move] = next_state
Example #2
0
    def move(self, board):
        start_time = time()
        best_move = None

        plies = 0
        max_plies = 64 - board.total_count()

        while True:
            plies += 1

            if plies > max_plies or (time() - start_time) >= self.max_time:
                return best_move

            def search(state, alpha, beta):
                if (time() - start_time) >= self.max_time:
                    return -1

                player = State.opponent(state.player)
                moves = state.board.legal_moves(player)

                if state.depth == plies or len(moves) == 0:
                    self.evaluate(state)
                else:
                    for move in moves:
                        next_board = deepcopy(state.board)
                        next_board.make_move(move[0], move[1], player)
                        next_state = GameState(next_board,
                                               parent=state,
                                               player=player,
                                               depth=state.depth+1,
                                               move=move)
                        ret = search(next_state, alpha, beta)
                        if ret == -1:
                            return ret

                        if state.player is State.black:
                            alpha = max(alpha, next_state.score)
                            state.score = alpha
                        else:
                            beta = min(beta, next_state.score)
                            state.score = beta

                        if alpha >= beta:
                            break

                        state.children[move] = next_state

            initial_player = State.opponent(self.color)
            current_state = GameState(board, depth=0, player=initial_player)
            ret = search(current_state, float("-inf"), float("inf"))

            if ret == -1:
                return best_move

            scores = [(s.score, m) for m, s in current_state.children.items()]

            if len(scores) == 0:
                # pass
                return None

            if self.color is State.black:
                best_score, best_move = max(scores)
            else:
                best_score, best_move = min(scores)

            s = "abcdefgh"[best_move[1]] + str(best_move[0] + 1)
            print("Searched {} plies and got {} ({})".format(plies,
                                                             s,
                                                             best_score))

        return best_move