跳到正文

个人记录

florr 2048

_Missile_约 31 分钟
原文

这是 2026年7月15日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月11日
收录
2026年5月24日
更新
2026年11月14日
篇幅
约 31 分钟

版本

  1. v3当前
  2. v2
  3. v1
C++
<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes, maximum-scale=1.5">
  <title>florr 2048</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    body {
      background: #ffffff;
      font-family: 'Segoe UI', 'Arial', sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
      margin: 0;
      padding: 16px;
      position: relative;
    }
    .game-wrapper {
      width: 100%;
      max-width: 480px;
      display: flex;
      flex-direction: column;
      align-items: center;
      position: relative;
    }

    .achievement-container {
      position: fixed;
      top: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 90%;
      max-width: 460px;
      z-index: 1000;
      display: flex;
      flex-direction: column;
      align-items: center;
      pointer-events: none;
    }
    .achievement-toast {
      background: #7EEF6D;
      color: white;
      padding: 16px 28px;
      border-radius: 0 0 32px 32px;
      font-weight: 700;
      font-size: 1.2rem;
      letter-spacing: 0.5px;
      box-shadow: 0 12px 24px rgba(0,0,0,0.25);
      text-align: center;
      white-space: nowrap;
      transition: transform 0.4s cubic-bezier(0.22, 0.61, 0.36, 1);
      transform: translateY(-130px);
      margin-top: 4px;
      pointer-events: auto;
      backdrop-filter: blur(6px);
      border: 1px solid rgba(255,255,255,0.35);
    }
    .achievement-toast.show {
      transform: translateY(8px);
    }
    .achievement-toast.black-text {
      color: #1a1a1a;
    }

    .top-bar {
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      margin-bottom: 18px;
      gap: 12px;
    }
    .score-box {
      background: #f8f8f8;
      padding: 12px 26px;
      border-radius: 48px;
      font-weight: 700;
      font-size: 1.8rem;
      color: #2c3e50;
      box-shadow: 0 6px 14px rgba(0,0,0,0.04);
      letter-spacing: 0.3px;
    }
    .undo-btn {
      background: #34495e;
      color: #f1f1f1;
      border: none;
      border-radius: 48px;
      padding: 12px 24px;
      font-weight: 700;
      font-size: 1.1rem;
      cursor: pointer;
      transition: all 0.2s ease;
      box-shadow: 0 6px 14px rgba(0,0,0,0.08);
      display: flex;
      align-items: center;
      gap: 6px;
      letter-spacing: 0.6px;
      white-space: nowrap;
    }
    .undo-btn:hover {
      background: #1e2b38;
      transform: scale(1.03);
    }
    .undo-btn:active {
      transform: scale(0.96);
    }

    .grid-container {
      background: #eaeaea;
      border-radius: 32px;
      padding: 18px;
      box-shadow: 0 18px 35px rgba(0,0,0,0.08);
      width: 100%;
      aspect-ratio: 1 / 1;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      grid-template-rows: repeat(4, 1fr);
      gap: 14px;
      position: relative;
    }
    .tile {
      background: #cfcfcf;
      border-radius: 22px;
      display: flex;
      align-items: center;
      justify-content: center;
      font-weight: 800;
      font-size: clamp(0.65rem, 3.2vw, 1.2rem);
      color: #1e1e1e;
      transition: transform 0.15s ease, background 0.15s, color 0.15s;
      box-shadow: 0 4px 10px rgba(0,0,0,0.05);
      will-change: transform;
      user-select: none;
      aspect-ratio: 1 / 1;
      text-align: center;
      line-height: 1.1;
      word-break: break-word;
      padding: 3px;
      overflow-wrap: anywhere;
    }

    .game-over-message {
      margin-top: 24px;
      display: flex;
      flex-direction: column;
      align-items: center;
      gap: 16px;
    }
    .restart-btn {
      background: #4a4a4a;
      color: white;
      border: none;
      border-radius: 48px;
      padding: 14px 34px;
      font-weight: 700;
      font-size: 1.4rem;
      cursor: pointer;
      box-shadow: 0 10px 22px rgba(0,0,0,0.15);
      transition: 0.2s;
      letter-spacing: 1px;
      display: none;
      border: 1px solid rgba(255,255,255,0.2);
    }
    .restart-btn.show {
      display: inline-block;
    }
    .restart-btn:hover {
      background: #2b2b2b;
      transform: scale(1.03);
    }

    @media (max-width: 450px) {
      .tile {
        font-size: clamp(0.55rem, 3.5vw, 0.9rem);
        border-radius: 18px;
      }
      .grid-container {
        gap: 10px;
        padding: 14px;
      }
      .achievement-toast {
        font-size: 1rem;
        padding: 12px 20px;
      }
    }
  </style>
