【问题标题】:Python minimax Game treePython minimax 博弈树
【发布时间】:2017-05-05 11:36:38
【问题描述】:

我在代码的特定部分遇到问题。 我正在尝试为类似国际象棋的游戏创建一个游戏树,以使用 minimax 算法。 问题在于嵌套的for循环,每次退出嵌套循环时游戏状态似乎都不会重置。这会导致游戏进行而不是探索可能的游戏选项。

须知:

  1. available_moves 返回给定游戏状态(棋盘)和玩家的可能移动
  2. make_move 在进行给定移动后返回棋盘(二维列表)
  3. 我通过leaf_cnt变量跟踪叶子节点的数量
  4. make_move 参数功能正常(经过测试)

    def minimax_3 (board,myColor,depth=4):
    board_s = board
    player = myColor
    leaf_cnt = 0
    
    l=1
    
    for m1 in available_moves(board_s,l%2):
        board2 = make_move(int(m1[0]), int(m1[1]), int(m1[2]), int(m1[3]), 9, 9, board_s)
    
        l=2
        for m2 in available_moves(board2, l % 2):
    
            board3 = make_move(int(m2[0]), int(m2[1]), int(m2[2]), int(m2[3]), 9, 9, board2)
    
            l = 3
            for m3 in available_moves(board3, l % 2):
    
                board4 = make_move(int(m3[0]), int(m3[1]), int(m3[2]), int(m3[3]), 9, 9, board3)
    
                leaf_cnt+=1
    
    
    print leaf_cnt
    

编辑:错误的方式

def make_move(x1,y1,x2,y2,px,py,board_o):

board_l = board_o
board_l[x2][y2]=board_l[x1][y1]
board_l[x1][y1]= " "

if px!=9 and py!=9:
    board_l[px][py] = "P"

for i in range(5):
    if board_l[6][i]=="BP":
        board_l[6][i]=" "
    if board_l[0][i] == "WP":
        board_l[0][i] = " "

return board_l

【问题讨论】:

  • 您是否检查过 make_move 实际上返回了一个新板。根据您的移动功能的定义,您完全有可能只有一个被多个变量引用的实际棋盘对象。发布 make move 可以帮助确定是否是这种情况
  • 编辑包括 make_move
  • board_l = board_o 不会制作新板。赋值在 Python 中不是这样工作的。见nedbatchelder.com/text/names.html
  • 非常感谢你。我为不理解这一点而感到羞耻,我解决了这个问题,程序现在可以完美运行。

标签: python python-2.7 artificial-intelligence


【解决方案1】:

解决了 正确的方式

def make_move(x1,y1,x2,y2,px,py,board_o):
w, h = 5, 7;
board_l = [[board_o[y][x] for x in range(w)] for y in range(h)]
board_l[x2][y2]=board_l[x1][y1]
board_l[x1][y1]= " "

if px!=9 and py!=9:
    board_l[px][py] = "P"

for i in range(5):
    if board_l[6][i]=="BP":
        board_l[6][i]=" "
    if board_l[0][i] == "WP":
        board_l[0][i] = " "

return board_l

非常感谢@user2357112

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多