10.带人机对战的五子棋程序

时间:2022-07-22
本文章向大家介绍10.带人机对战的五子棋程序,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

今天我们带来一个带人机对战功能的五子棋程序。程序基于前面文章中的框架搭建,新增人机对战的策略。程序基于规则进行决策,不考虑禁手,玩家执黑子先行。棋盘规模采用15乘15,棋盘规模容易扩展,程序棋力中等,可以用来放松陪玩。

image.png

程序每一步都会搜索棋盘所有可以落子的位置,然后识别出最值得落子的位置,实现进攻和围堵。目前评估值为人为设置,所以程序棋力只能与编者相当,勉强算作中等棋力,后期若引入遗传算法或者强化学习,通过养蛊之法,可以养出更高棋力的程序。部分代码如下(简书中插入代码还是比较方便的:-):

#带AI的五子棋程序
import wx
UNIT=30
ROW_NODE_NUM=15
BOARD = []
for i in range(UNIT, UNIT * ROW_NODE_NUM + UNIT, UNIT):
    for j in range(UNIT, UNIT * ROW_NODE_NUM + UNIT, UNIT):
        BOARD.append((j, i))
print(BOARD)
class myFrame(wx.Frame):
    def __init__(self):
        self.unit = UNIT
        self.pointNum = ROW_NODE_NUM#每行落棋点数
        self.pieceNum=0
        self.bkCol=(220, 210, 0)
        self.wht=(255,255,255)
        self.blk=(0,0,0)
        self.actColor=self.blk
        self.piecePos =[]
        self.piecePosCols =[]
        # 元组列表记录落棋位置和落棋颜色
        super().__init__
            (parent=None,pos=[800,100],
             size=[self.unit*self.pointNum
                   +self.unit+20,
                   self.unit*self.pointNum
                   +self.unit+30+20],
             title="商贾三国")
        self.SetIcon(wx.Icon("WeatherBundle.ico"))
        self.panel = wx.Panel(self)
        self.panel.SetBackgroundColour(self.bkCol)
        self.tip = 
            wx.TextCtrl(self.panel, -1, "",
                        pos=(self.unit*self.pointNum
                             +self.unit-80, 0),
                        size=(80,25))
        self.tip.SetBackgroundColour(self.bkCol)
        self.panel.Bind(wx.EVT_PAINT,self.draw)
        self.panel.Bind(wx.EVT_LEFT_UP, self.OnClick)
        self.Show()
    def draw(self,event):
        mydc=wx.PaintDC(self.panel)
        unit=self.unit
        pointNum=self.pointNum
        x=unit
        y=unit
        for i in range(1,pointNum+1):
            mydc.DrawLine(x,y,x,unit*pointNum)
            x=x+unit
        x=unit
        for i in range(1,pointNum+1):
            mydc.DrawLine(x, y, unit*pointNum, y)
            y=y+unit
        for i in range(0,len(self.piecePos)):
            mydc.SetBrush(wx.Brush(self.piecePosCols[i][2]))
            mydc.DrawCircle(self.piecePos[i][0],
                            self.piecePos[i][1], self.unit / 2.5)
    def OnClick(self,event):
        unit=self.unit
        pos = event.GetPosition()
        mydc=wx.ClientDC(self.panel)
        mydc.SetBrush(wx.Brush(self.actColor))
        x = round(pos.x / unit) * unit
        y = round(pos.y / unit) * unit
        piecePo = (x, y)
        if piecePo in BOARD:
            if piecePo not in self.piecePos:
                if self.actColor == self.blk:
                    piecePoCol=(x,y,self.actColor)
                    if piecePo not in self.piecePos:
                        mydc.DrawCircle(x,y,self.unit/2.5)
                        self.piecePos.append(piecePo)
                        self.piecePosCols.append(piecePoCol)
                        self.fiveChk(piecePo)
                        self.pieceNum = self.pieceNum+1
                        print(self.pieceNum)
                        self.tip.SetValue('%s,%s' % (x,y))
                        self.actColor = self.wht
                if self.actColor == self.wht:
                    self.board()
                    print(self.result)
                    mydc.SetBrush(wx.Brush(self.actColor))
                    x = self.result[0]
                    y = self.result[1]
                    piecePo = (x, y)
                    piecePoCol = (x, y, self.actColor)
                    if piecePo not in self.piecePos:
                        mydc.DrawCircle(x, y, self.unit / 2.5)
                        self.piecePos.append(piecePo)
                        self.piecePosCols.append(piecePoCol)
                        self.fiveChk(piecePo)
                        self.pieceNum = self.pieceNum + 1
                        print(self.pieceNum)
                        self.tip.SetValue('%s,%s' % (x, y))
                        self.actColor = self.blk

总体代码比较多,限于篇幅,获取完整源码可以移步公众号:哈哈哈利,回复五子棋人机对战,可以获取决策部分代码。

五子棋程序已经打包为exe,可以脱离Python环境,程序获取路径如下:链接:https://pan.baidu.com/s/19U9AZa0Msoyt6xs-OpuDNA 提取码:q940 感兴趣可以用来放松娱乐。