四子棋对决(二)

2023-05-16

import com from './common'
cc.Class({
    extends: cc.Component,
    properties: {
        overLab: {
            default: null,
            type:cc.Label
        },
        chessPrefab: {//棋子的预制资源
            default: null,
            type: cc.Prefab
        },
        chessList:{
            default:[],
            type:[cc.Node]
        },
        whiteSpriteFrame: {
            default: null,
            type: cc.SpriteFrame
        },
        blackSpriteFrame: {
            default: null,
            type: cc.SpriteFrame
        },
        touchChess: {
            default: null,
            type: cc.SpriteFrame
        },
        gameState: 'black',
        fourGroup: [],//四元组
        fourGroupScore: [],//四元组
        chessBoard: { //棋盘
            default: null,
            type: cc.Node
        }
    },
    onLoad: function() {
        let self = this;
        
        //初始化棋盘上225个棋子节点,并为每个节点添加事件
        for (var y = 0; y < 15;y++) {
            for (var x = 0; x < 15; x++) {              
                var newNode = cc.instantiate(this.chessPrefab);//复制chess预制资源               
                this.chessBoard.addChild(newNode);
                newNode.setPosition(cc.v2(x * 40 + 20, y * 40 + 20));
                newNode.attr({ tag: y * 15 + x });
                newNode.on(cc.Node.EventType.TOUCH_END, function (event) {  //人下黑棋
                    self.touchChess = this;                  
                    if (this.getComponent(cc.Sprite).spriteFrame === null) {//判断可以下棋
                        if (self.gameState === 'black') {//下黑棋
                            if (com.gameMode === 'random') { //与网友对决

                            } else {                         //与人工智障对决
                                    this.getComponent(cc.Sprite).spriteFrame = self.blackSpriteFrame;
                                    //----------------------------------判断玩家胜利-------------------------------
                                    if (self.jugeOver()) {                              //玩家胜利
                                        self.overLab.string = "胜利";
                                        self.overLab.node.active = true;
                                        self.gameState = 'close';                       //关闭下棋
                                    } else {                                            //玩家当前没有胜利
                                        self.scheduleOnce(function () { self.af() }, 1);//延迟一秒电脑下棋
                                    }
                            }
                        } 
                    }
                }.bind(newNode));
                this.chessList.push(newNode);
            }
        }
        //建立四元组为人工智障埋下伏笔
        for (var y = 0; y < 14;y++) {
            for (var x = 0; x < 14; x++) {
                this.fourGroup.push([y*15+x,y*15+x+1,(y+1)*15+x,(y+1)*15+x+1]);//存储顺序
            }
        }
    },
    af: function () {
        //评分
        for (var i = 0; i < this.fourGroup.length; i++) {
            var b = 0;//四元组里黑棋个数
            var w = 0;//四元组里白棋个数
            //统计黑棋与白棋的个数
            for (var j = 0; j < 4; j++) {
                if (this.chessList[this.fourGroup[i][j]].getComponent(cc.Sprite).spriteFrame === this.blackSpriteFrame) {//黑棋
                    b++;
                } else if (this.chessList[this.fourGroup[i][j]].getComponent(cc.Sprite).spriteFrame === this.whiteSpriteFrame) {//白色
                    w++;
                }
            }
            
            if (b + w === 0) {             //b=w=0
                this.fourGroupScore[i] = 1;
            } else if (b > 0 && w > 0) {   //b<0,w>0
                this.fourGroupScore[i] = 0;
            } else if (b === 0 && w == 1) {//电脑发展壮大自己
                this.fourGroupScore[i] = 2
            } else if (b === 0 && w === 2) {
                this.fourGroupScore[i] = 3;
            } else if (b === 0 && w === 3) {
                this.fourGroupScore[i] = 10;
            } else if (w === 0 && b === 1) {//电脑破坏敌方阵地
                this.fourGroupScore[i] = 2;
            } else if (w === 0 && b === 2) {
                this.fourGroupScore[i] = 3;
            } else if (w === 0 && b === 3) {
                this.fourGroupScore[i] = 9;
            }
        }
        //找到最高分的四元组
        var hScore = 0;
        var mPosition = 0;
        for (var i = 0; i < this.fourGroup.length; i++) {
            if (this.fourGroupScore[i] > hScore) {
                hScore = this.fourGroupScore[i];
                mPosition = i;//记录最高的元组
            }
        }

        //在最高分的四元组找到最优下子位置
        var nPosition = 0;
        for (var i = 0; i < 4; i++) {
            if (this.chessList[this.fourGroup[mPosition][i]].getComponent(cc.Sprite).spriteFrame === null) {
                nPosition = i;
                break;
            }
        }
        //在较优的位置下棋
        this.chessList[this.fourGroup[mPosition][nPosition]].getComponent(cc.Sprite).spriteFrame = this.whiteSpriteFrame;
        this.touchChess = this.chessList[this.fourGroup[mPosition][nPosition]];
        if (this.jugeOver()) {                      //电脑胜利
            this.overLab.string = "失败";
            this.overLab.node.active = true;
            this.gameState = 'close'                 //
        } else {                                     //电脑当前没有胜利
            this.gameState = 'black'                 //设置轮到玩家下棋
        }
    },
    jugeOver: function () {
        //---------获取刚放下去的棋子tag(序号)
        var index = this.touchChess.tag;
        var x = index % 15;
        var y = parseInt(index / 15);      
        var sameCount = 0;
        //结构一
        //[0]0   (x,y)  (index)     (x+1,y)(index+1)
        // 0 0   (x,y-1)(index-15)  (x+1,y-1)(index+16)
        if (x<14 &&y > 0) {
            //判断其余棋是否同一棋型
            if (this.jugeSame(x + 1, y)) {//右侧棋
                sameCount++;
            }
            if (this.jugeSame(x, y - 1)) {//下面棋
                sameCount++;
            }
            if (this.jugeSame(x+1,y-1)) {  //右下棋
                sameCount++;
            }
            //判断下棋结束
            if (sameCount===3) {//相同成功
                return true;
            }
        }

        //结构二
        //0[0]
        //0 0
        sameCount = 0;//同类型棋初始化为零
        if (x > 0 && y > 0) {                 
            if (this.jugeSame(x - 1, y)) { //左侧棋  
                sameCount++;
            }
            if (this.jugeSame(x - 1, y - 1)) {//左下棋
                sameCount++;
            }
            if (this.jugeSame(x, y - 1)) {  //下面棋
                sameCount++;
            }
            //判断下棋结束
            if (sameCount===3) {
                return true;
            }
        }

        //结构三
        // 0 0
        //[0]0
        sameCount = 0;//初始化同类型个数为零
        if (x < 15 && y < 14) {
            if (this.jugeSame(x , y+1)) {     //上面棋
                sameCount++;
            }
            if (this.jugeSame(x + 1, y + 1)) {//右上棋
                sameCount++;
            }
            if (this.jugeSame(x+1, y)) {      //右侧棋
                sameCount++;
            }           
            if (sameCount===3) {
                return true;
            }
        }
        //结构四
        // 0 0
        // 0[0]
        sameCount = 0;//初始化同类型为零
        if (x > 0 && y < 14) {
            if (this.jugeSame(x-1, y + 1)) {   //左上棋
                sameCount++;
            }
            if (this.jugeSame(x, y + 1)) {     //上面棋
                sameCount++;
            }
            if (this.jugeSame(x - 1, y)) {     //左侧棋
                sameCount++;
            }     
            if (sameCount===3) {
                return true;
            }
        }
        return false;
    },
    //判断是否属于同一类型棋子
    jugeSame: function (x, y) {
        if (this.chessList[y * 15 + x].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {
            return true
        }  
        return false;
    },
});

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

四子棋对决(二) 的相关文章

随机推荐

  • 领航-跟随型编队 (六)避障问题综述

    领航 跟随型编队避障问题指编队在运动过程中 xff0c 领航机器人根据某种方式获取与识别前方障碍物 xff0c 同时编队整体采取一定方法及时规避障碍物与防止内部碰撞 xff0c 涉及到障碍物检测 编队避障规划 编队避碰协调 xff0c 运动
  • 领航跟随型编队(十)编队实验视频

    实验一 xff1a 圆形轨迹下编队生成与保持实验 如图 5 19 所示 xff0c 两个机器人完成从随机状态形成编队并沿圆形轨迹保持编队运行 xff0c 且图中下方的窗口动态显示编队的运行情况 领航机器人初始信息 xff1a 坐标 0 5m
  • 内网项目中引入NoVnc服务

    内网项目中引入NoVnc服务 背景目标方案部署步骤完成后验证效果 背景 目前项目中 xff0c 管理的实例底层为虚拟机 xff0c 而在用户或运维人员管理具体的实例时 xff0c 需另外启动VNC Viewer客户端才能配置实例 xff0c
  • Jupyter最全指南及常见问题(持续更新)

    anaconda与jupyter lab搭配使用 jupyter lab是jupyter notebook升级版 非常好用 命令行安装 xff1a pip install jupyterlab即可 安装好了之后 xff0c 命令行输入 ju
  • 头文件

    include lt iostream gt include lt algorithm gt include lt cassert gt include lt string gt include lt sstream gt include
  • MySQL 8.0 密码重置

    MySQL 版本 8 0 系统 xff1a Linux 原因 xff1a 数据库无法登录 xff08 非忘记密码 xff09 xff0c 登上后发现竟然数据库被黑了 xff0c 留了一条 BTC 的 赎回记录 首先关闭现有的mysql 服务
  • MySQL 取出每个分组中最新的一条数据(ID最大)

    场景 xff1a 由于一个摄像头管理一个范围 xff0c 且管理的某个人可以多次犯规 故 xff0c 一个摄像头可以上报有多个事件 xff0c 多个事件可能同时上报 xff0c 可能有先后顺序 需求 xff1a 现地图只显示有事件摄像头的最
  • java获取天气接口

    如下图 span class token keyword package span span class token namespace com span class token punctuation span octv span cla
  • Eclipse反编译插件(免费无需下载资源)

    分享一个适用eclipse的java反编译插件JD Decompiler 最近eclipse插件库被玩坏了 xff0c 于是重新安装插件 xff0c 站内搜索发现反编译插件竟然都要积分下载了 以下是插件官网 xff0c 看不懂英文的小伙伴用
  • JAVA基础疑难——001Boolean类型传值问题

    今天在帮助一位小伙伴解决传值的问题的时候 xff0c 发现他使用的是boolean类型的带参方法 程序执行没有问题 xff0c 但是boolen类型的值传不出来 怎么找问题都找不出来 今天就该问题所产生的原因给大家分享一下 xff0c 下面
  • 洛谷p4180 ac自动机

    匹配字符串时 xff0c 对于重复的单词我们只考虑一次 xff0c 我们开一个数组记录 xff0c 重复单词的第一个id将重复单词的出现次数全部变为第一次出现的个数相加 且在匹配时 xff0c 对于每个now只扫描一次 xff0c 不重复扫
  • Debian开启SSH

    一 Debian开启SSH 参考链接 xff1a https blog csdn net zzpzheng article details 71170572 https help aliyun com knowledge detail 41
  • IDEA配置Tomcat

    IntelliJ IDEA 2017 配置Tomcat 运行Web项目 以前都用MyEclipse写程序的 突然用了IDEA各种不习惯的说 借鉴了很多网上好的配置办法 xff0c 感谢各位大神 前期准备 IDEA JDK Tomcat请先在
  • 如何实现页面登录验证

    现在很多网站在登录的时候都需要输入验证码 xff0c 现在输入的验证码方式层出不穷有单单是数字的 字母 xff08 又分大小写 xff09 的 xff0c 有数字 字母混合的 xff0c 有给出运算表达式需要回答结果的 xff0c 还有的卡
  • REST,RESTful到底是个什么?

    0 REST不是 34 rest 34 这个单词 而是几个单词缩写 但即使那几个单词说出来 也无法理解在说什么 不是要贬低人 是我自己也理解困难 1 REST描述的是在网络中client和server的一种交互形式 REST本身不实用 实用
  • spring boot 入门

    什么是 spring boot Spring Boot是由Pivotal团队提供的全新框架 xff0c 其设计目的是用来简化新Spring应用的初始搭建以及开发过程 该框架使用了特定的方式来进行配置 xff0c 从而使开发人员不再需要定义样
  • html如何使用springboot进行跳转

    问题 xff1a 页面之间的跳转 xff0c 通常带有值的传输 xff0c 但是 xff0c 在现在比较流行的SPRING MVC WEB 开发模型中 xff0c 设计机制导致页面之间的直接接跳转和传值不被支持 xff08 网上看到的 xf
  • PowerShell升级

    PowerShell升级 1 查看版本 span class token variable PSVersionTable span 2 搜索软件包 winget search Microsoft PowerShell 3 使用 id 参数安
  • 四子棋对决(一)

    1 算法一 cc Class extends cc Component properties overLab default null type cc Label chessPrefab 棋子的预制资源 default null type
  • 四子棋对决(二)

    import com from 39 common 39 cc Class extends cc Component properties overLab default null type cc Label chessPrefab 棋子的