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


🔥 更多精彩专栏

目录
相关文章
|
13天前
|
机器学习/深度学习 算法 TensorFlow
交通标志识别系统Python+卷积神经网络算法+深度学习人工智能+TensorFlow模型训练+计算机课设项目+Django网页界面
交通标志识别系统。本系统使用Python作为主要编程语言,在交通标志图像识别功能实现中,基于TensorFlow搭建卷积神经网络算法模型,通过对收集到的58种常见的交通标志图像作为数据集,进行迭代训练最后得到一个识别精度较高的模型文件,然后保存为本地的h5格式文件。再使用Django开发Web网页端操作界面,实现用户上传一张交通标志图片,识别其名称。
43 6
交通标志识别系统Python+卷积神经网络算法+深度学习人工智能+TensorFlow模型训练+计算机课设项目+Django网页界面
|
13天前
|
机器学习/深度学习 算法 数据挖掘
Python数据分析革命:Scikit-learn库,让机器学习模型训练与评估变得简单高效!
在数据驱动时代,Python 以强大的生态系统成为数据科学的首选语言,而 Scikit-learn 则因简洁的 API 和广泛的支持脱颖而出。本文将指导你使用 Scikit-learn 进行机器学习模型的训练与评估。首先通过 `pip install scikit-learn` 安装库,然后利用内置数据集进行数据准备,选择合适的模型(如逻辑回归),并通过交叉验证评估其性能。最终,使用模型对新数据进行预测,简化整个流程。无论你是新手还是专家,Scikit-learn 都能助你一臂之力。
64 8
|
9天前
|
机器学习/深度学习 数据可视化 数据挖掘
数据可视化大不同!Python数据分析与机器学习中的Matplotlib、Seaborn应用新视角!
在数据科学与机器学习领域,数据可视化是理解数据和优化模型的关键。Python凭借其强大的可视化库Matplotlib和Seaborn成为首选语言。本文通过分析一份包含房屋面积、卧室数量等特征及售价的数据集,展示了如何使用Matplotlib绘制散点图,揭示房屋面积与售价的正相关关系;并利用Seaborn的pairplot探索多变量间的关系。在机器学习建模阶段,通过随机森林模型展示特征重要性的可视化,帮助优化模型。这两个库在数据分析与建模中展现出广泛的应用价值。
25 2
|
14天前
|
机器学习/深度学习 存储 人工智能
文本情感识别分析系统Python+SVM分类算法+机器学习人工智能+计算机毕业设计
使用Python作为开发语言,基于文本数据集(一个积极的xls文本格式和一个消极的xls文本格式文件),使用Word2vec对文本进行处理。通过支持向量机SVM算法训练情绪分类模型。实现对文本消极情感和文本积极情感的识别。并基于Django框架开发网页平台实现对用户的可视化操作和数据存储。
20 0
文本情感识别分析系统Python+SVM分类算法+机器学习人工智能+计算机毕业设计
|
21天前
|
机器学习/深度学习 算法 数据挖掘
从菜鸟到大师:Scikit-learn库实战教程,模型训练、评估、选择一网打尽!
【9月更文挑战第13天】在数据科学与机器学习领域,Scikit-learn是不可或缺的工具。本文通过问答形式,指导初学者从零开始使用Scikit-learn进行模型训练、评估与选择。首先介绍了如何安装库、预处理数据并训练模型;接着展示了如何利用多种评估指标确保模型性能;最后通过GridSearchCV演示了系统化的参数调优方法。通过这些实战技巧,帮助读者逐步成长为熟练的数据科学家。
63 3
|
25天前
|
机器学习/深度学习 算法 Python
从菜鸟到大师:一棵决策树如何引领你的Python机器学习之旅
【9月更文挑战第9天】在数据科学领域,机器学习如同璀璨明珠,吸引无数探索者。尤其对于新手而言,纷繁复杂的算法常让人感到迷茫。本文将以决策树为切入点,带您从Python机器学习的新手逐步成长为高手。决策树以其直观易懂的特点成为入门利器。通过构建决策树分类器并应用到鸢尾花数据集上,我们展示了其基本用法及效果。掌握决策树后,还需深入理解其工作原理,调整参数,并探索集成学习方法,最终将所学应用于实际问题解决中,不断提升技能。愿这棵智慧之树助您成为独当一面的大师。
23 3
|
15天前
|
机器学习/深度学习 数据采集 算法
机器学习新纪元:用Scikit-learn驾驭Python,精准模型选择全攻略!
在数据爆炸时代,机器学习成为挖掘数据价值的关键技术,而Scikit-learn作为Python中最受欢迎的机器学习库之一,凭借其丰富的算法集、简洁的API和高效性能,引领着机器学习的新纪元。本文通过一个实际案例——识别垃圾邮件,展示了如何使用Scikit-learn进行精准模型选择。从数据预处理、模型训练到交叉验证和性能比较,最后选择最优模型进行部署,详细介绍了每一步的操作方法。通过这个过程,我们不仅可以看到如何利用Scikit-learn的强大功能,还能了解到模型选择与优化的重要性。希望本文能为你的机器学习之旅提供有价值的参考。
17 0
|
23天前
|
机器学习/深度学习 人工智能 TensorFlow
神经网络入门到精通:Python带你搭建AI思维,解锁机器学习的无限可能
【9月更文挑战第10天】神经网络是开启人工智能大门的钥匙,不仅是一种技术,更是模仿人脑思考的奇迹。本文从基础概念入手,通过Python和TensorFlow搭建手写数字识别的神经网络,逐步解析数据加载、模型定义、训练及评估的全过程。随着学习深入,我们将探索深度神经网络、卷积神经网络等高级话题,并掌握优化模型性能的方法。通过不断实践,你将能构建自己的AI系统,解锁机器学习的无限潜能。
18 0
|
5月前
|
机器学习/深度学习 人工智能 自然语言处理
【Python机器学习】文本特征提取及文本向量化讲解和实战(图文解释 附源码)
【Python机器学习】文本特征提取及文本向量化讲解和实战(图文解释 附源码)
345 0
|
5月前
|
机器学习/深度学习 算法 数据挖掘
【Python机器学习】K-Means对文本聚类和半环形数据聚类实战(附源码和数据集)
【Python机器学习】K-Means对文本聚类和半环形数据聚类实战(附源码和数据集)
153 0
下一篇
无影云桌面