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 class="board" id="game_board">
  <div class="cell" row="0" col="0"></div>
  <div class="cell" row="0" col="1"></div>
  <div class="cell" row="0" col="2"></div>
  <div class="cell" row="0" col="3"></div>

  <div class="cell" row="1" col="0"></div>
  <div class="cell" row="1" col="1"></div>
  <div class="cell" row="1" col="2"></div>
  <div class="cell" row="1" col="3"></div>

  <div class="cell" row="2" col="0"></div>
  <div class="cell" row="2" col="1"></div>
  <div class="cell" row="2" col="2"></div>
  <div class="cell" row="2" col="3"></div>

  <div class="cell" row="3" col="0"></div>
  <div class="cell" row="3" col="1"></div>
  <div class="cell" row="3" col="2"></div>
  <div class="cell" row="3" col="3"></div>
</div>

<div class="help tool-tip">
  <div class="trigger fa fa-question-circle fa-lg"></div>
  <div class="content">
    Use the arrow keys
  </div>
</div>


<div class='credit'>  <h3>Thank you to:</h3>
  <a href='https://fortawesome.github.io/Font-Awesome/'>Font Awesome Icons</a>
</div>
              
            
!

CSS

              
                *, *:after, *:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

/* =========================================================
   Declare colors, fonts
   ========================================================= */

/* body */
$bg_col : #373633;
$font_family : 'Lato', Calibri, Arial, sans-serif; 
$main_font_color: #ffffff;

/* grid */
$grid_bg_col: #ededed;
$grid_border: none;
$grid_cells_bg_col: #bdbdbd;
$grid_cells_border: none;

$cell_border_rad: 10px;

/* modals */
$md_bg_col: #63997A;
$md_hd_bg_color: #40805C;
$md_overlay_col: rgba(0,0,0,0.9);
$md_col: #ffffff;

$n1_bg_col: #11644D;
$n2_bg_col: #A0B046;
$n3_bg_col: #F2C94E;
$n4_bg_col: #F78145;
$n5_bg_col: #F24E4E;

$n6_bg_col: #183B64;
$n7_bg_col: #71A341;
$n8_bg_col: #F2E34E;
$n9_bg_col: #F7B045;
$n10_bg_col: #F2984E;
$n11_bg_col: #F2E34E;

$num_col_1: #ffffff;
$num_col_2: #ffffff;

body   {
  background-color: $bg_col;
  font-family: $font_family;
  color: $main_font_color;
  text-align: center;
}


/**********************************
 *           GRID STUFF           *
 **********************************/

.board	{
  background: $grid_bg_col;
  border: $grid_border;
  text-align: center;
  margin: 50px auto 50px auto;
  width:500px;
}

.board:after {
  content: "";
  display: table;
  clear: both;
}

.cell {
  background-color: $grid_cells_bg_col;
  border: $grid_cells_border;
  border-radius: $cell_border_rad;
  
  float: left;
  width: 100px;
  height: 100px;
  margin: 12.5px;
}

.tile { 
  position: relative;
  border-radius: $cell_border_rad;
  height: 100px;
  width: 100px;
  color: $num_col_1;
  font-size: 36px;
  padding-top: 25px;
}

.n1 {  background-color: $n1_bg_col;  }
.n2 {  background-color: $n2_bg_col;  }
.n3 {  background-color: $n3_bg_col;  }
.n4 {  background-color: $n4_bg_col;  }
.n5 {  background-color: $n5_bg_col;  }
.n6 {  background-color: $n6_bg_col;  }
.n7 {  background-color: $n7_bg_col;  }
.n8 {  background-color: $n8_bg_col;  }
.n9 {  background-color: $n9_bg_col;  }
.n10 {  background-color: $n10_bg_col;  }
.n11 {
  background-color: $n11_bg_col;
  -webkit-box-shadow: 0px 0px 25px 1px rgba(242,227,78,1);
  -moz-box-shadow: 0px 0px 25px 1px rgba(242,227,78,1);
  box-shadow: 0px 0px 25px 1px rgba(242,227,78,1);
}

