首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用python的Tic,Tac,Toe中的Minimax算法:递归不会结束

使用python的Tic,Tac,Toe中的Minimax算法:递归不会结束
EN

Stack Overflow用户
提问于 2018-12-04 04:09:57
回答 1查看 459关注 0票数 0

我是编程新手,想用极小极大算法编程一个Tic,tac,toe求解器。当我测试我的程序时,它返回:在比较中超过了最大递归深度。我不知道为什么我的递归不会停止。有没有人可以帮我解决这个问题?请随时给我一些关于如何改进代码的技巧。

代码语言:javascript
复制
# in the scores list the scores of the moves are kept
scores = []
# in the empty_spots list all indices of the empty cells in the grid are kept
empty_spots = []

# function, which prints the grid
def show_grid(grid):
    a = 0
    for cell in grid:
        a = a + 1
        if cell == 1:
            if a < 3:
                print("X", end="")
            else:
                print("X")
                a = 0
        elif cell == -1:
            if a < 3:
                print("O", end="")
            else:
                print("O")
                a = 0
        else:
            if a < 3:
                print("_", end="")
            else:
                print("_")
                a = 0

# function which checks if there is a victory or draw
def check_victory(grid, player):
    Victory_Combos = [[0, 1, 2], [3, 4, 5], [6, 7, 8], [0, 3, 6], [1, 4, 7], [2, 5, 8], [0, 4, 8], [6, 4, 2]]
    for victorys in Victory_Combos:
        if grid[victorys[0]] == player*-1 and grid[victorys[1]] == player*-1 and grid[victorys[2]] == player*-1:
            return -10 * player
    if 0 not in grid:
        return 0

# function which finds all empty spots
def find_empty_spots(grid):
    for cell in range(9):
        if grid[cell] == 0:
            empty_spots.append(cell)
    return empty_spots



# minimax function
def minimax(grid, player, best_score, depth):
    if check_victory(grid, player) != None:
        return check_victory(grid, player)
    list = find_empty_spots(grid)
    for cell in list:
        grid[cell] = player
        scores.append(minimax(grid, player*-1, 1000*-player, depth + 1))
        if player == 1:
            best_score = -1000
            for score in scores:
                if best_score < score:
                    best_score = score
        else:
            best_score = 1000
            for score in scores:
                if best_score > score:
                    best_score = score
        grid[cell] = 0
    if depth == 0:
        grid[scores.index(best_score)] = player
        show_grid()
    scores.clear()
    return best_score



# example
print(minimax([-1,1,-1,1,1,0,1,-1,0],-1,1000,0))

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-04 04:19:34

问题出在你的empty_spots,你永远不会清除它,因此你总是检查以前的(空)单元格。

代码语言:javascript
复制
def find_empty_spots(grid):
    empty_spots = []
    for cell in range(9):
        if grid[cell] == 0:
            empty_spots.append(cell)
    return empty_spots

添加了empty_spots = []以在每次调用之前清除列表,否则您只是将单元格附加到已存在的列表中。

还有一件事- list = find_empty_spots(grid)是非常非常错误的,不要使用关键字作为变量名,正确的方法应该是lst = find_empty_spots(grid),或者更好的,一些有意义的名称,empty_cells = find_empty_spots(grid)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53601133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档