Python 机器学习实战(二):三个人也能联机下五子棋?怎么赢?

简介: Python 机器学习实战(二):三个人也能联机下五子棋?怎么赢?

目录

0 写在前面

人工智能被广泛用于棋类对弈的主要原因是:


棋类对弈自古以来就被认为是人类智力活动的象征,若人工智能成功达到、甚至高于人类水平,则就代表AI的发展潜力,从而吸引更多研究者关注并投身其中;


棋类很适合作为新AI算法的标杆。棋类游戏规则简洁、输赢都在盘面,适合计算机求解。理论上只要在计算能力和算法上有新的突破,任何新的棋类游戏都有可能得到攻克。而在棋类游戏上的表现也可以直观体现出AI之间计算能力与算法的高低,是促进AI算法发展的有效途径。


就本五子棋智能对弈系统 而言,其服务对象为同局域网下的多个终端,讲求联机互动、互相限制、互相博弈,打破了传统二人对弈五子棋规则中,“若无禁手,先手易胜;若为后手,十堵九输”的说法,是AI算法设计、网络通信、数据库等技术的综合应用。

1 效果展示


image.png

2 项目需求与技术选型

系统可以正确判定胜负——以率先连续五子连成一条线的玩家为胜

支持三名玩家参与游戏且具有合理的UI界面,三人各执黑、白、黄三色棋子

系统包含对战服务平台,用以管理玩家对战、判定胜负、记录走子轨迹等

支持三个相同或有所不同的智能对弈Agent

能够在服务端记录并保存所有玩家历史走子记录或棋盘状态变化历史,并能够在客户端中回放

能在游戏对弈过程中根据走子数据动态输出对每个玩家水平的战斗力评级,以及每个玩家对自身当前态势即输赢概率的估计

对战服务平台可以将状态、评级等数据实时发送给客户端

总结起来,最终的效果就是:局域网内三个玩家联机,由对战服务器协调游戏进程,三个玩家可以是真人也可以是AI。


技术选型方面,前端使用PyQt,后端使用Flask,数据库使用MySQL

3 主要接口设计

8ad18603516b4e24980a27b5cd3a5404.png

上图所示是本项目在对弈过程中用到的主要接口。


image.png

上图所示是本项目在玩家管理中用到的主要接口。

4 项目流程

4.1 登录注册


image.png

其中选子界面如图所示,用户可自主选择喜欢的颜色。客户端选子界面与服务器通过接口 /rest/player 产生交互,选择的颜色若未被其他玩家选择,服务器会返回一条成功信息给用户,并提交颜色注册信息到数据库;若该颜色已被其他玩家选择,服务器在提交用户注册信息到数据库时,会返回核验失败信息,客户端相应弹出“颜色已被选择”提示。


image.png

模式选择界面如图所示,用户可以自主选择“真人对战”或“AI对战”。选择后客户端记录相应模式。


image.png

等待界面如图所示,本系统支持3人对战,在玩家人数未达到要求时,所有玩家都将进入等待界面——类似于“游戏大厅”。此时界面通过 /rest/play 接口与服务器发生交互,服务器不断审查数据库的玩家注册信息,直至人数达标。基于PyQT封装的定时器属性Timer,当轮询到数据库返回成功消息后,Timer激活相应的槽函数,进入“中断服务”,在“中断服务”内使能“完成”按钮,并设置进度条达到100%


image.png

4.2 智能走子


image.png

下棋权限即是棋局状态State,由客户端与服务器通过 /rest/play/state 接口交互产生。


当State为1时即表示“轮到己方”,此时根据前设的模式决定不同的响应事件:“真人对战”——鼠标点击事件、“AI对战”——Agent走子事件,完成后再通过 /rest/board接口将走子信息送达服务器记入数据库,并广播给其他客户端。


当State为0时即表示“轮到他方”,此时通过接口 /rest/play/state 获取其他客户端广播的走子信息,实现对战情况的实时更新。

5 项目实现

前端业务代码不一一展示,主要列出后端数据处理的逻辑。只要运行本文实现的后端服务器,采取任意方式的前端页面访问接口都能实现相应功能,包括但不限于PyQtC#MFCVue等方案。

5.1 用户管理

