js实现2048

样式部分

* {
    margin: 0px;
    padding: 0px;
}

.case {
    width: 450px;
    height: 600px;
    margin: 20px auto;
    /*overflow: hidden;*/
    position: relative;
    /*border: 1px solid red;*/
}

.above_box {
    width: 450px;
    height: 150px;
    margin: 0px auto;
    /*border: 1px solid red;*/
}

.logo {
    width: 350px;
    height: 150px;
    float: left;
}

.logo_num {
    color: #766f67;
    font-size: 60px;
    font-family: '微软雅黑';
    font-weight: bold;
}

.logo_font {
    color: #9B988F;
    float: left;
    font-size: 16px;
}

.above_box button {
    width: 100px;
    height: 50px;
    color: white;
    font-size: 15px;
    background-color: #8b7c65;
    /*border-radius: 10px: */
    border: none;
    margin-top: 10px;
    float: right;
}

.countScore {
    width: 100px;
    height: 70px;
    background-color: #8b7c65;
    color: white;
    float: right;
    margin-top: 10px;
}

.countScore p {
    /*float: right;
	width: 100px;
	height: 40px;
	background-color: #8b7c65;
	color: white;*/
    /*text-align: center; */
    line-height: 30px;
    text-align: center;
}

.countScore span {
    display: block;
    line-height: 30px;
    text-align: center;
    font-weight: bold;
    font-size: 20px;
}

.main_box {
    width: 450px;
    height: 450px;
    margin: 0px auto;
    /*border: 1px solid red;*/
    background-color: #bbada0;
    border-radius: 10px;
    /* position: absolute; */
}

.main_box2 {
    width: 450px;
    height: 450px;
    margin: 0px auto;
    border-radius: 10px;
    background-color: rgba(0, 0, 0, 0.4);
    position: absolute;
    font-size: 50px;
    color: white;
    text-align: center;
    line-height: 450px;
    /*z-index: 1;*/
}

.main_box2 button {
    width: 101px;
    height: 50px;
    color: white;
    font-size: 20px;
    background-color: rgba(0, 0, 0, 0);
    border: none;
    position: absolute;
    top: 260px;
    left: 165px;
}

.game {
    z-index: 1;
}

.min_box {
    width: 100px;
    height: 100px;
    background-color: #ccc0b2;
    float: left;
    margin: 10px 0 0 10px;
    border-radius: 5px;
    font-size: 35px;
    line-height: 100px;
    text-align: center;
    color: #796f65;
}


/**/

.n2 {
    background-color: #aae3da
}

.n4 {
    background-color: #ede0c8
}

.n8 {
    background-color: #f2b179
}

.n16 {
    background-color: #f59563
}

.n32 {
    background-color: #f67c5f
}

.n64 {
    background-color: #f65e3b
}

.n128 {
    background-color: #edcf72
}

.n256 {
    background-color: #edcc61
}

.n512 {
    background-color: #9c0
}

.n1024 {
    background-color: #33b5e5
}

.n2048 {
    background-color: #09c9c0
}

.n4096 {
    background-color: #a6c
}

.n8192 {
    background-color: #93c
}

.n2,
.n4 {
    color: #776e65
}

.n1024,
.n2048,
.n4096,
.n8192 {
    font-size: 40px
}

结构部分

<body>
    <div class="main_box">
        <div class="min_box" id="number00"></div>
        <div class="min_box" id="number01"></div>
        <div class="min_box" id="number02"></div>
        <div class="min_box" id="number03"></div>
        <div class="min_box" id="number10"></div>
        <div class="min_box" id="number11"></div>
        <div class="min_box" id="number12"></div>
        <div class="min_box" id="number13"></div>
        <div class="min_box" id="number20"></div>
        <div class="min_box" id="number21"></div>
        <div class="min_box" id="number22"></div>
        <div class="min_box" id="number23"></div>
        <div class="min_box" id="number30"></div>
        <div class="min_box" id="number31"></div>
        <div class="min_box" id="number32"></div>
        <div class="min_box" id="number33"></div>
    </div>

    <script src="2048-2.js"></script>
</body>

功能部分

左移

leftMove() {
        this.num = this.panduan();
        if(this.num==16) {
        this.moverl();
        }
        if(this.num<16||this.canmoverl==true){
            for (var r = 0; r < this.RN; r++) {
                for (var c = 0; c < this.RC; c++) {
                    //当前有值
                    if (this.data[r][c]) {
                        for (var i = c + 1; i < this.RC; i++) {
                            //当前位置后的c+1 值不为0时
                            if (this.data[r][i]) {
                                //后面有值
                                if (this.data[r][c] === this.data[r][i]) {
                                    this.data[r][c] *= 2;
                                    //把后面的值清空
                                    this.data[r][i] = 0;
                                    break;
                                } else {
                                    break;
                                }
                            }
                        }
                    } else {
                        //判断其后是否有值
                        for (var j = c + 1; j < this.RC; j++) {
                            if (this.data[r][j]) {
                                this.data[r][c] = this.data[r][j];
                                this.data[r][j] = 0;
                                c--;
                                break;
                            }
                        }
                    }
                }
            }
            this.getRandom();
            this.updateView();
        }else{
           
        }
    }

右移

