gpt4 book ai didi

python - 超过最大递归深度(无法确定无限循环的原因)

转载 作者:太空宇宙 更新时间:2023-11-03 17:35:16 24 4
gpt4 key购买 nike

希望有人能够理解可能是一些非常困惑的代码。我在空闲时间慢慢接触Python,并决定制作一个井字棋游戏。

问题是,在游戏结束时,如果人类玩家没有获胜,计算机的回合函数就会进入无限循环并使代码崩溃。

代码:

import random
board = [[' ',' ',' ',],[' ',' ',' ',],[' ',' ',' ',]]

def printboard():
spacercount = 0
print " | | "
for row in board:
print row[0] + " | " + row[1] + " | " + row[2]
if spacercount < 2:
print "---------------"
print " | | "
spacercount += 1
print "\n"
print "____________________ \n \n"


def userturn():
x = raw_input("Choose row: ")
y = raw_input("Choose column: ")
print "\n"
if board[int(x)][int(y)] == " ":
board[int(x)][int(y)] = "X"
printboard()
if checkwin("X") == True:
print "player has won!"
elif checkfull() == True:
print "it's a tie!"
else:
computerturn()
else:
print "this space is already taken, try again"
userturn()

def computerturn():
x = random.randint(1,2)
y = random.randint(1,2)
print "\n"
if board[int(x)][int(y)] == " ":
board[int(x)][int(y)] = "O"
printboard()
if checkwin("O") == True:
print "computer has won!"
elif checkfull() == True:
print "it's a tie!"
else:
userturn()
else:
computerturn()

def checkwin(le):
return ((board[0][2] == le and board[1][1] == le and board[2][0] == le) or
(board[0][0] == le and board[1][1] == le and board[2][2] == le) or
(board[0][0] == le and board[1][0] == le and board[2][0] == le) or
(board[0][1] == le and board[1][1] == le and board[2][1] == le) or
(board[0][2] == le and board[1][2] == le and board[2][2] == le) or
(board[0][0] == le and board[0][1] == le and board[0][2] == le) or
(board[1][0] == le and board[1][1] == le and board[1][2] == le) or
(board[2][0] == le and board[2][1] == le and board[2][2] == le))

def checkfull():
for x in board:
if x[0] != " " and x[1] != " " and x[2] != " ":
return True
else:
return False

printboard()
userturn()

我一定已经使用computerturn函数一百次试图确定为什么启动无限循环,但没有成功。

非常感谢任何帮助:)希望代码足够简单,不需要注释,但如果是这样,我将添加它们

最佳答案

首先,你的空:

board = [[' ',' ',' ',],[' ',' ',' ',],[' ',' ',' ',]]

单个空格字符表示一个空方 block 。

接下来,程序在打印电路板后执行的第一件事是调用userturn()。用户做出某种移动,如果成功,程序将调用 computerturn()。但是,计算机是如何移动的呢?

它会在棋盘的一部分(不是全部 - 仅第二和第三列和行)中查找空方 block ,因为 randint(1,2) 而不是 randint(0 ,2))。如果随机选择的方格已被占据,则计算机会尝试再次移动。一旦它检查的所有方格都被占据,它就无法做任何事情,只能尝试再次前进。然后它将重复执行此操作,直到达到最大递归深度为止。

我们如何解决这个问题?我们不会让计算机重复地从整个棋盘中随机选择一个棋步并让它不断尝试直到选择一个有效的棋步,而是将其棋步限制为实际可用的棋步。

def computerturn():
available = [(x, y) for x,row in enumerate(board)
for y,column in enumerate(row)
if board[x][y] == ' ']
# this is a list of actual available moves
x,y = random.choice() # select from actual available moves
# x = random.randint(0,2) # don't want this
# y = random.randint(0,2) # don't want this
print "\n"
# don't need the following conditional
#if board[x][y] == " ": # don't need the int() calls
board[x][y] = "O" # this is the only thing that a successful move should do
printboard() # unindent all this stuff
if checkwin("O"): # don't need to compare a Boolean to True
print "computer has won!"
elif checkfull():
print "it's a tie!"
else:
userturn()

#else: # let's just take out this whole recursive call and fix the random move
# computerturn()

现在是一个“干净”版本,删除了注释和过时的代码:

def computerturn():
available = [(x, y) for x,row in enumerate(board)
for y,column in enumerate(row)
if board[x][y] == ' ']
x,y = random.choice()
print "\n"
board[x][y] = "O"
printboard()
if checkwin("O"):
print "computer has won!"
elif checkfull():
print "it's a tie!"
else:
userturn()

我们hardly even need the comments - 您可以从代码本身看到发生了什么。

关于python - 超过最大递归深度(无法确定无限循环的原因),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31281344/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com