# 针对所有的用户的操作
class UserResource(Resource):
    # get 请求的处理
    # marshal 维持秩序,可以定制显示哪些数据(是有序的字典)
    @marshal_with(user_fields)
    def get(self):
        users = User.query.all()
        return users   # 也可以users[0]
    @marshal_with(user_fields)
    def post(self):
        # 获取数据
        args = parser.parse_args()  # 把验证通过的所有数据都放到一个字典里
        username = args.get('username')
        password = args.get('password')
        # 创建user对象
        user = User()
        user.username = username
        user.password = password
        db.session.add(user)
        db.session.commit()
        return user  # 返回创建成功的对象
    # put
    def put(self):
        return {'msg': '------>put'}
    # delete
    def delete(self):
        return {'msg': '------>delete'}

5.2 选子与游戏大厅玩家等待

# url:/rest/play
# 玩家在选完子之后向服务端不断发起提问,直至收到1
class RestPlayResource(Resource):
    # get 请求的处理
    def get(self):
        result1 = Player.query.filter_by(ID=1).first()
        if result1 is None:
            playstate1 = 0  # 执黑棋的人
        else:
            playstate1 = 1
        result2 = Player.query.filter_by(ID=2).first()
        if result2 is None:
            playstate2 = 0
        else:
            playstate2 = 1
        result3 = Player.query.filter_by(ID=3).first()
        if result3 is None:
            playstate3 = 0
        else:
            playstate3 = 1
        if playstate1 and playstate2 and playstate3:
            playstate = 1
        else:
            playstate = 0
        return {"PlayState": playstate}
# url:/rest/player
# 判断玩家是否可以选择这个颜色的棋子,选子成功则注册玩家信息
class RestPlayerResource(Resource):
    # post 请求的处理
    def post(self):
        # 获取数据
        # global CountPlayers
        args = parser.parse_args()  # 把验证通过的所有数据都放到一个字典里
        id = args.get('id')
        color = args.get('color')
        # 查询数据库中有没有重复的id
        result = Player.query.filter_by(ID=id).first()
        if result is None:
            # 创建player对象,并加入数据库
            player = Player()
            player.ID = id
            player.Player = color
            player.Enable = 0
            db.session.add(player)
            db.session.commit()
            if id == 1:
                msg = "您当前执棋为黑棋"
                config.add_value("CountPlayers", 1)
                # CountPlayers = CountPlayers + 1
            elif id == 2:
                msg = "您当前执棋为白棋"
                config.add_value("CountPlayers", 1)
                # CountPlayers = CountPlayers + 1
            else:
                msg = "您当前执棋为黄棋"
                config.add_value("CountPlayers", 1)
                # CountPlayers = CountPlayers + 1
            if config.get_value("CountPlayers") == 3:
                # if CountPlayers == 3:   如果三个人都到齐了 删掉上一次的走子信息表
                Pieces.query.delete()
                db.session.commit()
                config.init_board()   # 清全局变量
            return {"succ": 1, "msg": msg}
        else:
            msg = "该颜色已被选择!请重新选择。"
            return {"succ": 0, "msg": msg}

5.3 AI智能走子

