Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id='score'>分数: <span id='realScore'>0</span></div>
	<section id='container'>
		<div id='0-0'></div><div id='0-1'></div><div id='0-2'></div><div id='0-3'></div><div id='1-0'></div><div id='1-1'></div><div id='1-2'></div><div id='1-3'></div><div id='2-0'></div><div id='2-1'></div><div id='2-2'></div><div id='2-3'></div><div id='3-0'></div><div id='3-1'></div><div id='3-2'></div><div id='3-3'></div>
	</section>
              
            
!

CSS

              
                *{
	margin:0;
	padding:0;
}
html{
	font-size:12px;
}

body{
	background:#faf8ef;
	color:#776e65;
	font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
}
#container{
	position:relative;
	left:35%;
	top:15rem;
	background:#bbada0;
	width:32rem;
	height:32rem;
	padding:0.5rem;
	border-radius:0.7rem;
}

#score{
	display:inline-block;
	position:absolute;
	font-size:3rem;
	padding-top:1rem;
	text-align:center;
	top:5rem;
	left:44%;
	width:16rem;
	height:4rem;
	background:#bbada0;
	border-radius:0.5rem;
}

#score span{
	color:white;
}

#container div{
	font-weight:bold;	
	padding-top:1.5rem;
	font-size:4rem;
	color:white;
	text-align:center;
	box-sizing:border-box;
	display:inline-block;
	width:8rem;
	height:8rem;
	background:rgba(238, 228, 218, 0.35);
	vertical-align:middle;
	border:0.5rem solid #bbada0;
}

.bgcolor-2{
	background:'#green';
}

.bgcolor{
	background:rgba(238, 228, 218, 0.35);
}

.bgcolor-4{
	background:'red';
}
              
            
!

JS

              
                /*
	Author: 林水溶
	英文名:shuiRongLin
	博客: https://shuirong.github.io

	!-------转载请保留上面作者信息-------!人最起码的东西得有是吧。
	
	思路:不管是这个1024游戏还是其他任何程序,思路最重要,用代码写出来反而是次要的东西!

	程序流程:打开页面,程序运行-->先随机出个4-->人操作(上下左右)-->(根据人的按键)对棋盘进行解析,
	该合并合并,该不动不动,该移动的移动,同时随机再出个4-->重复上一步-->在人按了512步之后,每次人按之后判断2048出现了没

	PS:之所以512步之前不判断赢了没,是因为最快赢需要512步,但实际情况下不太可能。之前判断没必要
*/

var checkerBoard = [
					[0,0,0,0],
					[0,0,0,0],
					[0,0,0,0],
					[0,0,0,0]
				]  // 初始化游戏棋盘,0就是什么都没有,
var checkerBoardId = [
					['0-0','0-1','0-2','0-3'],
					['1-0','1-1','1-2','1-3'],
					['2-0','2-1','2-2','2-3'],
					['3-0','3-1','3-2','3-3']
				]//棋盘对应位置的元素id,方便后面用
var color = {
	'2': '#eee4da',
	'4': '#ede0c8',
	'8': '#f2b179',
	'16': '#f59563',
	'32': '#f67c5f',
	'64': '#f65e3b',
	'128': '#edcf72',
	'256': '#edcc61',
	'512': '#edc850',
	'1024': '#edc53f',
	'2048': '#edc22e',
}
var score = 0; //记录分数

window.onload = function(){
	start();
}

//主函数
function start(){
	randomTwo();
	randomTwo();
	//给键盘的上下左右绑定监听事件  上 下 左 右--38 40 37 39
	$(document).keyup(function(event){
		if(event.which>=37&&event.which<=40){
			judge(event.which);//把用户的按键行为传进去
		}
	});
}

//在棋盘上随机出现2
function randomTwo(){
	let status = false;//随机4是不是搞定了
	while(!status){//随机xy直到棋盘上对应位置的值是0
		let x = Math.floor(Math.random()*4);
		let y = Math.floor(Math.random()*4);
		if(checkerBoard[x][y]===0){
			checkerBoard[x][y]=2;
			status = true;
		}
	}
	render();
}

