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


🔥 更多精彩专栏

目录
相关文章
|
8月前
|
SQL 关系型数据库 数据库
Python SQLAlchemy模块:从入门到实战的数据库操作指南
免费提供Python+PyCharm编程环境,结合SQLAlchemy ORM框架详解数据库开发。涵盖连接配置、模型定义、CRUD操作、事务控制及Alembic迁移工具,以电商订单系统为例,深入讲解高并发场景下的性能优化与最佳实践,助你高效构建数据驱动应用。
978 7
|
8月前
|
数据采集 Web App开发 数据安全/隐私保护
实战:Python爬虫如何模拟登录与维持会话状态
实战:Python爬虫如何模拟登录与维持会话状态
|
8月前
|
传感器 运维 前端开发
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
本文解析异常(anomaly)与新颖性(novelty)检测的本质差异,结合distfit库演示基于概率密度拟合的单变量无监督异常检测方法,涵盖全局、上下文与集体离群值识别,助力构建高可解释性模型。
605 10
Python离群值检测实战:使用distfit库实现基于分布拟合的异常检测
|
8月前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
8月前
|
Cloud Native 算法 API
Python API接口实战指南:从入门到精通
🌟蒋星熠Jaxonic,技术宇宙的星际旅人。深耕API开发,以Python为舟,探索RESTful、GraphQL等接口奥秘。擅长requests、aiohttp实战,专注性能优化与架构设计,用代码连接万物,谱写极客诗篇。
1748 1
Python API接口实战指南:从入门到精通
|
8月前
|
存储 分布式计算 测试技术
Python学习之旅:从基础到实战第三章
总体来说,第三章是Python学习路程中的一个重要里程碑,它不仅加深了对基础概念的理解,还引入了更多高级特性,为后续的深入学习和实际应用打下坚实的基础。通过这一章的学习,读者应该能够更好地理解Python编程的核心概念,并准备好应对更复杂的编程挑战。
244 12
|
9月前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
1080 19
|
8月前
|
存储 数据采集 监控
Python文件操作全攻略:从基础到高级实战
本文系统讲解Python文件操作核心技巧,涵盖基础读写、指针控制、异常处理及大文件分块处理等实战场景。结合日志分析、CSV清洗等案例,助你高效掌握文本与二进制文件处理,提升程序健壮性与开发效率。(238字)
672 1
|
8月前
|
存储 Java 调度
Python定时任务实战:APScheduler从入门到精通
APScheduler是Python强大的定时任务框架,通过触发器、执行器、任务存储和调度器四大组件,灵活实现各类周期性任务。支持内存、数据库、Redis等持久化存储,适用于Web集成、数据抓取、邮件发送等场景,解决传统sleep循环的诸多缺陷,助力构建稳定可靠的自动化系统。(238字)
1487 1
|
9月前
|
设计模式 人工智能 API
AI智能体开发实战:17种核心架构模式详解与Python代码实现
本文系统解析17种智能体架构设计模式,涵盖多智能体协作、思维树、反思优化与工具调用等核心范式,结合LangChain与LangGraph实现代码工作流,并通过真实案例验证效果,助力构建高效AI系统。
1005 7

推荐镜像

更多