def judge(JudgeId, x, y):
    # global WinnerId, WinX1, WinX2, WinY1, WinY2, BoardSize
    # 新建函数内变量 修改完成后提交全局变量
    boardsizetemp = config.get_value("BoardSize")
    checkerboardtemp = config.get_value("checkerboard")
    winx1temp = config.get_value("WinX1")
    winx2temp = config.get_value("WinX2")
    winy1temp = config.get_value("WinY1")
    winy2temp = config.get_value("WinY2")
    winneridtemp = config.get_value("WinnerId")
    # 判断算法
    # 判断最后一子竖排
    if y - 4 >= 0 and y <= boardsizetemp - 1:
        if checkerboardtemp[x][y - 1] == JudgeId \
                and checkerboardtemp[x][y - 2] == JudgeId \
                and checkerboardtemp[x][y - 3] == JudgeId \
                and checkerboardtemp[x][y - 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x
            winy1temp = y
            winy2temp = y - 4
    if y - 3 >= 0 and y + 1 <= boardsizetemp - 1:
        if checkerboardtemp[x][y - 3] == JudgeId \
                and checkerboardtemp[x][y - 2] == JudgeId \
                and checkerboardtemp[x][y - 1] == JudgeId \
                and checkerboardtemp[x][y + 1] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x
            winy1temp = y + 1
            winy2temp = y - 3
    if y - 2 >= 0 and y + 2 <= boardsizetemp - 1:
        if checkerboardtemp[x][y - 2] == JudgeId \
                and checkerboardtemp[x][y - 1] == JudgeId \
                and checkerboardtemp[x][y + 1] == JudgeId \
                and checkerboardtemp[x][y + 2] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x
            winy1temp = y + 2
            winy2temp = y - 2
    if y - 1 >= 0 and y + 3 <= boardsizetemp - 1:
        if checkerboardtemp[x][y - 1] == JudgeId \
                and checkerboardtemp[x][y + 1] == JudgeId \
                and checkerboardtemp[x][y + 2] == JudgeId \
                and checkerboardtemp[x][y + 3] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x
            winy1temp = y + 3
            winy2temp = y - 1
    if y >= 0 and y + 4 <= boardsizetemp - 1:
        if checkerboardtemp[x][y + 1] == JudgeId \
                and checkerboardtemp[x][y + 2] == JudgeId \
                and checkerboardtemp[x][y + 3] == JudgeId \
                and checkerboardtemp[x][y + 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x
            winy1temp = y + 4
            winy2temp = y
            # 判断最后一子横排
    if x - 4 >= 0 and x <= boardsizetemp - 1:
        if checkerboardtemp[x - 1][y] == JudgeId \
                and checkerboardtemp[x - 2][y] == JudgeId \
                and checkerboardtemp[x - 3][y] == JudgeId \
                and checkerboardtemp[x - 4][y] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x - 4
            winy1temp = y
            winy2temp = y
    if x - 3 >= 0 and x + 1 <= boardsizetemp - 1:
        if checkerboardtemp[x - 3][y] == JudgeId \
                and checkerboardtemp[x - 2][y] == JudgeId \
                and checkerboardtemp[x - 1][y] == JudgeId \
                and checkerboardtemp[x + 1][y] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 1
            winx2temp = x - 3
            winy1temp = y
            winy2temp = y
    if x - 2 >= 0 and x + 2 <= boardsizetemp - 1:
        if checkerboardtemp[x - 2][y] == JudgeId \
                and checkerboardtemp[x - 1][y] == JudgeId \
                and checkerboardtemp[x + 1][y] == JudgeId \
                and checkerboardtemp[x + 2][y] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 2
            winx2temp = x - 2
            winy1temp = y
            winy2temp = y
    if x - 1 >= 0 and x + 3 <= boardsizetemp - 1:
        if checkerboardtemp[x - 1][y] == JudgeId \
                and checkerboardtemp[x + 1][y] == JudgeId \
                and checkerboardtemp[x + 2][y] == JudgeId \
                and checkerboardtemp[x + 3][y] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 3
            winx2temp = x - 1
            winy1temp = y
            winy2temp = y
    if x >= 0 and x + 4 <= boardsizetemp - 1:
        if checkerboardtemp[x + 1][y] == JudgeId \
                and checkerboardtemp[x + 2][y] == JudgeId \
                and checkerboardtemp[x + 3][y] == JudgeId \
                and checkerboardtemp[x + 4][y] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 4
            winx2temp = x
            winy1temp = y
            winy2temp = y
        # 判断最后一子45°
    if x - 4 >= 0 and y - 4 >= 0 and x <= boardsizetemp - 1 and y <= boardsizetemp - 1:
        if checkerboardtemp[x - 1][y - 1] == JudgeId \
                and checkerboardtemp[x - 2][y - 2] == JudgeId \
                and checkerboardtemp[x - 3][y - 3] == JudgeId \
                and checkerboardtemp[x - 4][y - 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x - 4
            winy1temp = y
            winy2temp = y - 4
    if x - 3 >= 0 and y - 3 >= 0 and x + 1 <= boardsizetemp - 1 and y + 1 <= boardsizetemp - 1:
        if checkerboardtemp[x + 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 1][y - 1] == JudgeId \
                and checkerboardtemp[x - 2][y - 2] == JudgeId \
                and checkerboardtemp[x - 3][y - 3] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 1
            winx2temp = x - 3
            winy1temp = y + 1
            winy2temp = y - 3
    if x - 2 >= 0 and y - 2 >= 0 and x + 2 <= boardsizetemp - 1 and y + 2 <= boardsizetemp - 1:
        if checkerboardtemp[x + 2][y + 2] == JudgeId \
                and checkerboardtemp[x + 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 1][y - 1] == JudgeId \
                and checkerboardtemp[x - 2][y - 2] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 2
            winx2temp = x - 2
            winy1temp = y + 2
            winy2temp = y - 2
    if x - 1 >= 0 and y - 1 >= 0 and x + 3 <= boardsizetemp - 1 and y + 3 <= boardsizetemp - 1:
        if checkerboardtemp[x + 3][y + 3] == JudgeId \
                and checkerboardtemp[x + 2][y + 2] == JudgeId \
                and checkerboardtemp[x + 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 1][y - 1] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 3
            winx2temp = x - 1
            winy1temp = y + 3
            winy2temp = y - 1
    if x >= 0 and y >= 0 and x + 4 <= boardsizetemp - 1 and y + 4 <= boardsizetemp - 1:
        if checkerboardtemp[x + 1][y + 1] == JudgeId \
                and checkerboardtemp[x + 2][y + 2] == JudgeId \
                and checkerboardtemp[x + 3][y + 3] == JudgeId \
                and checkerboardtemp[x + 4][y + 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 4
            winx2temp = x
            winy1temp = y + 4
            winy2temp = y
    # 判断最后一子135°
    if x - 4 >= 0 and y + 4 <= boardsizetemp - 1 and x <= boardsizetemp - 1 and y >= 0:
        if checkerboardtemp[x - 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 2][y + 2] == JudgeId \
                and checkerboardtemp[x - 3][y + 3] == JudgeId \
                and checkerboardtemp[x - 4][y + 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x
            winx2temp = x - 4
            winy1temp = y
            winy2temp = y + 4
    if x - 3 >= 0 and y + 3 <= boardsizetemp - 1 and x + 1 <= boardsizetemp - 1 and y - 1 >= 0:
        if checkerboardtemp[x - 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 2][y + 2] == JudgeId \
                and checkerboardtemp[x - 3][y + 3] == JudgeId \
                and checkerboardtemp[x + 1][y - 1] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 1
            winx2temp = x - 3
            winy1temp = y - 1
            winy2temp = y + 3
    if x - 2 >= 0 and y + 2 <= boardsizetemp - 1 and x + 2 <= boardsizetemp - 1 and y - 2 >= 0:
        if checkerboardtemp[x - 1][y + 1] == JudgeId \
                and checkerboardtemp[x - 2][y + 2] == JudgeId \
                and checkerboardtemp[x + 1][y - 1] == JudgeId \
                and checkerboardtemp[x + 2][y - 2] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 2
            winx2temp = x - 2
            winy1temp = y - 2
            winy2temp = y + 2
    if x - 1 >= 0 and y + 1 <= boardsizetemp - 1 and x + 3 <= boardsizetemp - 1 and y - 3 >= 0:
        if checkerboardtemp[x - 1][y + 1] == JudgeId \
                and checkerboardtemp[x + 1][y - 1] == JudgeId \
                and checkerboardtemp[x + 2][y - 2] == JudgeId \
                and checkerboardtemp[x + 3][y - 3] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 3
            winx2temp = x - 1
            winy1temp = y - 3
            winy2temp = y + 1
    if x >= 0 and y <= boardsizetemp - 1 and x + 4 <= boardsizetemp - 1 and y - 4 >= 0:
        if checkerboardtemp[x + 1][y - 1] == JudgeId \
                and checkerboardtemp[x + 2][y - 2] == JudgeId \
                and checkerboardtemp[x + 3][y - 3] == JudgeId \
                and checkerboardtemp[x + 4][y - 4] == JudgeId:
            winneridtemp = JudgeId
            winx1temp = x + 4
            winx2temp = x
            winy1temp = y
            winy2temp = y - 4
    else:
        pass
    config.set_value("WinnerId", winneridtemp)
    config.set_value("WinX1", winx1temp)
    config.set_value("WinX2", winx2temp)
    config.set_value("WinY1", winy1temp)
    config.set_value("WinY2", winy2temp)

6 项目实际运行展示


image.png

image.png

结果表明:三个相同算法的AI分不出胜负,可以引入合作-竞争机制、增强学习算法等强化Agent算法,或是两个人类围堵一个AI


🔥 更多精彩专栏

目录
相关文章
|
15小时前
|
机器学习/深度学习 算法 算法框架/工具
Python深度学习基于Tensorflow(5)机器学习基础
Python深度学习基于Tensorflow(5)机器学习基础
11 2
|
1天前
|
数据采集 数据可视化 数据挖掘
Python 与 PySpark数据分析实战指南:解锁数据洞见
Python 与 PySpark数据分析实战指南:解锁数据洞见
|
1天前
|
机器学习/深度学习 人工智能 大数据
AI时代Python金融大数据分析实战:ChatGPT让金融大数据分析插上翅膀
AI时代Python金融大数据分析实战:ChatGPT让金融大数据分析插上翅膀
|
2天前
|
机器学习/深度学习 算法 Python
深入浅出Python机器学习:从零开始的SVM教程/厾罗
深入浅出Python机器学习:从零开始的SVM教程/厾罗
|
7天前
|
机器学习/深度学习 自然语言处理 算法
机器学习算法原理与应用:深入探索与实战
【5月更文挑战第2天】本文深入探讨机器学习算法原理,包括监督学习(如线性回归、SVM、神经网络)、非监督学习(聚类、PCA)和强化学习。通过案例展示了机器学习在图像识别(CNN)、自然语言处理(RNN/LSTM)和推荐系统(协同过滤)的应用。随着技术发展,机器学习正广泛影响各领域,但也带来隐私和算法偏见问题,需关注解决。
|
9天前
|
机器学习/深度学习 边缘计算 TensorFlow
【Python机器学习专栏】Python机器学习工具与库的未来展望
【4月更文挑战第30天】本文探讨了Python在机器学习中的关键角色,重点介绍了Scikit-learn、TensorFlow和PyTorch等流行库。随着技术进步,未来Python机器学习工具将聚焦自动化、智能化、可解释性和可信赖性,并促进跨领域创新,结合云端与边缘计算,为各领域应用带来更高效、可靠的解决方案。
|
9天前
|
机器学习/深度学习 传感器 物联网
【Python机器学习专栏】机器学习在物联网(IoT)中的集成
【4月更文挑战第30天】本文探讨了机器学习在物联网(IoT)中的应用,包括数据收集预处理、实时分析决策和模型训练更新。机器学习被用于智能家居、工业自动化和健康监测等领域,例如预测居民行为以优化能源效率和设备维护。Python是支持物联网项目机器学习集成的重要工具,文中给出了一个使用`scikit-learn`预测温度的简单示例。尽管面临数据隐私、安全性和模型解释性等挑战,但物联网与机器学习的结合将持续推动各行业的创新和智能化。
|
9天前
|
机器学习/深度学习 数据采集 算法
【Python 机器学习专栏】机器学习在医疗诊断中的前沿应用
【4月更文挑战第30天】本文探讨了机器学习在医疗诊断中的应用,强调其在处理复杂疾病和大量数据时的重要性。神经网络、决策树和支持向量机等方法用于医学影像诊断、疾病预测和基因数据分析。Python作为常用工具,简化了模型构建和数据分析。然而,数据质量、模型解释性和伦理法律问题构成挑战,需通过数据验证、可解释性研究及建立规范来应对。未来,机器学习将更深入地影响医疗诊断,带来智能和精准的诊断工具,同时也需跨学科合作推动其健康发展。
|
9天前
|
机器学习/深度学习 分布式计算 物联网
【Python机器学习专栏】联邦学习:保护隐私的机器学习新趋势
【4月更文挑战第30天】联邦学习是保障数据隐私的分布式机器学习方法,允许设备在本地训练数据并仅共享模型,保护用户隐私。其优势包括数据隐私、分布式计算和模型泛化。应用于医疗、金融和物联网等领域,未来将发展更高效的数据隐私保护、提升可解释性和可靠性的,并与其他技术融合,为机器学习带来新机遇。
|
2月前
|
机器学习/深度学习 存储 搜索推荐
利用机器学习算法改善电商推荐系统的效率
电商行业日益竞争激烈,提升用户体验成为关键。本文将探讨如何利用机器学习算法优化电商推荐系统,通过分析用户行为数据和商品信息,实现个性化推荐,从而提高推荐效率和准确性。