예제 #1
0
def min_max(board_state,
            ai,
            game,
            max_depth,
            alpha=-sys.float_info.max,
            beta=sys.float_info.max):
    # end = False
    # if time.time() - max_depth >= 5:
    #     end = True
    if not board_state.legal_moves:
        return 0, None
    best_score_move = None
    new_board = board_state.copy()
    new_game = Game()
    new_game.board_state = new_board

    for move in new_board.legal_moves:
        new_board = board_state.copy()
        new_board.push(move)

        if board_state.turn and new_board.is_checkmate():
            return 10000, move
        if not board_state.turn and new_board.is_checkmate():
            return -10000, move
        if board_state.turn and new_board.is_game_over():
            return -10, move
        if not board_state.turn and new_board.is_game_over():
            return 10, move
        if max_depth <= 1:
            score = best_move(new_board, new_game, ai)
        else:
            score, _ = min_max(new_board, ai, new_game, max_depth - 1, alpha,
                               beta)

        if board_state.turn:
            if score > alpha:
                # print(score, ' ', best_score, ' ', best_score_move)
                alpha = score
                best_score_move = move
        else:
            if score < beta:
                beta = score
                best_score_move = move
        if alpha >= beta:
            break
    # print(alpha, not game.board_state.turn)
    return alpha if not game.board_state.turn else beta, best_score_move
예제 #2
0
        while True:
            # try:
            #     game.board_state.push(best_move(game, ai, game.board_state.turn))
            # except:
            #     break
            # print('ai move \n')
            # print(game.board_state)
            # if game.board_state.is_checkmate():
            #     shutil.copyfile(path, "random_"+path)
            # if game.board_state.is_game_over():
            #     break
            time_move = time.time()
            # try:
            board_state = game.board_state.copy()
            move = min_max_NN.min_max_player(board_state, ai, game)
            game.board_state = board_state

            game.board_state.push(move)
            print('ai move \n')
            print(game.board_state)
            # if game.board_state.is_checkmate():
            #     shutil.copyfile(path, "NN_" + path)
            if game.board_state.is_game_over():
                break
            # except:
            #     break

            print(time.time() - time_move)
            # input('wcisnij enter')
            # board_state = game.board_state.copy()
            # move = min_max.min_max_player(board_state, ai, game)