簡體   English   中英

為什么我的基於minimax算法的井字游戲播放器不完美?

[英]Why is my tic-tac-toe player based on minimax algorithm not perfect?

我正在嘗試使用Minimax算法用Python編寫一個完美的井字游戲程序,但我不知道為什么我的程序不能玩完美的井字游戲。 它容易丟失。 有人可以解釋我的代碼中缺少的內容嗎?

import copy

def winner(m):
    #rows 
    for i in range(0,3):
        if m[i][0]==m[i][1] and m[i][0]==m[i][2]:
            if m[i][0]!=0:
                return m[i][0]
    #cols
    for i in range(0,3):
        if m[0][i]==m[1][i] and m[0][i]==m[2][i]:
            if m[0][i]!=0:
                return m[0][i]
    #diagonal right
    if m[0][0]==m[1][1] and m[0][0]==m[2][2]:
        if m[0][0]!=0:
            return m[0][0]
    #diagonal left
    if m[0][2]==m[1][1] and m[0][2]==m[2][0]:
        if m[0][2]!=0:
            return m[0][2]
    #return -1 if the game is still not over
    for i in range(0,3):
        for j in range(0,3):
            if m[i][j]==0:
                return -1
    #tie
    return 0

#finds out possible moves
def possible_moves(m):
    lst=[]
    for i in range(0,3):
        for j in range(0,3):
            if m[i][j]==0:
                lst.append((i,j))
    return lst

def tic_tac_toe(m,r,c,computer,no_of_moves=0):
    if computer: #initial value of computer = False
        m[r][c]=2 #2 is for human player
    else: 
        m[r][c]=1 #1 is for computer
    computer=not computer

    score = winner(m)
    if score==1:
        return 10-no_of_moves
    elif score==2:
        return no_of_moves-10
    elif score==0:
        return 0

    moves = possible_moves(m)
    score_lst = []
    for i in moves:
        m2 = copy.deepcopy(m)
        score_lst.append(tic_tac_toe(m2, i[0], i[1], computer,no_of_moves+1))

    if computer:
        return max(score_lst)
    if not computer:
        return min(score_lst)


#game play
import numpy as np

def game_play():
    m=[[0,0,0],
       [0,0,0],
       [0,0,0]]
    m = np.array(m)
    while True:
        moves = possible_moves(m)
        #computer's move
        score_lst2=[]
        m2=copy.deepcopy(m)
        for i in moves:
            score_lst2.append(tic_tac_toe(m2, i[0], i[1], computer=False))
        max_move_index = score_lst2.index(max(score_lst2))
        move = moves[max_move_index]
        m[move[0]][move[1]]=1
        print(m)
        if winner(m)==1:
            print("computer wins")
            break
        elif winner(m)==0:
            print("tie")
            break
        #human move
        r,c = map(int,input("enter row and col: ").split())
        m[r][c]=2
        print(m)
        if winner(m)==2:
            print("human wins")
            break
        elif winner(m)==0:
            print("tie")
            break
game_play()

產量

以下只是我擊敗了程序的一個游戲示例。 電腦邁出了第一步。

[[0 0 1]
 [0 0 0]
 [0 0 0]]
enter row and col: 1 1
[[0 0 1]
 [0 2 0]
 [0 0 0]]
[[0 1 1]
 [0 2 0]
 [0 0 0]]
enter row and col: 0 0
[[2 1 1]
 [0 2 0]
 [0 0 0]]
[[2 1 1]
 [0 2 0]
 [0 0 1]]
enter row and col: 1 2
[[2 1 1]
 [0 2 2]
 [0 0 1]]
[[2 1 1]
 [0 2 2]
 [0 1 1]]
enter row and col: 1 0
[[2 1 1]
 [2 2 2]
 [0 1 1]]
human wins

您應該重新考慮體重。

例如,試圖最小化winner功能的玩家1寧願選擇平局(權重0 ),也不願尋求勝利(權重1 )。

在井字游戲中,如果一名玩家獲勝,而另一名則輸掉,所以在“玩家1獲勝”中使用1在“玩家2獲勝”中使用-1 ,對於平局或未知使用0 (中立)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM