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

              
                <html>
  <link href="https://fonts.googleapis.com/css?family=Rye" rel="stylesheet">
<body onkeyup="start(event)"><h1><b>2048 Game</b></h1><div id="score"></div><button onClick="newGame()">New Game</button><br><br>
        <div class="grid-container">
      <div class="grid-row">
        <div class="grid-cell" id="1" ></div>
        <div class="grid-cell" id="2"></div>
        <div class="grid-cell" id="3"></div>
        <div class="grid-cell" id="4"></div>
      </div>
      <div class="grid-row">
        <div class="grid-cell" id="5"></div>
        <div class="grid-cell" id="6"></div>
        <div class="grid-cell" id="7"></div>
        <div class="grid-cell" id="8"></div>
      </div>
      <div class="grid-row">
        <div class="grid-cell" id="9"></div>
        <div class="grid-cell" id="10"></div>
        <div class="grid-cell" id="11"></div>
        <div class="grid-cell" id="12"></div>
      </div>
      <div class="grid-row">
        <div class="grid-cell" id="13"></div>
        <div class="grid-cell" id="14"></div>
        <div class="grid-cell" id="15"></div>
        <div class="grid-cell" id="16"></div>
      </div>
    </div>
</body>
</html>
              
            
!

CSS

              
                .grid-cell {
  width: 75px;
  height: 75px;
  margin-right: 15px;
  float: left;
  position:relative;
  background:#d3d3d3;
  box-shadow:0px 0px 0px 10px #A9A9A9; 
  text-align:center; 
  padding:50px;
}
.grid-row {
  margin-bottom:15px;
  height:100px;
  width:800px;
  }
p
{
  margin-top:40px
}
.grid-container
{
  height:450px;
  width:450px;
  border:10px inset #d3d3d3;
  font-size:40px;
}
h1
{
  font-size:60px;
  font-family: 'Rye', cursive;
}
span{
 
}
button
{
  height:40px;
  width:150px;
  font-family: 'Rye', cursive;
  font-size:20px;
  font-weight:bold;
  float:left;
  margin-left:340px;
  margin-top:-20px;
}
button:hover
{
  cursor:pointer;
  background:#0C090A;
  color:white  
}
#score
{
 
  width:75px;
  background-color:#d3d3d3;
  padding:20px;
  font-size:25px;
  margin:10px 0px -30px 200px ;
  text-align:center;
}

body 
{
  margin:0% 50% 0% 25%;
}
              
            
!

JS

              
                //random function
