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

              
                 <!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>2048 GAME</title>
</head>

<body class="noselect">
    <script src="script1.js"></script>
</body>

</html>
              
            
!

CSS

              
                * {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: arial;
}

:root {
  --size: 10;
  --box-radius: 1vmin;
  --bg-color: #202020;
  --box-bg-color: #606060;
  --cell-bg-color: #404040;
  --box-side: 90vmin;
  --box-gap: calc(var(--box-side)/(var(--size)*16));
  --cell-side: calc((var(--box-side) - (var(--size) + 1) * var(--box-gap)) / var(--size));
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background-color: var(--bg-color);
}

#game-board {
  display: flex;
  flex-wrap: wrap;
  height: var(--box-side);
  width: var(--box-side);
  background-color: var(--box-bg-color);
  border-radius: var(--box-radius);
  padding: var(--box-gap);
  gap: var(--box-gap);
  position: relative;
}

.cell {
  width: var(--cell-side);
  height: var(--cell-side);
  background-color: var(--cell-bg-color);
  border-radius: var(--box-radius);
}

.tile {
  position: absolute;
  left: calc(var(--box-gap) + var(--x) * (var(--box-gap) + var(--cell-side)));
  top: calc(var(--box-gap) + var(--y) * (var(--box-gap) + var(--cell-side)));
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--cell-side);
  height: var(--cell-side);
  font-size: calc(var(--cell-side) / 3);
  font-weight: bolder;
  border-radius: var(--box-radius);
  /* background-color: hsl(42, 96%, var(--background-lightness)); */
  background-color: hsl(204, 70%, var(--background-lightness));
  color: hsl(200, 25%, var(--text-lightness));
  transition: 150ms ease-in-out;
  animation: show 200ms ease-in-out;
}

.hide {
  animation: show 200ms ease-in-out reverse forwards;
}

