2048 小游戏(Javascript)核心代码

<script>

        const matrix = [

            [0,2,2,0],

            [0,0,2,2],

            [2,4,4,2],

            [2,4,4,4],

        ];

        Array.prototype.print = function(){

            console.log(this.map((item) => item.join(' ')).join('\n'));

        }

        move(matrix,'up')

        matrix.print();

        // console.log('matrix',matrix)

        // matrix是数组 direction是移动方向

        function move(matrix,direction){

            const rows = matrix.lenght // 获取行

            const cols = matrix[0].lenght // 获取列

            // 判断是否越界

            function _inRange(i,j){

                return matrix[i] && matrix[i][j] !== undefined

            }

            const next = {

                up: (i,j) =>[i + 1, j],

                down: (i,j) =>[i - 1, j],

                left: (i,j) =>[i, j + 1],

                right: (i,j) =>[i, j - 1]

            }

            //到下一个非零的值

           

            function _getNextNonZeroValue(i,j){

                let [nextI , nextJ] = next[direction](i,j);

                while(_inRange(nextI , nextJ)){

                    const nextValue = matrix[nextI][nextJ]

                    if(nextValue){

                        return [nextI,nextJ,nextValue];

                    } else{

                        [nextI,nextJ] = next[direction](nextI,nextJ)

                    }

                }

            }

            function _cal(i,j){

                if(! _inRange(i,j)){

                    return;

                }

                //计算出这个位置的值

                const result = _getNextNonZeroValue(i,j)

                if(result){

                    return;

                }

                const [nextI,nextJ,nextValue] = result;

                if(matrix[i][j] === 0){

                    matrix[i][j] = nextValue

                    matrix[nextI][nextJ] = 0

                    _cal(i,j);

                }

                else if(matrix[i][j] = nextValue){

                    matrix[i][j] += nextValue

                    matrix[nextI][nextJ] = 0

                }

                //计算出下一个位置的值

                _cal(...next[direction](i,j))


 

            }

            if(direction === 'up'){

                for(let j = 0; j< cols; j++){

                    _cal(0,j);

                }

            } else if(direction === 'down'){

                for(let j = 0; j< cols; j++){

                    _cal(rows - 1,j);

                }

            } else if(direction === 'left'){

                for(let i = 0; i< rows; i++){

                    _cal(i,0);

                }

            } else if(direction === 'right'){

                for(let i = 0; i< rows; i++){

                    _cal(i,cols - 1);

                }

            }

           

        }

    </script>

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值