- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试实现一个 minimax 算法来创建一个与玩家玩井字游戏的机器人。
gui 功能在另一个文件中并且工作正常。每当轮到机器人移动时,gui 文件就会使用下面提到的代码调用该文件。
我已经在注释中包含了每个函数的作用,我相信除了 minimax() 之外的所有函数都有效
每当我运行脚本时,都会显示此错误:
“RecursionError:相比之下超出了最大递归深度”
如果有什么不清楚的地方做评论,我会尽力简化它。
感谢您的帮助
X = "X"
O = "O"
EMPTY = None
def initial_state():
"""
Returns starting state of the board.
"""
return [[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def player(board):
"""
Returns player who has the next turn on a board.
"""
o_counter = 0
x_counter = 0
for i in board:
for j in i:
if j == 'X':
x_counter += 1
elif j == 'O':
o_counter += 1
if x_counter == 0 and o_counter == 0:
return 'O'
elif x_counter > o_counter:
return 'O'
elif o_counter > x_counter:
return 'X'
def actions(board):
"""
Returns set of all possible actions (i, j) available on the board.
"""
action = []
for i in range(3):
for j in range(3):
if board[i][j] is None:
action.append([i, j])
return action
def result(board, action):
"""
Returns the board that results from making move (i, j) on the board.
"""
p = player(board)
i, j = action
board[i][j] = p
return board
def winner(board):
"""
Returns the winner of the game, if there is one.
"""
if board[0][0] == board[1][1] == board[2][2]:
return board[0][0]
elif board[0][2] == board[1][1] == board[2][0]:
return board[0][2]
else:
for i in range(3):
if board[i][0] == board[i][1] == board[i][2]:
return board[i][0]
elif board[0][i] == board[1][i] == board[2][i]:
return board[0][i]
def terminal(board):
"""
Returns True if game is over, False otherwise.
"""
if winner(board) == 'X' or winner(board) == 'O' :
return True
else:
return False
def utility(board):
"""
Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
"""
if winner(board) == 'X':
return 1
elif winner(board) == 'O':
return -1
else:
return 0
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
return_action = [0, 0]
available_actions = actions(board)
score = 0
temp_board = board
for action in range(len(available_actions)):
temp_score = 0
i, j = available_actions[action]
temp_board[i][j] = player(temp_board)
if winner(temp_board) == 'X' or winner(temp_board) == 'O':
temp_score += utility(temp_board)
else:
minimax(temp_board)
if temp_score > score:
score = temp_score
return_action = action
return available_actions[return_action]
最佳答案
让我们考虑 minimax 算法本身,因为其余的看起来不错:
def minimax(board):
"""
Returns the optimal action for the current player on the board.
"""
return_action = [0, 0]
available_actions = actions(board)
score = 0
temp_board = board
for action in range(len(available_actions)):
temp_score = 0
i, j = available_actions[action]
temp_board[i][j] = player(temp_board)
if winner(temp_board) == 'X' or winner(temp_board) == 'O':
temp_score += utility(temp_board)
else:
minimax(temp_board)
if temp_score > score:
score = temp_score
return_action = action
return available_actions[return_action]
temp_board = board
不制作副本;它只是为同一个板创建一个新的本地名称。因此,当您从递归返回时,试验移动不会被“删除”。 available_actions
(请记住,抽奖游戏是可能的!)。这意味着 for 循环不会运行,最后一个 return
将尝试索引到 available_actions
- 空列表 - 具有无效值(这里的所有内容都无效,但 [0, 0]
的初始设置尤其没有意义,因为它不是整数)。 if temp_score > score:
,无论正在考虑哪个玩家的举动。那是,呃,maximax,并没有给你一个有用的策略。 minimax(temp_board)
,您想知道该板上的分数是多少。所以你的整体函数需要返回一个分数以及建议的移动,当你进行递归调用时,你需要考虑这些信息。 (您可以忽略临时棋盘上的建议走法,因为它只是告诉您算法希望玩家回答什么;但您需要分数,以便您可以确定这一走法是否获胜。)temp_score = 0
,因为我们将通过递归或注意到游戏结束得到答案。 temp_score += utility(temp_board)
也没有道理;我们没有汇总值,而只是使用单个值。 utility
函数来考虑绘制游戏的可能性,并生成候选移动。这为我们提供了一种巧妙的方式来封装“如果游戏赢了,即使有空位也不考虑棋盘上的任何 Action ”的逻辑。 for
循环循环并进行比较,我们可以使用内置的 min
和 max
在递归结果序列上使用函数 - 我们可以通过使用生成器表达式(您将在许多更高级的代码中看到的简洁的 Python 习语)获得。这也为我们提供了一种确保算法的最小和最大阶段交替的巧妙方法:我们只需将适当的函数传递给递归的下一级。 def score_and_candidates(board):
# your 'utility', extended to include candidates.
if winner(board) == 'X':
return 1, ()
if winner(board) == 'O':
return -1, ()
# If the game is a draw, there will be no actions, and a score of 0
# is appropriate. Otherwise, the minimax algorithm will have to refine
# this result.
return 0, actions(board)
def with_move(board, player, move):
# Make a deep copy of the board, but with the indicated move made.
result = [row.copy() for row in board]
result[move[0]][move[1]] = player
return result
def try_move(board, player, move):
next_player = 'X' if player == 'O' else 'O'
next_board = with_move(board, player, move)
next_score, next_move = minimax(next_board, next_player)
# We ignore the move suggested in the recursive call, and "tag" the
# score from the recursion with the current move. That way, the `min`
# and `max` functions will sort the tuples by score first, and the
# chosen tuple will have the `move` that lead to the best line of play.
return next_score, move
def minimax(board, player):
score, candidates = score_and_candidates(board)
if not candidates:
# The game is over at this node of the search
# We report the status, and suggest no move.
return score, None
# Otherwise, we need to recurse.
# Since the logic is a bit tricky, I made a separate function to
# set up the recursive calls, and then we can use either `min` or `max`
# to combine the results.
min_or_max = min if player == 'O' else max
return min_or_max(
try_move(board, player, move)
for move in candidates
)
关于python - 实现极小极大算法时的递归问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61328605/
在本教程中,您将借助示例了解 JavaScript 中的递归。 递归是一个调用自身的过程。调用自身的函数称为递归函数。 递归函数的语法是: function recurse() {
我的类(class) MyClass 中有这段代码: public new MyClass this[int index] { get {
我目前有一个非常大的网站,大小约为 5GB,包含 60,000 个文件。当前主机在帮助我将站点转移到新主机方面并没有做太多事情,我想的是在我的新主机上制作一个简单的脚本以 FTP 到旧主机并下载整个
以下是我对 AP 计算机科学问题的改编。书上说应该打印00100123我认为它应该打印 0010012但下面的代码实际上打印了 3132123 这是怎么回事?而且它似乎没有任何停止条件?! publi
fun fact(x: Int): Int{ tailrec fun factTail(y: Int, z: Int): Int{ if (y == 0) return z
我正在尝试用c语言递归地创建线性链表,但继续坚持下去,代码无法正常工作,并出现错误“链接器工具错误 LNK2019”。可悲的是我不明白发生了什么事。这是我的代码。 感谢您提前提供的大力帮助。 #inc
我正在练习递归。从概念上讲,我理解这应该如何工作(见下文),但我的代码不起作用。 请告诉我我做错了什么。并请解释您的代码的每个步骤及其工作原理。清晰的解释比只给我有效的代码要好十倍。 /* b
我有一个 ajax 调用,我想在完成解析并将结果动画化到页面中后调用它。这就是我陷入困境的地方。 我能记忆起这个功能,但它似乎没有考虑到动画的延迟。即控制台不断以疯狂的速度输出值。 我认为 setIn
有人愿意用通俗易懂的语言逐步解释这个程序(取自书籍教程)以帮助我理解递归吗? var reverseArray = function(x,indx,str) { return indx == 0 ?
目标是找出数组中整数的任意组合是否等于数组中的最大整数。 function ArrayAdditionI(arr) { arr.sort(function(a,b){ return a -
我在尝试获取 SQL 查询所需的所有数据时遇到一些重大问题。我对查询还很陌生,所以我会尽力尽可能地描述这一点。 我正在尝试使用 Wordpress 插件 NextGen Gallery 进行交叉查询。
虽然网上有很多关于递归的信息,但我还没有找到任何可以应用于我的问题的信息。我对编程还是很陌生,所以如果我的问题很微不足道,请原谅。 感谢您的帮助:) 这就是我想要的结果: listVariations
我一整天都在为以下问题而苦苦挣扎。我一开始就有问题。我不知道如何使用递归来解决这个特定问题。我将非常感谢您的帮助,因为我的期末考试还有几天。干杯 假设有一个包含“n”个元素的整数数组“a”。编写递归函
我有这个问题我想创建一个递归函数来计算所有可能的数字 (k>0),加上数字 1 或 2。数字 2 的示例我有两个可能性。 2 = 1+1 和 2 = 2 ,对于数字 3 两个 poss。 3 = 1+
目录 递归的基础 递归的底层实现(不是重点) 递归的应用场景 编程中 两种解决问题的思维 自下而上(Bottom-Up) 自上而下(Top-
0. 学习目标 递归函数是直接调用自己或通过一系列语句间接调用自己的函数。递归在程序设计有着举足轻重的作用,在很多情况下,借助递归可以优雅的解决问题。本节主要介绍递归的基本概念以及如何构建递归程序。
我有一个问题一直困扰着我,希望有人能提供帮助。我认为它可能必须通过递归和/或排列来解决,但我不是一个足够好的 (PHP) 程序员。 $map[] = array("0", "1", "2", "3")
我有数据 library(dplyr, warn.conflicts = FALSE) mtcars %>% as_tibble() %>% select(mpg, qsec) %>% h
在 q 中,over 的常见插图运算符(operator) /是 implementation of fibonacci sequence 10 {x,sum -2#x}/ 1 1 这确实打印了前 1
我试图理解以下代码片段中的递归调用。 static long fib(int n) { return n <= 1 ? n : fib(n-1) + fib(n-2); } 哪个函数调用首先被
我是一名优秀的程序员,十分优秀!