@keyframes show {
  0% {
    opacity: 0.5;
    transform: scale(0);
  }

  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.noselect {
  -webkit-touch-callout: none;
  /* iOS Safari */
  -webkit-user-select: none;
  /* Safari */
  -khtml-user-select: none;
  /* Konqueror HTML */
  -moz-user-select: none;
  /* Firefox */
  -ms-user-select: none;
  /* Internet Explorer/Edge */
  user-select: none;
  /* Non-prefixed version, currently
                                  supported by Chrome and Opera */
}
              
            
!

JS

              
                let game = null;
// let bestScore = 0;
// let score = 0;
let nextId = 1;
size = getComputedStyle(document.querySelector(":root")).getPropertyValue(
  "--size"
);
let gameBoard;

function createGameBoard() {
  gameBoard = document.createElement("div");
  gameBoard.id = "game-board";
  for (let i = 0; i < size * size; i++) {
    let cell = document.createElement("div");
    cell.className = "cell";
    gameBoard.appendChild(cell);
  }
  document.querySelector("body").appendChild(gameBoard);
}

function addRandomNumber() {
  let emptyCells = game
    .map((arrEle, index) => index)
    .filter((index) => game[index] === null); //Array of indexes of empty cells
  let randCellPos = emptyCells[Math.floor(Math.random() * emptyCells.length)];
  let newObj = {
    id: nextId++,
    index: randCellPos,
    value: generateRandomNumber(),
  };
  game[randCellPos] = newObj;
}

function generateRandomNumber() {
  return Math.random() <= 0.8 ? 2 : 4;
}

function updateDOM(before, after) {
  let newTiles = getNewTiles(before, after);
  let existingTiles = getExistingTiles(before, after);
  let mergedTiles = getMergedTiles(after);
  removeMergedTiles(mergedTiles);
  insertIntoDOM(newTiles, true);
  insertIntoDOM(existingTiles);
}

function getMergedTiles(after) {
  return after.filter((tile) => tile && tile.mergedIds);
}

function removeMergedTiles(mergedTiles) {
  for (let tile of mergedTiles) {
    for (let id of tile.mergedIds) {
      let divTile = document.getElementById(id);
      positionTile(tile, divTile);
      setTimeout(() => {
        divTile.remove();
      }, 150);
    }
  }
}

function insertIntoDOM(tiles, isNew) {
  for (let i = 0; i < tiles.length; i++) {
    let tile = tiles[i];
    if (tile) {
      if (isNew) {
        let tileDiv = document.createElement("div");
        positionTile(tile, tileDiv);
        tileDiv.classList.add("tile");
        tileDiv.id = tile.id;
        backgroundLightness = 100 - Math.log2(tile.value) * 9;
        tileDiv.style.setProperty(
          "--background-lightness",
          `${backgroundLightness}%`
        );
        tileDiv.style.setProperty(
          "--text-lightness",
          `${backgroundLightness <= 50 ? 90 : 10}%`
        );
        tileDiv.innerText = tile.value;
        gameBoard.appendChild(tileDiv);
      } else {
        let existingTile = document.getElementById(tile.id);
        positionTile(tile, existingTile);
      }
    }
  }
}

function positionTile(tile, elm) {
  let x = Math.floor(tile.index % size);
  let y = Math.floor(tile.index / size);
  elm.style.setProperty("--x", x);
  elm.style.setProperty("--y", y);
}

function getNewTiles(before, after) {
  let beforeIds = before.filter((tile) => tile).map((tile) => tile.id);
  let newTiles = after.filter((tile) => {
    return tile && beforeIds.indexOf(tile.id) === -1;
  });

  return newTiles;
}

function getExistingTiles(before, after) {
  let beforeIds = before.filter((tile) => tile).map((tile) => tile.id);
  let existingTiles = after.filter((tile) => {
    return tile && beforeIds.indexOf(tile.id) !== -1;
  });

  return existingTiles;
}

function startGame() {
  game = Array(size * size).fill(null);
  let previousGame = [...game];
  addRandomNumber();
  addRandomNumber();
  updateDOM(previousGame, game);
}

// startGame();

function getIndexForPoint(x, y) {
  return y * size + x;
}

function reflectGrid(grid) {
  let tempGrid = Array(size * size).fill(0);
  for (let row = 0; row < size; row++) {
    for (let col = 0; col < size; col++) {
      let index1 = getIndexForPoint(col, row);
      let index2 = getIndexForPoint(size - 1 - col, row);
      tempGrid[index2] = grid[index1];
    }
  }
  return tempGrid;
}

function rotateLeft90Deg(grid) {
  let tempGrid = Array(size * size).fill(0);
  for (let row = 0; row < size; row++) {
    for (let col = 0; col < size; col++) {
      let index1 = getIndexForPoint(col, row);
      let index2 = getIndexForPoint(row, size - 1 - col);
      tempGrid[index2] = grid[index1];
    }
  }
  return tempGrid;
}

function rotateRight90Deg(grid) {
  let tempGrid = Array(size * size).fill(0);
  for (let row = 0; row < size; row++) {
    for (let col = 0; col < size; col++) {
      let index1 = getIndexForPoint(col, row);
      let index2 = getIndexForPoint(size - 1 - row, col);
      tempGrid[index2] = grid[index1];
    }
  }
  return tempGrid;
}

function shiftGameUp(gameGrid) {
  let rotatedGame = rotateLeft90Deg(gameGrid);
  rotatedGame = shiftGameLeft(rotatedGame);
  return rotateRight90Deg(rotatedGame);
}

function shiftGameDown(gameGrid) {
  let rotatedGame = rotateRight90Deg(gameGrid);
  rotatedGame = shiftGameLeft(rotatedGame);
  return rotateLeft90Deg(rotatedGame);
}

function shiftGameRight(gameGrid) {
  let reflectedGame = reflectGrid(gameGrid);
  reflectedGame = shiftGameLeft(reflectedGame);
  return reflectGrid(reflectedGame);
}

function shiftGameLeft(gameGrid) {
  let newGameState = [];
  for (let i = 0; i < size; i++) {
    let startPos = size * i;
    let endPos = size * (i + 1);
    let row = gameGrid.slice(startPos, endPos);
    let filteredRow = row.filter((tile) => tile);
    for (let tile of filteredRow) {
      delete tile.mergedIds;
    }

    for (let j = 0; j < filteredRow.length - 1; j++) {
      if (filteredRow[j].value == filteredRow[j + 1].value) {
        let sum = filteredRow[j].value * 2;
        filteredRow[j] = {
          id: nextId++,
          mergedIds: [filteredRow[j].id, filteredRow[j + 1].id],
          value: sum,
        };
        filteredRow.splice(j + 1, 1);
      }
    }
    while (filteredRow.length < size) {
      filteredRow.push(null);
    }
    newGameState.push(...filteredRow);
  }
  return newGameState;
}

function handleKeypress(e) {
  let modifiers = e.altKey || e.ctrlKey || e.metaKey || e.shiftKey;
  let whichKey = e.which;

  let prevGame = [...game];
  if (!modifiers) {
    // e.preventDefault();
    switch (whichKey) {
      case 37:
      case 65:
        game = shiftGameLeft(game);
        break;
      case 38:
      case 87:
        game = shiftGameUp(game);
        break;
      case 39:
      case 68:
        game = shiftGameRight(game);
        break;
      case 40:
      case 83:
        game = shiftGameDown(game);
        break;
    }
    game = game.map((tile, index) => {
      return tile ? { ...tile, index: index } : null;
    });
    if (compareGameStates(prevGame, game)) return;
    addRandomNumber();
    updateDOM(prevGame, game);
  }
}

document.addEventListener("touchstart", handleTouchStart, false);
document.addEventListener("touchmove", handleTouchMove, false);

let xDown = null;
let yDown = null;

function handleTouchStart(evt) {
  xDown = evt.touches[0].clientX;
  yDown = evt.touches[0].clientY;
}

function handleTouchMove(evt) {
  const prevGame = [...game];
  if (!xDown || !yDown) {
    return;
  }
  const xUp = evt.touches[0].clientX;
  const yUp = evt.touches[0].clientY;

  const xDiff = xDown - xUp;
  const yDiff = yDown - yUp;

  if (Math.abs(xDiff) > Math.abs(yDiff)) {
    if (xDiff > 0) {
      game = shiftGameLeft(game);
    } else {
      game = shiftGameRight(game);
    }
  } else {
    if (yDiff > 0) {
      game = shiftGameUp(game);
    } else {
      game = shiftGameDown(game);
    }
  }
  game = game.map((tile, index) => {
    if (tile) {
      return {
        ...tile,
        index,
      };
    } else {
      return null;
    }
  });
  if (compareGameStates(prevGame, game)) return;
  addRandomNumber();
  updateDOM(prevGame, game);
  if (gameOver()) {
    setTimeout(() => {
      endDiv.classList.add("active");
    }, 800);
    return;
  }
  xDown = null;
  yDown = null;
}

function compareGameStates(before, after) {
  for (let i = 0; i < size * size; i++) {
    if (before[i] == null && after[i] == null) continue;
    if (before[i] && after[i]) {
      if (
        before[i].id == after[i].id &&
        before[i].index == after[i].index &&
        before[i].value == after[i].value
      ) {
        continue;
      }
    }
    return false;
  }
  return true;
}

//Event Listening Code

document.addEventListener("keydown", handleKeypress);

window.onload = function () {
  createGameBoard();
  startGame();
};

              
            
!
999px

Console