.n1::before {  content: '2';  }
.n2::before {  content: '4';  }
.n3::before {  content: '8';  }
.n4::before {  content: '16';  }
.n5::before {  content: '32';  }
.n6::before {  content: '64';  }
.n7::before {  content: '128';  }
.n8::before {  content: '256';  }
.n9::before {  content: '512';  }
.n10::before {  content: '1024';  }
.n11::before {  content: '2048';  }







/***********************************
 *           MODAL STUFF           *
 ***********************************/

.md {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: $md_overlay_col;
  z-index: 100;
}

.md-content {
  background-color: $md_bg_col;
  color: $md_col;
  position: relative;
  width: 500px;
  margin: 100px auto;
  padding:20px;
}

/*------------
    Variants  
  ------------*/

.md-hd {
  margin-bottom: 10px;
  padding: 0.4em;
  text-align: center;
  font-size: 1.2em;
  font-weight: 600;
  letter-spacing: 1px;
  background-color: $md_hd_bg_color;
}

.md-ft {
  /* TODO */
}

/**********************************
 *           IMAGE STUFF          *
 **********************************/


/**********************************
 *           MISC STUFF           *
 **********************************/

.left   {
  float: left;
}

.right   {
  float: right;
}

.hidden   {
  display: none;
}

.credit {
  margin: 50px;
  a   {
    color: white;
  }
}

.tool-tip {  
    position: relative;
  .trigger {
    opacity: 1;
    -webkit-transition: opacity 0.3s;
	  transition: opacity 0.3s;
  }
  .content {
    opacity: 0;
    position: absolute;
    top: 0px;
    left:0%;
    width:100%;height:100%;
    text-align: center;
    
    -webkit-transition: opacity 0.3s;
	  transition: opacity 0.3;
  }
}

.tool-tip:hover {
  .trigger {
    opacity: 0;
  }
  .content {
    opacity: 1;
  }
}

              
            
!

JS

              
                $(document).ready(function() {
  /* =========================================================
                              CONFIG
     ========================================================= */

  var GAME_SIZE = 4;
  var ANIMATION_SPEED = 200;
  /* =========================================================
                              GLOBALS
     ========================================================= */

  var GAME_BOARD;

  /* =========================================================
                            GAME LOGIC
     ========================================================= */
  function init_game() {
    GAME_BOARD = [['','','',''],['','','',''],['','','',''],['','','','']];
    generate_new_tile();
  }

  init_game();


  function is_board_full() {
    for (var i=0;i<GAME_SIZE;i++) {
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[i][j] == '') {
          return false;
        }
      }
    }
    return true;
  }

  function generate_new_tile() {
    if (is_board_full()) {
      alert('game over');
      $('#debug_btn1').remove();
    }
    var tile_val = Math.floor((Math.random()*2)+1);
    var r,c;
    var tester=0;
    while (true && tester < 1000000) {
      r = Math.floor(Math.random()*4);
      c = Math.floor(Math.random()*4);
      tester+=1;
      if (GAME_BOARD[r][c] == '') { break; }
    }
    make_tile(tile_val,r,c,true);
    GAME_BOARD[r][c] = tile_val;
  }

  function combine_pairs(row,reverse) {
    if (typeof(reverse) != 'boolean') {reverse = false;}    
    if (row.length == 0) {
      return row;
    }
    var new_row;
    if (reverse) {
      new_row = [row[row.length-1]];
      for (var i=row.length-2; i>=0;i--)   {
        if (row[i] == new_row[new_row.length-1]) {
          new_row[new_row.length-1] += 1;
          if (i > 0) {
            new_row.push(row[i-1]);
            i -= 1;
          }
        }
        else {
          new_row.push(row[i]);
        }
      }
      new_row.reverse();
    }
    else {
      /*  [2,2,4] ==> [4,4]

      */
      new_row = [row[0]];
      for (var i=1; i<row.length;i++)   {
        if (row[i] == new_row[new_row.length-1]) {
          new_row[new_row.length-1] += 1;
          if (i < row.length-1) {
            new_row.push(row[i+1]);
            i += 1;
          }
        }
        else {
          new_row.push(row[i]);
        }
      }
    }
    return new_row;
  }

  function move_left() {
    var new_row,m;
    for (var i=0;i<GAME_SIZE;i++) {
      new_row = [];
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[i][j] != '') {
          new_row.push(GAME_BOARD[i][j]);
        }
      }
      new_row = combine_pairs(new_row,false);
      m = GAME_SIZE-new_row.length;
      for (var k=0;k<m;k++) {
        new_row.push('');
      }
      GAME_BOARD[i] = new_row;
    }
  }

  function move_right() {
    var new_row,m;
    for (var i=0;i<GAME_SIZE;i++) {
      new_row = [];
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[i][j] != '') {
          new_row.push(GAME_BOARD[i][j]);
        }
      }
      new_row = combine_pairs(new_row,true);
      m = GAME_SIZE-new_row.length;
      for (var k=0;k<m;k++) {
        new_row.unshift('');
      }
      GAME_BOARD[i] = new_row;
    }
  }

  function replace_col(col,c) {
    for (var r=0; r<GAME_SIZE;r++) {
      GAME_BOARD[r][c] = col[r];
    }
  }

  function move_up() {
    var new_col,m;
    for (var i=0;i<GAME_SIZE;i++) {
      new_col = [];
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[j][i] != '') {
          new_col.push(GAME_BOARD[j][i]);
        }
      }
      new_col = combine_pairs(new_col,false);
      m = GAME_SIZE-new_col.length;
      for (var k=0;k<m;k++) {
        new_col.push('');
      }
      replace_col(new_col,i);
    }
  }

  function move_down() {
    var new_col,m;
    for (var i=0;i<GAME_SIZE;i++) {
      new_col = [];
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[j][i] != '') {
          new_col.push(GAME_BOARD[j][i]);
        }
      }
      new_col = combine_pairs(new_col,true);
      m = GAME_SIZE-new_col.length;
      for (var k=0;k<m;k++) {
        new_col.unshift('');
      }
      replace_col(new_col,i);
    }
  }

  /* =========================================================
                          DISPLAY LOGIC
     ========================================================= */

  function make_tile(tile_val,r,c,new_tile) {
    var jq = '.cell[row="' + r.toString() + '"][col="' + c.toString() + '"]';
    if (tile_val == '') {
      $(jq).html('');
      return;
    }
    var tile_html = '<div class="tile n'+ tile_val.toString() + '"></div>';
    $(jq).html(tile_html);
    if (new_tile) {
      var $tile = $(jq).children();
      $tile.css('height','80px');
      $tile.css('width','80px');
      $tile.css('margin','10px');
      $tile.animate({'height':'100px','width':'100px','margin':'0px'},ANIMATION_SPEED-50,'linear');
    }
  }

  function update_board() {
    var jq,val;
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        jq = '.cell[row="' + r.toString() + '"][col="' + c.toString() + '"]';
        // get the current value of this cell
        if ($(jq).html() != '') { 
          val = Number($(jq).children().attr('class').substring(6)); 
        }
        else                    { 
          val = ''; 
        }
        // check if the current cell and GAME_BOARD[r][c] don't match
        if (GAME_BOARD[r][c] != val) {
          make_tile(GAME_BOARD[r][c],r,c);
        } 
      }
    }
  }

  function animate_move_left() {
    var jq,val,animate_row,num_free;
    var animate_grid = [];
    var combo_flag = false;
    // build animation grid
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        if (c==0) {
          if (GAME_BOARD[r][c] == '') {
            animate_row = [-1];
            num_free = 1;
          }
          else {
            animate_row = [0];
            num_free = 0;
          }
        }
        else {
          if (GAME_BOARD[r][c] == '') {
            animate_row.push(-1);
            num_free += 1;
          }
          else if (GAME_BOARD[r][c-1] == GAME_BOARD[r][c] && !combo_flag) {
            num_free += 1;
            animate_row.push(num_free);
            combo_flag = true;
          }
          else {
            animate_row.push(num_free);
            combo_flag = false;
          }
        }
      }
      animate_grid.push(animate_row);
    }
    console.log('MOVE LEFT');
    print_2_matrix(GAME_BOARD,animate_grid);
    // use animate grid to animate!
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        if (animate_grid[r][c] <= 0) { continue; }
        jq = '.cell[row="'+r.toString()+'"][col="'+c.toString()+'"] .tile';
        val = (animate_grid[r][c]*125).toString();
        $(jq).animate({'left':'-='+val+'px'},ANIMATION_SPEED,'linear');
        $(jq).animate({'left':'+='+val+'px'},0);
      }
    }
  }

  function animate_move_right() {
    var jq,val,animate_row,num_free;
    var animate_grid = [];
    var combo_flag = false;
    // build animation grid
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=GAME_SIZE-1;c>=0;c-=1) {
        if (c==GAME_SIZE-1) {
          if (GAME_BOARD[r][c] == '') {
            animate_row = [-1];
            num_free = 1;
          }
          else {
            animate_row = [0];
            num_free = 0;
          }
        }
        else {
          if (GAME_BOARD[r][c] == '') {
            animate_row.unshift(-1);
            num_free += 1;
          }
          else if (GAME_BOARD[r][c+1] == GAME_BOARD[r][c] && !combo_flag) {
            num_free += 1;
            animate_row.unshift(num_free);
            combo_flag = true;
          }
          else {
            animate_row.unshift(num_free);
            combo_flag = false;
          }
        }
      }
      animate_grid.push(animate_row);
    }
    console.log('MOVE RIGHT');
    print_2_matrix(GAME_BOARD,animate_grid);
    // use animate grid to animate!
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        if (animate_grid[r][c] <= 0) { continue; }
        jq = '.cell[row="'+r.toString()+'"][col="'+c.toString()+'"] .tile';
        val = (animate_grid[r][c]*125).toString();
        $(jq).animate({'right':'-='+val+'px'},ANIMATION_SPEED,'linear');
        $(jq).animate({'right':'+='+val+'px'},0);
      }
    }
  }

  function add_col(grid,col) {
    for (var i=0;i<col.length;i++) {
      grid[i].push(col[i]);
    }
    return grid;
  }
  function animate_move_up() {
    var jq,val,animate_col,num_free;
    var animate_grid = [[],[],[],[]];
    var combo_flag = false;
    // build animation grid
    for (var c=0;c<GAME_SIZE;c++) {
      for (var r=0;r<GAME_SIZE;r++) {
        if (r==0) {
          if (GAME_BOARD[r][c] == '') {
            animate_col = [-1];
            num_free = 1;
          }
          else {
            animate_col = [0];
            num_free = 0;
          }
        }
        else {
          if (GAME_BOARD[r][c] == '') {
            animate_col.push(-1);
            num_free += 1;
          }
          else if (GAME_BOARD[r-1][c] == GAME_BOARD[r][c] && !combo_flag) {
            num_free += 1;
            animate_col.push(num_free);
            combo_flag = true;
          }
          else {
            animate_col.push(num_free);
            combo_flag = false;
          }
        }
      }
      animate_grid = add_col(animate_grid, animate_col);      
    }
    console.log('MOVE UP');
    print_2_matrix(GAME_BOARD,animate_grid);
    //use animate grid to animate!
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        if (animate_grid[r][c] <= 0) { continue; }
        jq = '.cell[row="'+r.toString()+'"][col="'+c.toString()+'"] .tile';
        val = (animate_grid[r][c]*125).toString();
        $(jq).animate({'top':'-='+val+'px'},ANIMATION_SPEED,'linear');
        $(jq).animate({'top':'+='+val+'px'},0);
      }
    }
  }

  function animate_move_down() {
    var jq,val,animate_col,num_free;
    var animate_grid = [[],[],[],[]];
    var combo_flag = false;
    // build animation grid
    for (var c=0;c<GAME_SIZE;c++) {
      for (var r=GAME_SIZE-1;r>=0;r-=1) {
        if (r==GAME_SIZE-1) {
          if (GAME_BOARD[r][c] == '') {
            animate_col = [-1];
            num_free = 1;
          }
          else {
            animate_col = [0];
            num_free = 0;
          }
        }
        else {
          if (GAME_BOARD[r][c] == '') {
            animate_col.unshift(-1);
            num_free += 1;
          }
          else if (GAME_BOARD[r+1][c] == GAME_BOARD[r][c] && !combo_flag) {
            num_free += 1;
            animate_col.unshift(num_free);
            combo_flag = true;
          }
          else {
            animate_col.unshift(num_free);
            combo_flag = false;
          }
        }
      }
      animate_grid = add_col(animate_grid, animate_col);
    }
    console.log('MOVE DOWN');
    print_2_matrix(GAME_BOARD,animate_grid);
    // use animate grid to animate!
    for (var r=0;r<GAME_SIZE;r++) {
      for (var c=0;c<GAME_SIZE;c++) {
        if (animate_grid[r][c] <= 0) { continue; }
        jq = '.cell[row="'+r.toString()+'"][col="'+c.toString()+'"] .tile';
        val = (animate_grid[r][c]*125).toString();     
        $(jq).animate({'bottom':'-='+val+'px'},ANIMATION_SPEED,'linear');
        $(jq).animate({'bottom':'+='+val+'px'},0);      
      }
    }
  }
  /* =========================================================
                            USER INPUT
     ========================================================= */

  $('#debug_btn1').click(function() {
    generate_new_tile();
  });
  $('#debug_btn2').click(function() {    
    var jq1 = '.cell[row="0"][col="0"] .tile';
    $(jq1).animate({'right':'-=125px'},ANIMATION_SPEED);
  });

  $(document).keydown(function(key)  {
    switch(parseInt(key.which,10)) {
      case 65:
      case 37:
        animate_move_left();
        move_left();        
        break;
      case 83:
      case 40:
        animate_move_down();
        move_down();
        break;
      case 87:
      case 38:
        animate_move_up();
        move_up();
        break;
      case 68:
      case 39:
        animate_move_right();
        move_right();
        break;
      default:
        break;
    }
    // TODO: only do these things if something has changed
    setTimeout(function(){update_board();},ANIMATION_SPEED+5);
    setTimeout(function(){generate_new_tile();},ANIMATION_SPEED+25);
  });

  /* =========================================================
                        HELPER FUNCTIONS
     ========================================================= */

  function print_board() {
    var s = '';
    for (var i=0;i<GAME_SIZE;i++) {
      for (var j=0;j<GAME_SIZE;j++) {
        if (GAME_BOARD[i][j] != '') {
          s += GAME_BOARD[i][j].toString() + '  ';
        }
        else {
          s += '*  ';
        }               
      }
      s += '\n';
    }
    console.log(s);
  }

  function print_matrix(mat) {
    var s = '';
    for (var i=0;i<mat.length;i++) {
      for (var j=0;j<mat[i].length;j++) {
        if (mat[i][j] == -1) {
          s += mat[i][j].toString() + ' '; 
        }
        else {
          s += ' ' + mat[i][j].toString() + ' '; 
        }              
      }
      s += '\n';
    }
    console.log(s);
  }

  function print_2_matrix(mat1,mat2) {
    // assuming same size
    var s = '';
    var s2 = '';
    var v1,v2;
    for (var i=0;i<mat1.length;i++) {
      for (var j=0;j<mat1[i].length;j++) {
        v1 = mat1[i][j].toString();
        v2 = mat2[i][j].toString();
        if (v1.length == 0) { s += '** ';}
        else if (v1.length == 1) { s += ' ' + v1 + ' ';}
        else  { s += v1 + ' ';}

        if (v2.length == 0) { s2 += '** ';}
        else if (v2.length == 1) { s2 += ' ' + v2 + ' ';}
        else  { s2 += v2 + ' ';}
      }
      s += '\t\t' + s2 + '\n';
      s2 = '';
    }
    console.log(s);
  }
});
              
            
!
999px

Console