TicTacToe(Python)的Minimax无法正常工作

时间:2018-11-13 06:12:03

标签: python

game:具有GameState类的游戏模块,其中包含“ current_turn”和“ board”字段。 “ current_turn”是“ player”或“ ai”,并且“ board”被初始化为具有所有空插槽的3x3矩阵(一个空插槽表示为“”)

我不相信我的问题出在getEmpty()或relative_player()中,我认为我的minimax逻辑是正确的。我不确定哪里出了问题。当至少应该与我联系游戏时,AI仍然会表现得很呆板(相反,它仍然会失败)。我还测试了游戏中的所有其他方法,所以我认为我在minimax中的逻辑在某处出错。

(同样,state.getBoard()返回矩阵的深层副本,所以这不是问题)

def getEmpty(state): 
    """Returns empty slots in given state's board """
    board = state.getBoard() 
    unoccupied_slots=[] 
    row,col=0,0 
    while row<3:
        while col<3: 
            if board[row][col] == " ":
                unoccupied_slots.append((row,col))
            col+=1 
        col=0 
        row+=1 

    return unoccupied_slots 




def opposite_player(state): 
    """Returns opposing player """
    if state.getTurn()=='ai':
        return 'player' 
     return 'ai' 




 def minimax(state): 
    """Minimax algorithm """

    possible_tiles=getEmpty(state) 

    #TERMINAL POSSIBLITIES 

    #player has won 
    if game.winning_board(state) and state.getTurn()=='player': 
        return Move(-10)

    #ai has won 
    elif game.winning_board(state) and state.getTurn()=='ai':
        return Move(10) 

    #tie 
    elif (possible_tiles==[] or game.isTie(state)):
        return Move(0) 


    moves=[None for i in range(len(possible_tiles))] #move_list to fill up 


    tile_idx=0 

    for tile in possible_tiles: 

        #get board created by playing move
        board=state.getBoard() 
        if state.getTurn()=='ai':  
            board[tile[0]][tile[1]]='O' 
        else:
            board[tile[0]][tile[1]]='X' 

        #get possible subsequent state 
        possible_state=game.GameState(opposite_player(state))
        possible_state.setBoard(board) 

        #record score 
        curr_move=minimax(possible_state)
        curr_move.placement=tile 
        moves[tile_idx]=curr_move 


        tile_idx+=1 

    #ai wants to maximize its score 

    if state.getTurn()=='ai':
        bestScore=-10000 
        i=0
        while i<len(moves):
            if moves[i].score>bestScore:
                bestScore=moves[i].score 
                bestMove=i 
            i+=1 
        return moves[bestMove] 

    else:
        bestScore=10000
        j=0
        while j<len(moves):
            if moves[j].score<bestScore:
                bestScore=moves[j].score 
                bestMove=j 
            j+=1 
        return moves[bestMove]  

0 个答案:

没有答案