Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· openaiHugging Face Suzhen/CodeChat

Make me a simple web based 2048 game using html, css and javascript.

  1. User

    Make me a simple web based 2048 game using html, css and javascript.

  2. Assistant

    Sure! Here's an example of a simple web-based 2048 game using HTML, CSS, and JavaScript.

    HTML:

    <!DOCTYPE html>
    <html>
    <head>
      <title>2048 Game</title>
      <link rel="stylesheet" type="text/css" href="style.css">
    </head>
    <body>
      <div class="container">
        <h1>2048 Game</h1>
        <div id="grid-container"></div>
        <button onclick="restartGame()">Restart</button>
      </div>
    
      <script src="script.js"></script>
    </body>
    </html>
    

    CSS (style.css):

    .container {
      text-align: center;
    }
    
    #grid-container {
      width: 400px;
      height: 400px;
      border: 2px solid black;
      margin: 20px auto;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-template-rows: repeat(4, 1fr);
    }
    
    .grid-cell {
      border: 1px solid black;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 24px;
    }
    
    button {
      margin-top: 10px;
      font-size: 18px;
      padding: 10px 20px;
    }
    

    JavaScript (script.js):

    let grid;
    
    function initializeGrid() {
      grid = [
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 0, 0, 0]
      ];
    }
    
    function addNewNumber() {
      const emptyCells = [];
    
      for (let row = 0; row < 4; row++) {
        for (let col = 0; col < 4; col++) {
          if (grid[row][col] === 0) {
            emptyCells.push({ row, col });
          }
        }
      }
    
      if (emptyCells.length > 0) {
        const randomIndex = Math.floor(Math.random() * emptyCells.length);
        const randomCell = emptyCells[randomIndex];
        const value = Math.random() > 0.5 ? 2 : 4;
        grid[randomCell.row][randomCell.col] = value;
      }
    }
    
    function renderGrid() {
      const gridContainer = document.getElementById('grid-container');
      gridContainer.innerHTML = '';
    
      for (let row = 0; row < 4; row++) {
        for (let col = 0; col < 4; col++) {
          const cellValue = grid[row][col];
    
          const gridCell = document.createElement('div');
          gridCell.className = 'grid-cell';
          gridCell.innerText = cellValue === 0 ? '' : cellValue;
    
          gridContainer.appendChild(gridCell);
        }
      }
    }
    
    function restartGame() {
      initializeGrid();
      addNewNumber();
      addNewNumber();
      renderGrid();
    }
    
    window.addEventListener('keydown', (event) => {
      event.preventDefault();
      const key = event.key;
      
      if (key === 'ArrowUp') {
        moveUp();
      } else if (key === 'ArrowDown') {
        moveDown();
      } else if (key === 'ArrowLeft') {
        moveLeft();
      } else if (key === 'ArrowRight') {
        moveRight();
      }
    });
    
    function move(direction) {
      const flippedGrid = flipGrid(grid);
      const transposedGrid = transposeGrid(grid);
      let rotatedGrid = grid;
    
      if (direction === 'down') {
        rotatedGrid = flippedGrid;
      } else if (direction === 'left') {
        rotatedGrid = transposedGrid;
      } else if (direction === 'right') {
        flippedGrid = flipGrid(transposedGrid);
        rotatedGrid = flippedGrid;
      }
    
      mergeCells(rotatedGrid);
      moveCells(rotatedGrid);
    
      if (direction === 'down') {
        grid = flippedGrid;
      } else if (direction === 'left') {
        grid = transposeGrid(rotatedGrid);
      } else if (direction === 'right') {
        grid = flipGrid(transposeGrid(rotatedGrid));
      }
    
      addNewNumber();
      renderGrid();
    }
    
    function flipGrid(grid) {
      const flippedGrid = [];
    
      for (let row = 0; row < grid.length; row++) {
        flippedGrid.push([]);
    
        for (let col = 0; col < grid[row].length; col++) {
          flippedGrid[row][col] = grid[row][grid[row].length - 1 - col];
        }
      }
    
      return flippedGrid;
    }
    
    function transposeGrid(grid) {
      const transposedGrid = [];
    
      for (let col = 0; col < grid[0].length; col++) {
        transposedGrid.push([]);
    
        for (let row = 0; row < grid.length; row++) {
          transposedGrid[col][row] = grid[row][col];
        }
      }
    
      return transposedGrid;
    }
    
    function mergeCells(grid) {
      for (let row = 0; row < grid.length; row++) {
        for (let col = 0; col < grid[row].length - 1; col++) {
          if (grid[row][col] === grid[row][col + 1]) {
            grid[row][col] *= 2;
            grid[row][col + 1] = 0;
          }
        }
      }
    }
    
    function moveCells(grid) {
      for (let row = 0; row < grid.length; row++) {
        let newRow = [];
    
        for (let col = 0; col < grid[row].length; col++) {
          if (grid[row][col] !== 0) {
            newRow.push(grid[row][col]);
          }
        }
    
        while (newRow.length < 4) {
          newRow.unshift(0);
        }
    
        grid[row] = newRow;
      }
    }
    
    function moveUp() {
      move('up');
    }
    
    function moveDown() {
      move('down');
    }
    
    function moveLeft() {
      move('left');
    }
    
    function moveRight() {
      move('right');
    }
    
    // Start the game
    restartGame();
    

    Save all three files (index.html, style.css, and script.js) in the same directory and open the index.html file in your web browser. You should be able to play the 2048 game!