四子棋对决(一)

1.算法一

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: [],//四元组
        fourGroup: [],//四元组
        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) {//判断无棋
                        console.log('gameState=' + self.gameState);
                        if (self.gameState === 'black') {//下黑棋
                            this.getComponent(cc.Sprite).spriteFrame = self.blackSpriteFrame;
                            self.gameState = 'white';
                        } else if (self.gameState === 'white'){//下白棋
                            this.getComponent(cc.Sprite).spriteFrame = self.whiteSpriteFrame;
                            self.gameState = 'black';             
                        };
                        if (self.jugeOver()) {
                            self.overLab.string = "胜利";
                            self.overLab.node.active = true;
                            self.gameState = '----';
                        }
                    }
                }.bind(newNode));              
                this.chessList.push(newNode);
            }
        }
    },
    jugeOver: function () {
        //---------获取刚放下去的棋子tag(序号)
        console.log("hello");
        var index = this.touchChess.tag;
        console.log('index=' + index);
        var x = index % 15;
        var y = parseInt(index / 15);      
        console.log('x=' + x);
        console.log('y=' + y);
        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 (y > 0 && x < 14) {//四元组位置
            //判断其余棋是否同一棋型
            if (this.chessList[y * 15 + x + 1].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//右侧棋
                sameCount++;
            }
            if (this.chessList[(y - 1) * 15 + x].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//下面棋
                sameCount++;
            }
            if (this.chessList[(y - 1) * 15 + x + 1].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//右下方棋
                sameCount++;
            }
            //判断下棋结束
            if (sameCount===3) {//相同成功
                return true;
            }
        }

        //结构二
        //0[0]
        //0 0
        sameCount = 0;//同类型棋初始化为零
        if (x > 0 && y > 0) {
            if (this.chessList[y * 15 + x - 1].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//左侧棋
                sameCount++;
            }
            if (this.chessList[(y-1) * 15 + x-1].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//左下角
                sameCount++;
            }
            if (this.chessList[(y-1) * 15 + x].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {  //下面棋
                sameCount++;
            }
            //判断下棋结束
            if (sameCount===3) {
                return true;
            }
        }

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

2.算法二

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: [],//四元组
        fourGroup: [],//四元组
        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) {//判断无棋
                        console.log('gameState=' + self.gameState);
                        if (self.gameState === 'black') {//下黑棋
                            this.getComponent(cc.Sprite).spriteFrame = self.blackSpriteFrame;
                            self.gameState = 'white';
                        } else if (self.gameState === 'white'){//下白棋
                            this.getComponent(cc.Sprite).spriteFrame = self.whiteSpriteFrame;
                            self.gameState = 'black';             
                        };
                        if (self.jugeOver()) {
                            self.overLab.string = "胜利";
                            self.overLab.node.active = true;
                            self.gameState = '----';
                        }
                    }
                }.bind(newNode));              
                this.chessList.push(newNode);
            }
        }
    },
    jugeOver: function () {
        //---------获取刚放下去的棋子tag(序号)
        console.log("hello");
        var index = this.touchChess.tag;
        console.log('index=' + index);
        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) {
        console.log('x=' + x);
        console.log('y=' + y);
        if (this.chessList[y * 15 + x].getComponent(cc.Sprite).spriteFrame === this.touchChess.getComponent(cc.Sprite).spriteFrame) {//左侧棋
            return true
        }  
        return false;
    }
});

3.添加人工智障

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') {//下黑棋
                            this.getComponent(cc.Sprite).spriteFrame = self.blackSpriteFrame;
                             //阻止玩家连续下棋,开启电脑下棋
                            //self.gameState = 'white'
                            //----------------------------------判断玩家胜利-------------------------------
                            if (self.jugeOver()) {                              //玩家胜利
                                self.overLab.string = "胜利";  
                                self.overLab.node.active = true;
                                self.gameState = 'over';
                            } else {                                            //玩家当前没有胜利
                                self.scheduleOnce(function () { self.af() }, 1);//延迟一秒电脑下棋   
                            //----------------------------------判断电脑是否胜利---------------------------
                              /* 
                               if (self.jugeOver()) {                       //电脑胜利
                                    self.overLab.string = "失败";
                                    self.overLab.node.active = true;
                                    self.gameState = 'over'
                                } else {                                     //电脑当前没有胜利
                                    self.gameState = 'black'              //设置轮到玩家下棋
                                }
                                */
                            }
                        }
                        
                    }
                }.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 = 'over'
        } 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;
    },
});

 

你可能感兴趣的:(微信小游戏)