为什么我的基于minimax算法的井字游戏玩家并不完美?

时间:2015-08-31 13:56:40

标签: python algorithm artificial-intelligence minimax

我正在尝试使用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

1 个答案:

答案 0 :(得分:1)

你应该重新考虑你的体重。

玩家1,例如,尝试最小化Sub WorkbooktoPowerPoint() Dim xlwksht As Worksheet Dim MyRange As String Dim MyRange1 As String 'Define another Range Dim oPPTApp As PowerPoint.Application Dim oPPTShape As PowerPoint.Shape Dim oPPTFile As PowerPoint.Presentation Dim SlideNum As Integer Dim oSlide As Slide Dim strPresPath As String, strExcelFilePath As String, strNewPresPath As String strPresPath = "C:\Users\FYI\PPT1.pptx" strNewPresPath = "C:\Users\FYI\new1.pptx" Set oPPTApp = CreateObject("PowerPoint.Application") oPPTApp.Visible = msoTrue Set oPPTFile = oPPTApp.Presentations.Open(strPresPath) For Each oSlide In oPPTFile.Slides i = oSlide.SlideNumber ' The following line was added after the OPs follow-up If i > ActiveWorkbook.Sheets.Count Then Exit For oSlide.Select MyRange = "B2:B5" MyRange1 = "B8:B11" With ActiveWorkbook.Sheets(i) .Range(MyRange).CopyPicture Appearance:=xlScreen, Format:=xlPicture oSlide.Shapes.Paste.Select With oPPTApp .ActiveWindow.Selection.ShapeRange.Align msoAlignTops, True .ActiveWindow.Selection.ShapeRange.Top = 65 .ActiveWindow.Selection.ShapeRange.Left = 7.2 .ActiveWindow.Selection.ShapeRange.Width = 400 End With .Range(MyRange1).CopyPicture Appearance:=xlScreen, Format:=xlPicture oSlide.Shapes.Paste.Select With oPPTApp .ActiveWindow.Selection.ShapeRange.Align msoAlignBottoms, True .ActiveWindow.Selection.ShapeRange.Top = 250 .ActiveWindow.Selection.ShapeRange.Left = 7.2 .ActiveWindow.Selection.ShapeRange.Width = 400 End With End With Next oPPTApp.Activate oPPTFile.SaveAs strNewPresPath oPPTFile.Close oPPTApp.Quit Set oPPTShape = Nothing Set oPPTFile = Nothing Set oPPTApp = Nothing MsgBox "Presentation Created", vbOKOnly + vbInformation End Sub 函数,宁愿选择绘制(权重winner)而不是他的胜利(权重0)。

如果一个玩家获胜,另一个玩家输了,那么使用1来获得"玩家1获胜",1以及#34 ;球员2获胜"和-1(中立)无论是平局还是未知。