递归minimax算法无法深入

问题描述

我正在尝试为CS50AI项目创建一个minimax tictactoe实现。我正在分享我为清楚起见使用的功能,但主要问题是minimax。我希望它是递归的,但问题是:如果发现残局状态,则假定返回best包括最佳移动的元组),否则返回评估编号。因此,我创建了depthcounter。我的想法是,counter总是比depth小一号,一旦算法到达并以minimax的最后一个节点结束游戏状态,它假设又经历了一次循环,因此{{1 }}和depth相等,并返回move(counter)而不是数字。

best

但是问题是""" Tic Tac Toe Player """ import math X = "X" O = "O" EMPTY = None def initial_state(): """ Returns starting state of the board. """ return [[EMPTY,EMPTY,EMPTY],[EMPTY,EMPTY]] def initial_stateh(): return [[X,X,X],EMPTY]] def initial_statetie(): return [[O,O],[X,O,O]] def initial_stateboş(): return [[EMPTY,EMPTY]] def player(board): numx = 0 numo = 0 for i in range(3): for j in range(3): if board[i][j] == X: numx = numx + 1 if board[i][j] == O: numo = numo + 1 if numx == numo: return X elif numx > numo: return O def actions(board): possiblemoves = set() for i in range(3): for j in range(3): if board[i][j] == EMPTY: possiblemoves.add((i,j)) return possiblemoves def result(board,action): """ Returns the board that results from making move (i,j) on the board. """ if action is None: raise Exception("Invalid action") copyboard = [row[:] for row in board] if copyboard[ action[0] ][ action[1] ] is EMPTY: #print(action[0],action[1]) copyboard[ action[0] ][ action[1] ] = player(board) return copyboard else: raise Exception("Move is not possible") def horizontal(board): for x in range(3): if (board[x][0] == board[x][1] and board[x][1] == board[x][2]) and board[x][0] != None: pl = board[x][0] return pl return None def vertical(board): for y in range(3): if (board[0][y] == board[1][y] and board[1][y] == board[2][y]) and board[1][y] != None: pl = board[1][y] return pl return None def diagonally(board): if (board[0][0] == board[1][1] and board[1][1]==board[2][2]) and board[0][0] != None: return board[0][0] if (board[0][2] == board[1][1] and board[1][1]==board[2][0]) and board[0][2] != None: return board[0][2] else: return None def winner(board): """ Returns the winner of the game,if there is one. """ if vertical(board) != None : return vertical(board) if horizontal(board) != None : return horizontal(board) if diagonally(board) != None : return diagonally(board) else: return None def arethereanyspace(board): space = 0 for row in board: for item in row: if item == None: space +=1 else: return space def tie(board): if winner(board) == None and arethereanyspace(board) == None: return True else: return False def terminal(board): """ Returns True if game is over,False otherwise. """ if tie(board) == True or winner(board) is not None: 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. """ counter = 0 depth = 1 if counter == depth and terminal(board): return best if terminal(board) : #if the game has ended return utility(board) print("reached") moveset = actions(board) if player(board) == X:#maximizer maxeval = -2#worst of the worst for action in moveset: value = minimax(result(board,action)) print(value,counter,depth) if value is not None: if value > maxeval: maxeval = value best = action depth = depth + 1 counter = counter + 1 else: mineval =+2 for action in moveset: value = minimax(result(board,depth) if value is not None: if value < mineval: mineval = value best = action depth = depth + 1 counter = counter + 1 #return best 中的value函数由于某种原因变成了minimax。 50秒钟左右之后,永远不会到达print(“ reached”)语句。这是输出的一部分。非常感谢任何帮助。

None

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)