2017-05-05 114 views
2

我對我的代碼的特定部分有問題。 我想創建一個像棋類遊戲的遊戲樹,以使用minimax算法。 問題在於嵌套for循環,每次退出嵌套循環時,遊戲狀態似乎都不會重置。這導致遊戲進行,而不是探索可能的遊戲選項。Python minimax遊戲樹

事項:

  1. available_moves後給定的舉動已取得返回給遊戲狀態(板)可能的行動和玩家
  2. make_move返回板(2D名單)
  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 
+0

你有沒有檢查make_move實際上返回一個新的董事會。根據您的移動功能的定義,完全有可能您只有一個實際的棋盤對象被多個變量引用。發佈make move可以幫助確定是否是這種情況 –

+0

編輯爲包含make_move – fpronios

+0

'board_l = board_o'沒有製作新的電路板。賦值在Python中不起作用。見https://nedbatchelder.com/text/names.html – user2357112

回答

0

解決 正確的方法

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