rightMove() {
        this.num = this.panduan();
        if(this.num==16) {
            this.moverl();
            }
        if(this.num<16||this.canmoverl==true){
            for (var r = 0; r < this.RN; r++) {
                for (var c = this.RC - 1; c >= 0; c--) {
                    //当前有值
                    if (this.data[r][c]) {
                        for (var i = c - 1; i >= 0; i--) {
                            //当前位置前的c-1 值不为0时
                            if (this.data[r][i]) {
                                //后面有值
                                if (this.data[r][c] === this.data[r][i]) {
                                    this.data[r][c] *= 2;
                                    //把后面的值清空
                                    this.data[r][i] = 0;
                                    break;
                                } else {
                                    break;
                                }
                            }
                        }
                    } else {
                        //判断其后是否有值
                        for (var j = c - 1; j >= 0; j--) {
                            if (this.data[r][j]) {
                                this.data[r][c] = this.data[r][j]
                                this.data[r][j] = 0;
                                c++;
                                break;
                            }
                        }
                    }
                }
            }
            this.getRandom();
            this.updateView();
        }else{
           
        }
    }

上移

topMove() {
        this.num = this.panduan();
        if(this.num==16) {
            this.movetb();
            }
        if(this.num<16||this.canmovetb==true){ 
            for (var c = 0; c < this.RC; c++) {
                for (var r = 0; r < this.RN; r++) {
                    //当前位置有值
                    if (this.data[r][c]) {
                        for (var i = r + 1; i < this.RN; i++) {
                            //当期位置 r+1开始遍历为是否0 
                            if (this.data[i][c]) {
                                //下面有值
                                if (this.data[i][c] === this.data[r][c]) {
                                    this.data[r][c] *= 2;
                                    // 把下面的值清空 
                                    this.data[i][c] = 0;
                                    break;
                                } else {
                                    break;
                                }
                            }
                        }
                        if(r==3){
                            break; 
                        }
                        //当前位置没值
                    } else {
                        // 判断其下面是否有值
                        for (var j = r + 1; j < this.RN; j++) {
                            if (this.data[j][c]) {
                                this.data[r][c] = this.data[j][c];
                                this.data[j][c] = 0;
                                r--;
                                break;
                            }
                        }
    
                    }
                }
            }
    
            this.getRandom();
            this.updateView();
        }else{
           
        }
    },

下移

 bottomMove() {
        this.num = this.panduan();
        if(this.num==16) {
            this.movetb();
            }
        if(this.num<16||this.canmovetb==true){ 
            for (var c = this.RC-1; c >=0; c--) {
                for (var r = this.RN-1; r >=0; r--) {
                    //当前位置有值
                    if (this.data[r][c]) {
                        for (var i = r - 1; i >=0; i--) {
                            //当期位置 r+1开始遍历为是否0 
                            if (this.data[i][c]) {
                                //下面有值
                                if (this.data[i][c] === this.data[r][c]) {
                                    this.data[r][c] *= 2;
                                    // 把下面的值清空 
                                    this.data[i][c] = 0;
                                    break;
                                } else {
                                    break;
                                }
                            }
                        }
                        if(r==3){
                            break; 
                        }
                        //当前位置没值
                    } else {
                        // 判断其下面是否有值
                        for (var j = r - 1; j >=0; j--) {
                            if (this.data[j][c]) {
                                this.data[r][c] = this.data[j][c];
                                this.data[j][c] = 0;
                                r++;
                                break;
                            }
                        }
                        if(j==3){
                            break; 
                        }
                    }
                }
            }
    
            this.getRandom();
            this.updateView();
        }else{
          
        }
        
    },

判断格子是否满格

    panduan() {
        var num=0;
        for(var i = 0;i<this.RN;i++){
            for(var j=0;j<this.RC;j++){
                if(this.data[i][j]!=0){
                    num++
                }
            }
        }
        this.canmoverl =false;
        this.canmovetb =false;
        return num;
     
    

判断满格时是否可以移动

可否左右移动:

    moverl() {
        for(var i = 0;i<this.RN;i++){
            for(var j=0;j<this.RC;j++){
                if(this.data[i][j]==this.data[i][j+1]){
                   this.canmoverl =true;
                    break;
                }
            }
        }
    },

可否上下移动:

    movetb() {
        for(var i = 0;i<this.RN;i++){
            for(var j=0;j<this.RC;j++){
                if(this.data[j][i]==this.data[j+1][i]){
                   this.canmovetb =true;
                    break;
                }
            }
        }
    },
    
};

随机数生成函数

    getRandom() {
        var r, c;
        while (true) {
            r = parseInt(Math.random() * this.RN);
            c = parseInt(Math.random() * this.RC);

            if (this.data[r][c] != 0) {
            }else {
                this.data[r][c] = Math.random() < 0.5 ? 2 : 4;
                break;
            }
        }
    },

渲染视图函数

 updateView() {
        for (var r = 0; r < this.RN; r++) {
            for (var c = 0; c < this.RC; c++) {
                //this.data[r][c]
                //将页面不为0的数据渲染到页面中 
                if (this.data[r][c]) {
                    document.getElementById("number" + r + c).innerHTML = this.data[r][c];
                    //给到对应的背景颜色
                    document.getElementById('number' + r + c).className = 'min_box n' + (this.data[r][c]);

                } else {
                    // 当前位置值为0时清空背景颜色
                    document.getElementById('number' + r + c).innerHTML = "";
                    document.getElementById("number" + r + c).className = 'min_box';
                }
            }

        }
    },

初始状态函数

    start() {
        for (var i = 0; i < this.RN; i++) {
            this.data[i] = [];
            for (var j = 0; j < this.RC; j++) {
                this.data[i][j] = 0;
            }
        }

        this.getRandom();
        this.getRandom();
        this.updateView();
    },

常量定义

    RN: 4,
    RC: 4,
    data: [],
    num:0,
    canmoverl:false,
    canmovetb:false,
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值