手撸消消乐算法

2 篇文章 0 订阅
2 篇文章 0 订阅

直接传入地图位置和落下的位置,适用双色消除和单色消除,将算法和整体逻辑剥离,也可以优化其它算法

  /**
     * 双色消除算法
     * @param arr  地图数据
     * @param tagrnum 所落下的位置
     * @returns 
     */
    public shuangse(arr: Array<Array<number>>, tagrnum: string): boolean {
     let county: number = 0;
        let countx: number = 0;
        let x: number = Number(tagrnum[0]);
        let y: number = Number(tagrnum[1]);
        let curritem = arr[x][y]; //当前物体

        let rigthy: number = y; //右边
        let lefty: number = y;//做边
        let upx: number = x;//上边
        let downx: number = x;//下边
        let arry: Array<string> = new Array<string>();//收集横向需要消除的方块
        let arrx: Array<string> = new Array<string>();//收集纵向需要消除的方块
        let curxy: number = arr[x][rigthy];//所在位置的临时值
        while (rigthy < 7) //右边
        {
            rigthy = rigthy + 1;
            if (x >= 0 && rigthy >= 0 && x < 7 && rigthy < 7)//一直找到最后
            {

                curxy = arr[x][rigthy];
                if (curxy > 0) {
                    if (curritem >= 10) //放的是双色
                    {
                        let str: string = curritem.toString();
                        let str1: string = str[0];
                        if (arr[x][rigthy] >= 10) //旁边是否双色
                        {
                            let arrstr: string = (arr[x][rigthy]).toString();
                            let arrstr1: string = arrstr[0];
                            if (str1 == arrstr1) {
                                arry.push(x.toString() + rigthy.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        } else {
                            if (str1 == (arr[x][rigthy]).toString())  //符合要求,不是双色
                            {
                                arry.push(x.toString() + rigthy.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                    else { //放的不是双色
                        if (arr[x][rigthy] >= 10) //旁边是双色
                        {
                            let arrstr: string = (arr[x][rigthy]).toString();
                            let arrstr1: string = arrstr[0];
                            if (curritem == Number(arrstr1)) {
                                arry.push(x.toString() + rigthy.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                        else { //不是双色
                            if (curritem == arr[x][rigthy]) //下一个目标是双色
                            {
                                arry.push(x.toString() + rigthy.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                }
                else {
                    break;
                }

            }
            else {
                break;
            }
        }
        while (lefty >= 0) //左边
        {
            lefty = lefty - 1;
            if (lefty >= 0 && x >= 0 && lefty < 7 && x < 7)//一直找到最后
            {
                curxy = arr[x][lefty];
                if (curxy > 0) {

                    if (curritem >= 10) //放的是双色
                    {
                        let str: string = curritem.toString();
                        let str1: string = str[0];
                        if (arr[x][lefty] >= 10) //旁边是否双色
                        {
                            let arrstr: string = (arr[x][lefty]).toString();
                            let arrstr1: string = arrstr[0];
                            if (str1 == arrstr1) {
                                arry.push(x.toString() + lefty.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                        else {
                            if (str1 == (arr[x][lefty]).toString())  //符合要求,不是双色
                            {
                                arry.push(x.toString() + lefty.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                    else { //放的不是双色
                        if (arr[x][lefty] >= 10) //旁边是双色
                        {
                            let arrstr: string = (arr[x][lefty]).toString();
                            let arrstr1: string = arrstr[0];
                            if (curritem == Number(arrstr1)) {
                                arry.push(x.toString() + lefty.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                        else { //不是双色
                            if (curritem == arr[x][lefty]) //下一个目标是双色
                            {
                                arry.push(x.toString() + lefty.toString());
                                county++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                }
                else {
                    break;
                }

            }
            else {
                break;
            }
        }
        while (upx >= 0) //向上
        {
            upx = upx - 1;
            if (y >= 0 && upx >= 0 && upx < 7 && y < 7)//一直找到最后
            {
                curxy = arr[upx][y] ;

                if (curxy>0) {

                    if (curritem >= 10) //放的是双色
                    {
                        let str: string = curritem.toString();
                        let str1: string = str[0];
                        if (arr[upx][y] >= 10) //旁边是否双色
                        {
                            let arrstr: string = (arr[upx][y]).toString();
                            let arrstr1: string = arrstr[0];
                            if (str1 == arrstr1) {
                                arrx.push(upx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        } else {
                            if (str1 == (arr[upx][y]).toString())  //符合要求,不是双色
                            {
                                arrx.push(upx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                    else { //放的不是双色
                        if (arr[upx][y] >= 10) //旁边是双色
                        {
                            let arrstr: string = (arr[upx][y]).toString();
                            let arrstr1: string = arrstr[0];
                            if (curritem == Number(arrstr1)) {
                                arrx.push(upx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        }
                        else { //不是双色
                            if (curritem == arr[upx][y]) //下一个目标是双色
                            {
                                arrx.push(upx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }

                        }
                    }

                }
                else {
                    break;
                }
            }
        }
        while (downx < 7) //向下
        {
            downx = downx + 1;
            if (y >= 0 && downx >= 0 && downx < 7 && y < 7)//一直找到最后
            {
                curxy =arr[downx][y] ;
                if (curxy>0) {

                    if (curritem >= 10) //放的是双色
                    {
                        let str: string = curritem.toString();
                        let str1: string = str[0];
                        if (arr[downx][y] >= 10) //旁边是否双色
                        {
                            let arrstr: string = (arr[downx][y]).toString();
                            let arrstr1: string = arrstr[0];
                            if (str1 == arrstr1) {
                                arrx.push(downx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        } else {
                            if (str1 == (arr[downx][y]).toString())  //符合要求,不是双色
                            {
                                arrx.push(downx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        }
                    }
                    else { //放的不是双色
                        if (arr[downx][y] >= 10) //旁边是双色
                        {
                            let arrstr: string = (arr[downx][y]).toString();
                            let arrstr1: string = arrstr[0];
                            if (curritem == Number(arrstr1)) {
                                arrx.push(downx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        }
                        else { //不是双色
                            if (curritem == arr[downx][y]) //下一个目标是双色
                            {
                                arrx.push(downx.toString() + y.toString());
                                countx++;
                            }
                            else {
                                break;
                            }
                        }
                    }

                }
                else {
                    break;
                }

            }
            else {
                break;
            }
        }
        let arrxy: Array<string> = new Array<string>();


        if (countx >= 2 && county >= 2) {
            arrx.push(tagrnum);
            arrxy = arrx.concat(arry);

        }
        else if (countx >= 2) {
            arrx.push(tagrnum);
            arrxy = arrx;
        }
        else if (county >= 2) {
            arry.push(tagrnum);
            arrxy = arry;
        }
        else {
            arrxy = null;
        }

        return arrxy;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值