许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  三消游戏怎么限制连着颜色不超过3个?消除逻辑设计

三消游戏怎么限制连着颜色不超过3个?消除逻辑设计

阅读数 18
点赞 0
article_banner


1:引言

     生成一个10x10的网格范围,但是不能让网格内的颜色起始的时候连起来有3个相同的颜色,也就是上下左右不能3个一样的颜色

2:代码

代码块

    //修正格子颜色,上下左右不能有3个相同的颜色
    onFixSlotColor() {
        for (let i = 0; i < this.slotParent.children.length; i++) {
            var slotSc = this.slotParent.children[i].getComponent(GuowangxiaochuSlot);
            //先随机一种
            slotSc.onRandomColor();
            var currColor = slotSc.myColor;

            //while (this.onSomeColor(i, currColor)) {
            //    currColor = this.onChooseColor();
            //}
            for (let j = 0; j < 1000; j++) {
                if (this.onSomeColor(i, currColor)) {
                    currColor = this.onChooseColor();
                }
                else {
                    break;
                }
            }

            slotSc.onSetColor(currColor);
        }
    }
    //颜色相同吗
    onSomeColor(num: number, str: string): boolean {
        var row = num / 10;
        var index = num % 10;
        var currNum = 1;
        var childrens = this.slotParent.children;
        //检测上
        if (row >= 2) {

            if (childrens[num - 10].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (childrens[num - 20].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (currNum >= 3) {
                return true;
            }
        }
        currNum = 1;
        //检测下
        if (row <= 7) {
            if (childrens[num + 10].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (childrens[num + 20].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (currNum >= 3) {
                return true;
            }
        }
        currNum = 1;
        //检测左
        if (index >= 2) {
            if (childrens[num - 1].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (childrens[num - 2].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (currNum >= 3) {
                return true;
            }
        }
        currNum = 1;
        //检测右
        if (index <= 7) {
            if (childrens[num + 1].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (childrens[num + 2].getComponent(GuowangxiaochuSlot).myColor == str) {
                currNum += 1;
            }
            if (currNum >= 3) {
                return true;
            }
        }

        return false;
    }
    //选择颜色
    onChooseColor(): string {
        const index = Math.floor(Math.random() * 3);
        return this.colors[index];
    }
    
      复制成功
     
     
     
     

这里是用一个一位数组存储的,还用for循环代替了while循环,主要是避免死循环,1000次还是随机到同一种颜色,只能说太倒霉了

3:总结

        进行上下左右遍历,若是有3个颜色一致,则重新选一种颜色



免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空