var id1;var list1;var list1Left=[];var score=0; var check;
random();random();
function random() { 
     document.getElementById("score").innerHTML= score;
    id1 = Math.ceil(Math.random() * 16);
    var a = document.getElementById(id1).innerText;
    if (a == "") {
        document.getElementById(id1).innerText = "2";
        document.getElementById(id1).style.background="#F0FFFF"
    } else {
        random();
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
//take value function
function list2() {
    list1 = [[],[],[],[]];
    for (i = 1; i <= 16; i++) {
        var key = Math.ceil(i / 4);
        list1[key - 1].push(document.getElementById(i).innerHTML);
    }
    return list1;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
//moveing function
function wrappedMoveNumber(conditionArray1, conditionArray2, cb,count){
  var number = [0,1,2,3];
  number.forEach(function(){
    conditionArray1.forEach(function(k){
      conditionArray2.forEach(function(i){
          cb(i,k);
      })
    })
  })  
 if (count==1){
 if(cb==left_arrow)
    {
       wrappedAddNumber(addObj.left.k,addObj.left.i,leftAdd);
    }
  else if(cb==down_arrow)
     {
       wrappedAddNumber(addObj.right.k,addObj.right.i,downAdd);
     }
  else if(cb==up_arrow)  
     {
        wrappedAddNumber(addObj.left.k,addObj.left.i,upAdd);
     }                                          
 else if (cb==right_arrow)
    {
      wrappedAddNumber(addObj.right.k,addObj.right.i,rightAdd);
    }

}  else if(count==2)
      {
       printing(list1);
       random();lose_Check();
      }
}
function up_arrow(i,k)
{
  if (list1[i][k] === "") 
  {
   list1[i][k] = list1[i+1][k];
   list1[i+1][k] = "";
   }
}
function down_arrow(i,k)
{  
   if (list1[i][k] === "")
   {
     list1[i][k] = list1[i-1][k];
     list1[i-1][k] = "";
   }
}
function left_arrow(i,k)
{ 
  if (list1[k][i] === "") 
  {
    list1[k][i] = list1[k][i+1];
    list1[k][i+1] = "";
  }
}
function right_arrow(i,k)
{ 
   if (list1[k][i] === "")
   {
     list1[k][i] = list1[k][i-1];
     list1[k][i-1] = "";
   } 
}
//----------------------------------------------------------------------------------------------------------------------------------------------------
var moveObj={
    left:{
      i:[0,1,2],
      k:[0,1,2,3] },
  right:{
    i:[3,2,1],
    k:[0,1,2,3] },
  up:{
    i:[0,1,2],
    k:[0,1,2,3]},
  down:{
    i:[3,2,1],
    k:[3,2,1,0] }   
}
var addObj={
  left:{
    i:[1,2,3],
    k:[0,1,2,3]},
  right:{
    i:[3,2,1],
    k:[3,2,1,0]},  
  }
var colour={
  0:"#d3d3d3",
  2:"#F0FFFF",
  4:"#FFE4C4",
  8:"#FFA500",
  16:"#FFA07A",
  32:"#FF7F50",
  64:"#FF0000",
  128:"#ffff99",
  256:"#ffff66",
  512:"#FFFF00",
  1024:" #e6e600",
  2048:"#FFD700"
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
//start function
function start(event)
{
  var key_value=event.keyCode;list2();console.log(score);
  if (key_value===37)
      {
      //left
        wrappedMoveNumber(moveObj.left.k,moveObj.left.i,left_arrow,1);
        
      }
  else if (key_value===38)
      {
      //up        
        
        wrappedMoveNumber(moveObj.up.k,moveObj.up.i,up_arrow,1);
      }
  else if (key_value===39)
      {
      //right
        wrappedMoveNumber(moveObj.right.k,moveObj.right.i,right_arrow,1);
      }
  else if (key_value===40)
      {
      //down
        wrappedMoveNumber(moveObj.down.k,moveObj.down.i,down_arrow,1);
      }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//add function
function wrappedAddNumber(conditionArray3, conditionArray4,callback)
{
  conditionArray3.forEach(function(k){
      conditionArray4.forEach(function(i){
          callback(k,i);
      })
    })
     
   if(callback==leftAdd)
    { 
       wrappedMoveNumber(moveObj.left.k,moveObj.left.i,left_arrow,2);
    }
  else if(callback==downAdd)
     {
       wrappedMoveNumber(moveObj.down.k,moveObj.down.i,down_arrow,2);
     }
  else if(callback==upAdd)  
     {
        wrappedMoveNumber(moveObj.up.k,moveObj.up.i,up_arrow,2);
     }                                          
 else if (callback==rightAdd)
    {
      wrappedMoveNumber(moveObj.right.k,moveObj.right.i,right_arrow,2);
    }
}
function upAdd(k,i)
{
   if (list1[i-1][k]==list1[i][k])
     {
       list1[i-1][k]=Number(list1[i-1][k])+Number(list1[i][k]);
       score +=Number(list1[i-1][k]);
       list1[i][k]="";
     }
}
function downAdd(k,i)
{
  if (list1[i-1][k]==list1[i][k])
     {
       list1[i][k]=Number(list1[i-1][k])+Number(list1[i][k]);
      score +=Number(list1[i][k]);
       list1[i-1][k]="";
     }
}

function leftAdd(k,i)
{ 
  if (list1[k][i]===list1[k][i-1])
      {
        list1[k][i]=Number(list1[k][i-1])+Number(list1[k][i-1]);
        score +=Number(list1[i][k]);
        list1[k][i-1]="";
      }
}
function rightAdd(k,i)
{
  
   if (list1[k][i]===list1[k][i-1])
      {
        list1[k][i-1]=Number(list1[k][i-1])+Number(list1[k][i]);
        score +=Number(list1[i-1][k]);
        list1[k][i]="";
      }
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//printing  function
function printing(list)
{ 
  for (var x=0,y=1;x<4;x++)
    {
      for(var z=0;z<4;z++)
        {if(list[x][z]==0)
          {
            list[x][z]="";
          }
          document.getElementById(y).innerText=list[x][z];
          var colors=Number(list[x][z])%2048;
         document.getElementById(y).setAttribute("style","background-color:"+colour[colors]+";text-align:center;")
           y++;
        }
    }
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//lose function
function lose_Check()
 {
  stop: for (var i=0;i<4;i++)
     {
       for (var j=0;j<3;j++)
         {
           if (list1[i][j]==list1[i][j+1] || list1[i][j]==list1[i+1][j])
             {
                check=true;
	            	break stop;
             }
           else
             {
               check=false;
    		
             }
         }
     }
    if (check!==true)

	{
         alert("Game Over");   
	}
 }
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//new game function
function newGame()
{ 
  for (var i = 1; i <= 16; i++)
    {
      document.getElementById(i).innerText = "";
      document.getElementById(i).setAttribute("style","background:'#d3d3d3';box-shadow:0px 0px 0px 10px #A9A9A9;")
    }score=0;
    random();
    random();
}
//--------------------------------------------------------------------------------------------------------------------------------------------

              
            
!
999px

Console