//根据用户的按键对棋盘上现有的棋子依照游戏规则进行计算,将结果体现在全局变量checkerBoard上
// 上下左右不管哪个,棋盘上的棋子每个都有三种可能,拿上举例。1:它上面是0或者是边缘。2:它上面有数字且不一样
//3:上面有数字且一样。这就12中情况,得想办法抽象化,代码重用。
function judge(button){
	
	if(button==38){ //上
		for(var j=0; j<4; j++){
			for(var i=1; i<4;i++){
				if(checkerBoard[i][j]!==0){ //第一排的话,不用动,所以从第二排开始计算
					for(var m=0; m<i; m++){
						if(checkerBoard[m][j]==0&&!shuCanMove(j,i,m)){
							checkerBoard[m][j] = checkerBoard[i][j];
							checkerBoard[i][j] = 0;
						}else if(checkerBoard[m][j]==checkerBoard[i][j]&&!shuCanMove(j,i,m)){
							checkerBoard[m][j] = (checkerBoard[m][j] * 2);
							checkerBoard[i][j] = 0;
							score += checkerBoard[m][j];
						}
					}
				}
			}	
		}
	}else if(button==40){ //下
		for(var j=0; j<4; j++){
			for(var i=2; i>=0;i--){
				if(checkerBoard[i][j]!==0){
					for(var m=3; m>i;m--){
						if(checkerBoard[m][j]==0&&!shuCanMove(j,i,m)){
							checkerBoard[m][j] = checkerBoard[i][j];
							checkerBoard[i][j] = 0;
						}else if(checkerBoard[m][j] == checkerBoard[i][j] && !shuCanMove(j,i,m)){
							checkerBoard[m][j] = (checkerBoard[m][j] * 2);
							checkerBoard[i][j] = 0;
							score += checkerBoard[m][j];
						}
					}
				}
			}
		}
	}else if(button == 37){
		for(var i=0; i<4; i++){
			for(var j=1; j<4; j++){
				if(checkerBoard[i][j]!=0){
					for(var m=0; m<j; m++){
						if(checkerBoard[i][m]==0 && !hengCanMove(i,j,m)){
							checkerBoard[i][m] = checkerBoard[i][j];
							checkerBoard[i][j] = 0;
						}else if(checkerBoard[i][m] == checkerBoard[i][j]&&!hengCanMove(i,j,m)){
							checkerBoard[i][m] = (checkerBoard[i][m]*2);
							checkerBoard[i][j] = 0;
							score += checkerBoard[i][m];
						}
					}
				}
			}
		}
	}else if(button == 39){
		for(var i=0; i<4; i++){
			for(var j=2; j>=0; j--){
				if(checkerBoard[i][j]!=0){
					for(var m=3; m>j; m--){
						if(checkerBoard[i][m]==0&&!hengCanMove(i,j,m)){
							checkerBoard[i][m] = checkerBoard[i][j];
							checkerBoard[i][j] = 0;
						}else if(checkerBoard[i][m] == checkerBoard[i][j]&&!hengCanMove(i,j,m)){
							checkerBoard[i][m] = (checkerBoard[i][m]*2);
							checkerBoard[i][j] = 0;	
							score += checkerBoard[i][m];
						}
					}
				}
			}
		}
	}
	render();
	randomTwo();
}

//根据checkerBoard将棋盘的值重新渲染一遍
function render(){
	checkerBoard.forEach(function(x,xIndex){
		x.forEach(function(y,yIndex){
			if(y!==0){
				text(checkerBoardId[xIndex][yIndex],y);
			}else{
				text(checkerBoardId[xIndex][yIndex],'');
			}
		})
	});
	document.getElementById('realScore').innerText = score;
}

//不知道为何 $(id).text(123) 和 $(id).html(123) 不能用了,不管它,自己造了个
function text(id,text){
	let ele = document.getElementById(id);
	ele.innerText = text;
	ele.setAttribute('style','background:'+color[text]);	
}

function conso(){
	console.log(checkerBoard[0][0],checkerBoard[0][1],checkerBoard[0][2],checkerBoard[0][3]);
	console.log(checkerBoard[1][0],checkerBoard[1][1],checkerBoard[1][2],checkerBoard[1][3]);
	console.log(checkerBoard[2][0],checkerBoard[2][1],checkerBoard[2][2],checkerBoard[2][3]);
	console.log(checkerBoard[3][0],checkerBoard[3][1],checkerBoard[3][2],checkerBoard[3][3]);	
	console.log('-----------------------');
}

function shuCanMove(j,i,m){
	var from,to;
	if(i>m){
		from = m;
		to = i;
	}else{
		from = i;
		to = m;
	}
	for(var n=from+1; n<to;n++){
		if(checkerBoard[n][j]!=0){
			return true;
		}
	}
	return false;
}

function hengCanMove(j,i,m){
	var from,to;
	if(i>m){
		from = m;
		to = i;
	}else{
		from = i;
		to = m;
	}

	for(var n=from+1;n<to;n++){
		if(checkerBoard[j][n]!=0){
			return true;
		}
	}
	return false;

}
              
            
!
999px

Console