四子棋对决(二)

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;
    },
});

 

你可能感兴趣的:(四子棋对决(二))