</head>
<body>
<div class="game-wrapper">
  <div class="top-bar">
    <div class="score-box" id="scoreDisplay">0</div>
    <button class="undo-btn" id="undoButton">↩ 退一步</button>
  </div>

  <div class="grid-container" id="gridContainer"></div>

  <div class="game-over-message">
    <button class="restart-btn" id="restartButton">再来一次</button>
  </div>
</div>

<div class="achievement-container" id="achievementContainer"></div>

<script>
  (function() {
    const LEVELS = [
      { name: "Common",     value: 1,   color: "#7EEF6D", textColor: "white" },
      { name: "Unusual",    value: 2,   color: "#FFE65D", textColor: "white" },
      { name: "Rare",       value: 4,   color: "#4D52E3", textColor: "white" },
      { name: "Epic",       value: 8,   color: "#861FDE", textColor: "white" },
      { name: "Legendary",  value: 16,  color: "#DE1F1F", textColor: "white" },
      { name: "Mythic",     value: 32,  color: "#1FDBDE", textColor: "white" },
      { name: "Ultra",      value: 64,  color: "#FF2B75", textColor: "white" },
      { name: "Super",      value: 128, color: "#2BFFA3", textColor: "black" },
      { name: "Eternal",    value: 256, color: "#F1F1F1", textColor: "black" },
      { name: "Unique",     value: 512, color: "#666666", textColor: "white" }
    ];

    const LEVEL_MAP = new Map();
    LEVELS.forEach(lvl => LEVEL_MAP.set(lvl.value, { ...lvl }));

    let grid = [];
    let score = 0;
    let gameOver = false;
    let uniqueReached = false;
    
    let history = [];
    const MAX_HISTORY = 1500;

    let achievementUltraShown = false;
    let achievementSuperShown = false;

    const gridContainer = document.getElementById('gridContainer');
    const scoreDisplay = document.getElementById('scoreDisplay');
    const undoButton = document.getElementById('undoButton');
    const restartButton = document.getElementById('restartButton');
    const achievementContainer = document.getElementById('achievementContainer');

    function getLevelByValue(val) {
      return LEVEL_MAP.get(val) || null;
    }

    function updateScoreUI() {
      scoreDisplay.textContent = score;
    }

    function pushHistory() {
      if (gameOver || uniqueReached) return;
      const copy = grid.map(row => [...row]);
      history.push({ grid: copy, score: score });
      if (history.length > MAX_HISTORY) history.shift();
    }

    function undoMove() {
      if (uniqueReached) {
        alert('Unique已合成,无法悔棋。');
        return;
      }
      if (history.length === 0) {
        alert('没有更早的步骤了');
        return;
      }
      const last = history.pop();
      grid = last.grid.map(row => [...row]);
      score = last.score;
      gameOver = false;
      uniqueReached = false;
      restartButton.classList.remove('show');
      updateScoreUI();
      renderGrid();
    }

    function resetGame() {
      grid = [
        [0,0,0,0],
        [0,0,0,0],
        [0,0,0,0],
        [0,0,0,0]
      ];
      score = 0;
      gameOver = false;
      uniqueReached = false;
      history = [];
      achievementUltraShown = false;
      achievementSuperShown = false;
      achievementContainer.innerHTML = '';
      addRandomTile();
      addRandomTile();
      updateScoreUI();
      restartButton.classList.remove('show');
      renderGrid();
    }

    function addRandomTile() {
      const emptyCells = [];
      for (let r = 0; r < 4; r++) {
        for (let c = 0; c < 4; c++) {
          if (grid[r][c] === 0) emptyCells.push({r, c});
        }
      }
      if (emptyCells.length === 0) return false;
      const {r, c} = emptyCells[Math.floor(Math.random() * emptyCells.length)];
      grid[r][c] = Math.random() < 0.9 ? 1 : 2;
      return true;
    }

    function checkGameOver() {
      for (let r = 0; r < 4; r++) {
        for (let c = 0; c < 4; c++) {
          if (grid[r][c] === 0) return false;
        }
      }
      for (let r = 0; r < 4; r++) {
        for (let c = 0; c < 4; c++) {
          const val = grid[r][c];
          if (c < 3 && grid[r][c+1] === val) return false;
          if (r < 3 && grid[r+1][c] === val) return false;
        }
      }
      return true;
    }

    function handleMergeAchievements(mergedValue) {
      const levelInfo = getLevelByValue(mergedValue);
      if (!levelInfo) return;

      if (mergedValue === 64 && !achievementUltraShown) {
        achievementUltraShown = true;
        showAchievementToast("合成师:合成第一个Ultra!", "#FF2B75", "white");
      }
      if (mergedValue === 128 && !achievementSuperShown) {
        achievementSuperShown = true;
        showAchievementToast("合成高手:合成Super!", "#2BFFA3", "black");
      }
      if (mergedValue === 256) {
        showAchievementToast("永恒的光芒!", "#F1F1F1", "black");
      }
      if (mergedValue === 512) {
        uniqueReached = true;
        gameOver = true;
        showAchievementToast("Unique!", "#666666", "white");
        restartButton.classList.add('show');
        renderGrid();
      }
    }

    function rotateClockwise(g, times) {
      let gridCopy = g.map(row => [...row]);
      for (let t = 0; t < times; t++) {
        const newG = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]];
        for (let r = 0; r < 4; r++) {
          for (let c = 0; c < 4; c++) {
            newG[c][3-r] = gridCopy[r][c];
          }
        }
        gridCopy = newG;
      }
      return gridCopy;
    }

    function move(direction) {
      if (gameOver || uniqueReached) return false;
      
      pushHistory();
      
      const oldGrid = grid.map(row => [...row]);
      
      let workGrid = [];
      let rotateBack = 0;

      if (direction === 'left') {
        workGrid = grid.map(row => [...row]);
        rotateBack = 0;
      } else if (direction === 'right') {
        workGrid = rotateClockwise(grid, 2);
        rotateBack = 2;
      } else if (direction === 'up') {
        workGrid = rotateClockwise(grid, 3);
        rotateBack = 1;
      } else if (direction === 'down') {
        workGrid = rotateClockwise(grid, 1);
        rotateBack = 3;
      }

      for (let r = 0; r < 4; r++) {
        let row = workGrid[r].filter(v => v !== 0);
        let newRow = [];
        let skip = false;
        for (let i = 0; i < row.length; i++) {
          if (skip) {
            skip = false;
            continue;
          }
          if (i < row.length - 1 && row[i] === row[i+1]) {
            const mergedVal = row[i] * 2;
            newRow.push(mergedVal);
            score += mergedVal;
            handleMergeAchievements(mergedVal);
            skip = true;
          } else {
            newRow.push(row[i]);
          }
        }
        while (newRow.length < 4) newRow.push(0);
        workGrid[r] = newRow;
      }

      let finalGrid = workGrid;
      if (rotateBack === 1) finalGrid = rotateClockwise(workGrid, 1);
      else if (rotateBack === 2) finalGrid = rotateClockwise(workGrid, 2);
      else if (rotateBack === 3) finalGrid = rotateClockwise(workGrid, 3);

      const isSame = oldGrid.every((row, r) => row.every((val, c) => val === finalGrid[r][c]));
      if (isSame) {
        history.pop();
        return false;
      }

      grid = finalGrid;
      addRandomTile();

      if (!uniqueReached && checkGameOver()) {
        gameOver = true;
        restartButton.classList.add('show');
      }

      updateScoreUI();
      renderGrid();
      return true;
    }

    function showAchievementToast(text, bgColor, textColorClass) {
      const toast = document.createElement('div');
      toast.className = 'achievement-toast';
      toast.textContent = text;
      toast.style.backgroundColor = bgColor;
      if (textColorClass === 'black') {
        toast.classList.add('black-text');
      } else {
        toast.style.color = 'white';
      }
      achievementContainer.appendChild(toast);
      
      requestAnimationFrame(() => {
        toast.classList.add('show');
      });
      
      setTimeout(() => {
        toast.classList.remove('show');
        setTimeout(() => {
          if (toast.parentNode) toast.parentNode.removeChild(toast);
        }, 400);
      }, 5000);
    }

    function renderGrid() {
      gridContainer.innerHTML = '';
      for (let r = 0; r < 4; r++) {
        for (let c = 0; c < 4; c++) {
          const value = grid[r][c];
          const tile = document.createElement('div');
          tile.className = 'tile';
          const level = getLevelByValue(value);
          if (value !== 0 && level) {
            tile.textContent = level.name;
            tile.style.backgroundColor = level.color;
            tile.style.color = level.textColor === 'black' ? '#1a1a1a' : 'white';
            if (['#F1F1F1', '#FFE65D', '#2BFFA3'].includes(level.color)) {
              tile.style.textShadow = '0 1px 2px rgba(0,0,0,0.25)';
            } else {
              tile.style.textShadow = '0 1px 3px rgba(0,0,0,0.5)';
            }
          } else {
            tile.textContent = '';
            tile.style.backgroundColor = '#cfcfcf';
            tile.style.color = '#1e1e1e';
            tile.style.textShadow = 'none';
          }
          gridContainer.appendChild(tile);
        }
      }
      
      if (gameOver && !uniqueReached) {
        restartButton.classList.add('show');
      }
    }

    function handleKey(e) {
      const key = e.key;
      e.preventDefault();
      if (gameOver && uniqueReached) return;
      
      if (key === 'ArrowUp' || key === 'w' || key === 'W') {
        move('up');
      } else if (key === 'ArrowDown' || key === 's' || key === 'S') {
        move('down');
      } else if (key === 'ArrowLeft' || key === 'a' || key === 'A') {
        move('left');
      } else if (key === 'ArrowRight' || key === 'd' || key === 'D') {
        move('right');
      }
    }

    window.addEventListener('keydown', (e) => {
      const controlKeys = ['ArrowUp','ArrowDown','ArrowLeft','ArrowRight','w','W','s','S','a','A','d','D'];
      if (controlKeys.includes(e.key)) {
        e.preventDefault();
        handleKey(e);
      }
    });

    undoButton.addEventListener('click', undoMove);
    restartButton.addEventListener('click', resetGame);

    let touchStartX = 0, touchStartY = 0;
    gridContainer.addEventListener('touchstart', (e) => {
      const touch = e.touches[0];
      touchStartX = touch.clientX;
      touchStartY = touch.clientY;
    });
    gridContainer.addEventListener('touchend', (e) => {
      if (gameOver && uniqueReached) return;
      if (!touchStartX || !touchStartY) return;
      const touchEnd = e.changedTouches[0];
      const dx = touchEnd.clientX - touchStartX;
      const dy = touchEnd.clientY - touchStartY;
      const absDx = Math.abs(dx);
      const absDy = Math.abs(dy);
      if (Math.max(absDx, absDy) < 20) return;
      
      if (absDx > absDy) {
        if (dx > 0) move('right');
        else move('left');
      } else {
        if (dy > 0) move('down');
        else move('up');
      }
      touchStartX = 0; touchStartY = 0;
    });

    resetGame();
  })();
</script>
</body>
</html>