井字游戏中极小极大算法的错误输出

代码执行没有错误,但极小极大算法的输出不正确,请查看。`AI_makemove`函数是从主循环中调用的,`board_state`是实际棋盘的副本。`AI_makemove`函数应该返回计算机对抗用户的最佳移动,`board_state`是当前棋盘状态,`depth`是棋盘上已填充的位置数量,`check_if_won`函数如果当前玩家的状态为胜利状态,则返回true。

def AI_makemove(board_state , isAI , depth):temp_board = copy.deepcopy(board_state)depth+=1print(temp_board , depth , isAI)if isAI:    bestVal = -9999    a = b = 0    for i in range(0,3):        for j in range(0,3):            if temp_board[i][j] == 0:                temp_board1  = copy.deepcopy(temp_board)                temp_board1[i][j] = 2                if check_if_won(2,temp_board1):                    return [1 , i, j]                if depth == 9:                    return [bestVal , a ,b]                l = AI_makemove(temp_board1,False,depth)                if int(l[0]) > bestVal:                    bestVal = int(l[0])                    a = int(l[1])                    b = int(l[2])else:    bestVal = +9999    a = b = 0    for i in range(0, 3):        for j in range(0, 3):            if temp_board[i][j] == 0:                temp_board1  = copy.deepcopy(temp_board)                temp_board1[i][j] = 1                if check_if_won(1,temp_board1):                    return [-1 , i, j]                if depth == 9:                    return [bestVal , a ,b]                l = AI_makemove(temp_board1,True,depth)                if int(l[0]) < bestVal:                    bestVal = int(l[0])                    a = int(l[1])                    b = int(l[2])return [bestVal , a ,b]

回答:

我尝试了几次调试代码,但无法修复它,所以我用不同的方法重新编写了代码,结果它工作了。这是代码

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注