因此,我遵循一个CodeHS课程来构建一个人工智能tic脚趾游戏,但是当我试图完成我的minimax函数时,它返回了第二个板测试(底部的测试)的错误答案,我不知道为什么。请帮帮我!
而且,由于网站要求我填写随机单词,我只想在这里输入一些随机的东西: Lorem ipsum dolor,consectetur adipiscing elit,sed do eiusmod tempor从现在到现在,我的工作一直都是这样的。我不是在谴责它,而是在无意义的地方逃亡。不正常的,不正常的。
board = []
##Copy your check_tie and check_win functions from the previous lesson
# and any other function needed for those functions to work.
def check_col_win(player):
if board[0][0] == board[1][0] == board[2][0] == player:
return True
elif board[0][1] == board[1][1] == board[2][1] == player:
return True
elif board[0][2] == board[1][2] == board[2][2] == player:
return True
else:
return False
def check_row_win(player):
if board[0][0] == board[0][1] == board[0][2] == player:
return True
elif board[1][0] == board[1][1] == board[1][2] == player:
return True
elif board[2][0] == board[2][1] == board[2][2] == player:
return True
else:
return False
def check_diag_win(player):
if board[0][0] == board[1][1] == board[2][2] == player:
return True
elif board[0][2] == board[1][1] == board[2][0] == player:
return True
else:
return False
def check_win(player):
return check_col_win(player) or check_row_win(player) or check_diag_win(player)
def check_tie():
for i in range(3):
for j in range(3):
if board[i][j] == "-":
return False
return True
##Copy over your place_player function
def is_valid_move(row, col):
if board[row][col] == "-":
return True
else:
print("Please enter a valid move")
return False
#places player on row,col on the board
def place_player(player, row, col):
if is_valid_move(row, col):
board[row][col] = player
def minimax(player, optimalRow = -1, optimalCol = -1):
#copy your basecase here:
if check_win("O"):
return (10, optimalRow, optimalCol)
if check_win("X"):
return (-10, optimalRow, optimalCol)
if check_tie():
return (0, optimalRow, optimalCol)
#implement recursive case
if player == "O":
best = -10000
for i in range(3):
for a in range(3):
if board[i][a] == "-":
place_player("O", i, a)
best, optimalRow, optimalCol = max(best, (minimax("X")[0])), (minimax("X")[1]), (minimax("X")[2])
board[i][a] = "-"
return (best, optimalRow, optimalCol)
if player == "X":
worst = 10000
for k in range (3):
for l in range (3):
if board[k][l] == "-":
place_player("X", k, l)
worst, optimalRow, optimalCol = min(worst, (minimax("O")[0])), (minimax("O")[1]), (minimax("O")[2])
board[k][l] = "-"
return (worst, optimalRow, optimalCol)
##Don't edit this code
# It checks to see if your minimax function is working correctly
def print_board():
print("\n")
print("\t0\t\t1\t\t2")
count = 0
for item in board:
row = ""
for space in item:
row += "\t" + space + "\t"
print(count,row + "\n")
count+= 1
board.append(["O","X","-"])
board.append(["-","X","-"])
board.append(["-","-","-"])
print("Calling minimax('O') on this board:")
print_board()
print("Minimax should return (0, 2, 1):", minimax("O"))
board.clear()
print("Calling minimax('O') on this board:")
board.append(["O","X","-"])
board.append(["-","X","X"])
board.append(["-","O","-"])
print_board()
print("Minimax should return (0, 1, 0) ", minimax("O"))
board.clear()
print("Calling minimax('O') on this board:")
board.append(["O","X","X"])
board.append(["O","X","X"])
board.append(["-","O","-"])
print_board()
print("Minimax should return (10, 2, 0) ", minimax("O"))发布于 2022-07-05 19:32:55
老实说,这个问题非常模糊,你应该让我们知道什么是“错误的答案”,而不是你期望的是什么。以下是一些猜测:
你犯了一个很常见的错误,就是在"O“赢的时候,无论是谁赢了,总是能给出一个胜利的分数。当"O“赢而它是"O”回合时,Minimax应该发送一个获胜分数,或者如果"X“赢了,那么它就是"X”回合。
如果"O“赢了,而它是"X”转,那么它应该返回输分,或者如果“X”赢了,那么它就是"O“转。
这可能会解决你的问题。
https://stackoverflow.com/questions/72869996
复制相似问题