882 lines
27 KiB
HTML
882 lines
27 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr" data-theme="dark">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
|
|
<title>2048 - AP Manageur Arcade</title>
|
|
<style>
|
|
* { box-sizing: border-box; margin: 0; padding: 0; user-select: none; -webkit-user-select: none; }
|
|
|
|
:root, html[data-theme="dark"] {
|
|
--bg: #071326;
|
|
--card-bg: #0d1d36;
|
|
--panel-box: #132746;
|
|
--border: #223c68;
|
|
--text: #eef4ff;
|
|
--muted: #9aa8c5;
|
|
--blue: #5ca7ff;
|
|
--blue-btn-text: #071326;
|
|
--grid-bg: #0b182d;
|
|
--empty-cell: #13243f;
|
|
}
|
|
html[data-theme="light"] {
|
|
--bg: #f2f5fa;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #e9eef7;
|
|
--border: #d2dcec;
|
|
--text: #15203a;
|
|
--muted: #5b6b8c;
|
|
--blue: #1e6ef0;
|
|
--blue-btn-text: #ffffff;
|
|
--grid-bg: #bbada0;
|
|
--empty-cell: rgba(238, 228, 218, 0.35);
|
|
}
|
|
html[data-theme="winxp"] {
|
|
--bg: #ece9d8;
|
|
--card-bg: #ece9d8;
|
|
--panel-box: #c0c0c0;
|
|
--border: #7f9db9;
|
|
--text: #000000;
|
|
--muted: #444444;
|
|
--blue: #0055ea;
|
|
--blue-btn-text: #ffffff;
|
|
--grid-bg: #808080;
|
|
--empty-cell: #c0c0c0;
|
|
font-family: 'Tahoma', sans-serif !important;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="light"],
|
|
html[data-theme="macos"]:not([data-macos-mode="dark"]) {
|
|
--bg: #f5f5f7;
|
|
--card-bg: #ffffff;
|
|
--panel-box: #f2f2f7;
|
|
--border: rgba(0, 0, 0, 0.12);
|
|
--text: #1d1d1f;
|
|
--muted: #86868b;
|
|
--blue: #0071e3;
|
|
--blue-btn-text: #ffffff;
|
|
--grid-bg: #d1d1d6;
|
|
--empty-cell: #e5e5ea;
|
|
}
|
|
html[data-theme="macos"][data-macos-mode="dark"] {
|
|
--bg: #1e1e1e;
|
|
--card-bg: #2d2d2d;
|
|
--panel-box: #3a3a3c;
|
|
--border: rgba(255, 255, 255, 0.15);
|
|
--text: #f5f5f7;
|
|
--muted: #98989d;
|
|
--blue: #0a84ff;
|
|
--blue-btn-text: #ffffff;
|
|
--grid-bg: #2c2c2e;
|
|
--empty-cell: #3a3a3c;
|
|
}
|
|
html[data-theme="futuristic"] {
|
|
--bg: #050811;
|
|
--card-bg: #091124;
|
|
--panel-box: #0f1c3a;
|
|
--border: #00f0ff;
|
|
--text: #e6f6ff;
|
|
--muted: #64b5f6;
|
|
--blue: #00f0ff;
|
|
--blue-btn-text: #050811;
|
|
--grid-bg: #08142b;
|
|
--empty-cell: #0f2249;
|
|
}
|
|
html[data-theme="hacker"] {
|
|
--bg: #000000;
|
|
--card-bg: #051206;
|
|
--panel-box: #0a1f0c;
|
|
--border: #00ff41;
|
|
--text: #00ff41;
|
|
--muted: #008f11;
|
|
--blue: #00ff41;
|
|
--blue-btn-text: #000000;
|
|
--grid-bg: #041407;
|
|
--empty-cell: #0a240e;
|
|
font-family: 'Courier New', monospace !important;
|
|
}
|
|
|
|
body {
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 100vh;
|
|
overflow-x: hidden;
|
|
padding: 4px;
|
|
transition: background 0.3s ease, color 0.3s ease;
|
|
}
|
|
|
|
.game-card {
|
|
background: var(--card-bg);
|
|
border: 1px solid var(--border);
|
|
border-radius: 14px;
|
|
padding: 10px 14px;
|
|
box-shadow: 0 10px 30px rgba(0,0,0,0.4), 0 0 20px rgba(92,167,255,0.1);
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
width: 100%;
|
|
max-width: 400px;
|
|
position: relative;
|
|
}
|
|
|
|
.header-bar {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
width: 100%;
|
|
margin-bottom: 8px;
|
|
}
|
|
|
|
.title-area h1 {
|
|
font-size: 38px;
|
|
font-weight: 900;
|
|
line-height: 1;
|
|
color: var(--blue);
|
|
letter-spacing: -1px;
|
|
}
|
|
.title-area p {
|
|
font-size: 11.5px;
|
|
color: var(--muted);
|
|
margin-top: 2px;
|
|
}
|
|
|
|
.scores-row {
|
|
display: flex;
|
|
gap: 8px;
|
|
}
|
|
|
|
.score-badge {
|
|
background: var(--panel-box);
|
|
border: 1px solid var(--border);
|
|
border-radius: 8px;
|
|
padding: 6px 12px;
|
|
min-width: 72px;
|
|
text-align: center;
|
|
}
|
|
.score-badge .score-label {
|
|
font-size: 9.5px;
|
|
font-weight: 700;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.5px;
|
|
color: var(--muted);
|
|
}
|
|
.score-badge .score-val {
|
|
font-size: 17px;
|
|
font-weight: 800;
|
|
color: var(--text);
|
|
font-family: 'Courier New', Courier, monospace;
|
|
}
|
|
|
|
.action-bar {
|
|
display: flex;
|
|
justify-content: space-between;
|
|
align-items: center;
|
|
width: 100%;
|
|
margin-bottom: 14px;
|
|
gap: 8px;
|
|
}
|
|
|
|
.btn {
|
|
background: var(--blue);
|
|
color: var(--blue-btn-text);
|
|
border: 0;
|
|
border-radius: 6px;
|
|
padding: 7px 14px;
|
|
font-weight: 700;
|
|
font-size: 12px;
|
|
cursor: pointer;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 5px;
|
|
transition: all .15s ease;
|
|
box-shadow: 0 2px 6px rgba(0,0,0,0.15);
|
|
}
|
|
.btn:hover { opacity: 0.9; transform: translateY(-1px); }
|
|
.btn.secondary {
|
|
background: var(--panel-box);
|
|
color: var(--text);
|
|
border: 1px solid var(--border);
|
|
box-shadow: none;
|
|
}
|
|
|
|
/* 2048 Grid Board */
|
|
.grid-container {
|
|
position: relative;
|
|
background: var(--grid-bg);
|
|
border-radius: 10px;
|
|
width: 330px;
|
|
height: 330px;
|
|
padding: 10px;
|
|
display: grid;
|
|
grid-template-columns: repeat(4, 1fr);
|
|
grid-template-rows: repeat(4, 1fr);
|
|
gap: 10px;
|
|
touch-action: none;
|
|
box-shadow: inset 0 0 10px rgba(0,0,0,0.3);
|
|
}
|
|
|
|
.grid-cell {
|
|
background: var(--empty-cell);
|
|
border-radius: 6px;
|
|
width: 100%;
|
|
height: 100%;
|
|
}
|
|
|
|
.tile-layer {
|
|
position: absolute;
|
|
top: 10px;
|
|
left: 10px;
|
|
width: 310px;
|
|
height: 310px;
|
|
pointer-events: none;
|
|
}
|
|
|
|
.tile {
|
|
position: absolute;
|
|
width: 70px;
|
|
height: 70px;
|
|
border-radius: 6px;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
font-weight: 800;
|
|
font-size: 26px;
|
|
transition: transform 0.11s ease-in-out, opacity 0.11s ease-in-out;
|
|
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
|
|
}
|
|
|
|
/* Tile Numbers & Themes Colors */
|
|
.tile-2 { background: #eee4da; color: #776e65; }
|
|
.tile-4 { background: #ede0c8; color: #776e65; }
|
|
.tile-8 { background: #f2b179; color: #f9f6f2; }
|
|
.tile-16 { background: #f59563; color: #f9f6f2; }
|
|
.tile-32 { background: #f67c5f; color: #f9f6f2; }
|
|
.tile-64 { background: #f65e3b; color: #f9f6f2; }
|
|
.tile-128 { background: #edcf72; color: #f9f6f2; font-size: 22px; box-shadow: 0 0 10px rgba(237, 207, 114, 0.4); }
|
|
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 22px; box-shadow: 0 0 12px rgba(237, 204, 97, 0.5); }
|
|
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 22px; box-shadow: 0 0 14px rgba(237, 200, 80, 0.6); }
|
|
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 19px; box-shadow: 0 0 16px rgba(237, 197, 63, 0.7); }
|
|
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 19px; box-shadow: 0 0 20px rgba(237, 194, 46, 0.85); animation: pulse2048 1.5s infinite; }
|
|
.tile-super { background: #3c3a32; color: #f9f6f2; font-size: 18px; box-shadow: 0 0 22px rgba(92,167,255,0.8); }
|
|
|
|
@keyframes pulse2048 {
|
|
0% { transform: scale(1); }
|
|
50% { transform: scale(1.05); }
|
|
100% { transform: scale(1); }
|
|
}
|
|
|
|
.tile-new {
|
|
animation: appear 0.15s ease-in;
|
|
}
|
|
@keyframes appear {
|
|
0% { opacity: 0; transform: scale(0); }
|
|
100% { opacity: 1; transform: scale(1); }
|
|
}
|
|
|
|
.tile-merged {
|
|
animation: pop 0.18s ease-in-out;
|
|
}
|
|
@keyframes pop {
|
|
0% { transform: scale(0.9); }
|
|
50% { transform: scale(1.15); }
|
|
100% { transform: scale(1); }
|
|
}
|
|
|
|
/* Overlays */
|
|
.overlay {
|
|
position: absolute;
|
|
top: 0; left: 0; right: 0; bottom: 0;
|
|
background: rgba(7, 19, 38, 0.88);
|
|
backdrop-filter: blur(4px);
|
|
border-radius: 10px;
|
|
display: flex;
|
|
flex-direction: column;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 12px;
|
|
z-index: 50;
|
|
animation: fadeIn 0.3s ease;
|
|
}
|
|
@keyframes fadeIn {
|
|
from { opacity: 0; }
|
|
to { opacity: 1; }
|
|
}
|
|
.overlay h2 {
|
|
font-size: 26px;
|
|
font-weight: 900;
|
|
color: var(--blue);
|
|
}
|
|
.overlay p {
|
|
font-size: 13px;
|
|
color: var(--text);
|
|
}
|
|
|
|
.instructions {
|
|
font-size: 11px;
|
|
color: var(--muted);
|
|
line-height: 1.4;
|
|
text-align: center;
|
|
margin-top: 12px;
|
|
}
|
|
.instructions kbd {
|
|
background: var(--panel-box);
|
|
border: 1px solid var(--border);
|
|
border-radius: 3px;
|
|
padding: 1px 4px;
|
|
color: var(--text);
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="game-card">
|
|
<div class="header-bar">
|
|
<div class="title-area">
|
|
<h1>2048</h1>
|
|
<p id="lblSubtitle">Fusionnez les tuiles pour atteindre 2048 !</p>
|
|
</div>
|
|
<div class="scores-row">
|
|
<div class="score-badge">
|
|
<div id="lblScore" class="score-label">Score</div>
|
|
<div id="valScore" class="score-val">0</div>
|
|
</div>
|
|
<div class="score-badge">
|
|
<div id="lblBest" class="score-label">Meilleur</div>
|
|
<div id="valBest" class="score-val">0</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="action-bar">
|
|
<div style="display:flex;gap:6px">
|
|
<button id="btnNew" class="btn" onclick="startNewGame()">▶ <span id="lblNew">Nouveau</span></button>
|
|
<button id="btnUndo" class="btn secondary" onclick="undoMove()">↩ <span id="lblUndo">Annuler</span></button>
|
|
</div>
|
|
<button id="btnSound" class="btn secondary" onclick="toggleSound()">🎵 <span id="lblSound">Son : ON</span></button>
|
|
</div>
|
|
|
|
<div id="gridContainer" class="grid-container">
|
|
<!-- 16 static grid cells -->
|
|
<div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div>
|
|
<div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div>
|
|
<div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div>
|
|
<div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div><div class="grid-cell"></div>
|
|
|
|
<!-- Moving tile layer -->
|
|
<div id="tileLayer" class="tile-layer"></div>
|
|
|
|
<!-- Win / Game Over Overlay -->
|
|
<div id="gameOverlay" class="overlay" style="display:none">
|
|
<h2 id="ovTitle">Game Over !</h2>
|
|
<p id="ovDesc">Plus aucun mouvement possible.</p>
|
|
<div style="display:flex;gap:8px;margin-top:6px">
|
|
<button id="btnOvKeep" class="btn secondary" onclick="keepPlaying()" style="display:none">Continuer</button>
|
|
<button id="btnOvReplay" class="btn" onclick="startNewGame()">Rejouer</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div id="lblInstructions" class="instructions">
|
|
Utilisez les touches <kbd>⬆️</kbd> <kbd>⬇️</kbd> <kbd>⬅️</kbd> <kbd>➡️</kbd> ou glissez pour déplacer les tuiles. Deux tuiles identiques fusionnent en une seule !
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
// Theme Management
|
|
function setTheme(theme, macosMode) {
|
|
if (!theme) theme = 'dark';
|
|
document.documentElement.dataset.theme = theme;
|
|
if (theme === 'macos' && macosMode) {
|
|
document.documentElement.dataset.macosMode = macosMode;
|
|
} else {
|
|
delete document.documentElement.dataset.macosMode;
|
|
}
|
|
}
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
setTheme(urlParams.get('theme') || 'dark', urlParams.get('macosMode'));
|
|
|
|
// Game State Variables (Declared early to prevent TDZ errors)
|
|
const SIZE = 4;
|
|
let grid = [];
|
|
let previousState = null;
|
|
let score = 0;
|
|
let bestScore = parseInt(localStorage.getItem('ap_2048_hi') || '0', 10);
|
|
let isWon = false;
|
|
let keepPlayingAfterWin = false;
|
|
let isOver = false;
|
|
let isSoundOn = true;
|
|
|
|
// i18n
|
|
const I18N_2048 = {
|
|
fr: {
|
|
subtitle: "Fusionnez les tuiles pour atteindre 2048 !",
|
|
score: "Score",
|
|
best: "Meilleur",
|
|
new_game: "Nouveau",
|
|
undo: "Annuler",
|
|
sound_on: "Son : ON",
|
|
sound_off: "Son : OFF",
|
|
win_title: "🎉 Victoire !",
|
|
win_desc: "Vous avez atteint la tuile 2048 !",
|
|
keep_going: "Continuer",
|
|
replay: "Rejouer",
|
|
over_title: "Partie terminée !",
|
|
over_desc: "Plus aucun mouvement possible.",
|
|
instructions: "Utilisez les touches <kbd>⬆️</kbd> <kbd>⬇️</kbd> <kbd>⬅️</kbd> <kbd>➡️</kbd> ou glissez pour déplacer les tuiles. Deux tuiles identiques fusionnent en une seule !"
|
|
},
|
|
en: {
|
|
subtitle: "Join the tiles to reach 2048!",
|
|
score: "Score",
|
|
best: "Best",
|
|
new_game: "New",
|
|
undo: "Undo",
|
|
sound_on: "Sound: ON",
|
|
sound_off: "Sound: OFF",
|
|
win_title: "🎉 You Win!",
|
|
win_desc: "You reached the 2048 tile!",
|
|
keep_going: "Keep going",
|
|
replay: "Play again",
|
|
over_title: "Game Over!",
|
|
over_desc: "No more moves available.",
|
|
instructions: "Use arrow keys <kbd>⬆️</kbd> <kbd>⬇️</kbd> <kbd>⬅️</kbd> <kbd>➡️</kbd> or swipe to move tiles. Identical tiles merge into one!"
|
|
}
|
|
};
|
|
|
|
let currentLang = 'fr';
|
|
function apply2048Lang(lang) {
|
|
currentLang = (lang && (lang.startsWith('en') ? 'en' : 'fr')) || 'fr';
|
|
const dict = I18N_2048[currentLang] || I18N_2048.fr;
|
|
|
|
const sub = document.getElementById('lblSubtitle');
|
|
const sc = document.getElementById('lblScore');
|
|
const be = document.getElementById('lblBest');
|
|
const ng = document.getElementById('lblNew');
|
|
const un = document.getElementById('lblUndo');
|
|
const so = document.getElementById('lblSound');
|
|
const ins = document.getElementById('lblInstructions');
|
|
|
|
if (sub) sub.textContent = dict.subtitle;
|
|
if (sc) sc.textContent = dict.score;
|
|
if (be) be.textContent = dict.best;
|
|
if (ng) ng.textContent = dict.new_game;
|
|
if (un) un.textContent = dict.undo;
|
|
if (so) so.textContent = isSoundOn ? dict.sound_on : dict.sound_off;
|
|
if (ins) ins.innerHTML = dict.instructions;
|
|
}
|
|
apply2048Lang(urlParams.get('lang'));
|
|
|
|
window.addEventListener('message', e => {
|
|
if (e.data && e.data.type === 'themeChange') setTheme(e.data.theme, e.data.macosMode);
|
|
if (e.data && e.data.type === 'langChange') apply2048Lang(e.data.lang);
|
|
});
|
|
|
|
// Sound Synthesizer via Web Audio API
|
|
let audioCtx = null;
|
|
function playSound(type, val = 2) {
|
|
if (!isSoundOn) return;
|
|
try {
|
|
if (!audioCtx) audioCtx = new (window.AudioContext || window.webkitAudioContext)();
|
|
if (audioCtx.state === 'suspended') audioCtx.resume();
|
|
const now = audioCtx.currentTime;
|
|
|
|
if (type === 'slide') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(140, now);
|
|
osc.frequency.linearRampToValueAtTime(190, now + 0.04);
|
|
g.gain.setValueAtTime(0.025, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.04);
|
|
osc.start(now); osc.stop(now + 0.04);
|
|
} else if (type === 'merge') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'sine';
|
|
const baseFreq = 260 + Math.min(600, Math.log2(val) * 45);
|
|
osc.frequency.setValueAtTime(baseFreq, now);
|
|
osc.frequency.exponentialRampToValueAtTime(baseFreq * 1.5, now + 0.08);
|
|
g.gain.setValueAtTime(0.05, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.09);
|
|
osc.start(now); osc.stop(now + 0.09);
|
|
} else if (type === 'win') {
|
|
[440, 554.37, 659.25, 880].forEach((freq, idx) => {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'triangle';
|
|
osc.frequency.setValueAtTime(freq, now + idx * 0.1);
|
|
g.gain.setValueAtTime(0.08, now + idx * 0.1);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + idx * 0.1 + 0.28);
|
|
osc.start(now + idx * 0.1); osc.stop(now + idx * 0.1 + 0.28);
|
|
});
|
|
} else if (type === 'over') {
|
|
const osc = audioCtx.createOscillator();
|
|
const g = audioCtx.createGain();
|
|
osc.connect(g); g.connect(audioCtx.destination);
|
|
osc.type = 'sawtooth';
|
|
osc.frequency.setValueAtTime(180, now);
|
|
osc.frequency.linearRampToValueAtTime(80, now + 0.3);
|
|
g.gain.setValueAtTime(0.08, now);
|
|
g.gain.exponentialRampToValueAtTime(0.001, now + 0.3);
|
|
osc.start(now); osc.stop(now + 0.3);
|
|
}
|
|
} catch (e) {}
|
|
}
|
|
|
|
function toggleSound() {
|
|
isSoundOn = !isSoundOn;
|
|
const dict = I18N_2048[currentLang] || I18N_2048.fr;
|
|
const lbl = document.getElementById('lblSound');
|
|
if (lbl) lbl.textContent = isSoundOn ? dict.sound_on : dict.sound_off;
|
|
}
|
|
|
|
// Grid Initialization & Rendering
|
|
function emptyGrid() {
|
|
const g = [];
|
|
for (let r = 0; r < SIZE; r++) {
|
|
g.push(new Array(SIZE).fill(0));
|
|
}
|
|
return g;
|
|
}
|
|
|
|
function saveState() {
|
|
previousState = {
|
|
grid: grid.map(r => [...r]),
|
|
score: score,
|
|
isWon: isWon,
|
|
isOver: isOver
|
|
};
|
|
const btnUndo = document.getElementById('btnUndo');
|
|
if (btnUndo) btnUndo.disabled = false;
|
|
}
|
|
|
|
function undoMove() {
|
|
if (!previousState) return;
|
|
grid = previousState.grid.map(r => [...r]);
|
|
score = previousState.score;
|
|
isWon = previousState.isWon;
|
|
isOver = previousState.isOver;
|
|
previousState = null;
|
|
const btnUndo = document.getElementById('btnUndo');
|
|
if (btnUndo) btnUndo.disabled = true;
|
|
|
|
document.getElementById('gameOverlay').style.display = 'none';
|
|
updateScoresUI();
|
|
renderTiles();
|
|
playSound('slide');
|
|
}
|
|
|
|
function getEmptyCells() {
|
|
const cells = [];
|
|
for (let r = 0; r < SIZE; r++) {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
if (grid[r][c] === 0) cells.push({ r, c });
|
|
}
|
|
}
|
|
return cells;
|
|
}
|
|
|
|
function spawnTile() {
|
|
const empty = getEmptyCells();
|
|
if (empty.length === 0) return null;
|
|
const spot = empty[Math.floor(Math.random() * empty.length)];
|
|
const val = Math.random() < 0.9 ? 2 : 4;
|
|
grid[spot.r][spot.c] = val;
|
|
return { r: spot.r, c: spot.c, val };
|
|
}
|
|
|
|
function updateScoresUI() {
|
|
document.getElementById('valScore').textContent = score;
|
|
if (score > bestScore) {
|
|
bestScore = score;
|
|
localStorage.setItem('ap_2048_hi', bestScore);
|
|
}
|
|
document.getElementById('valBest').textContent = bestScore;
|
|
}
|
|
|
|
function renderTiles(newTilePos = null, mergedCoords = []) {
|
|
const layer = document.getElementById('tileLayer');
|
|
layer.innerHTML = '';
|
|
|
|
const CELL_SIZE = 70;
|
|
const GAP = 10;
|
|
|
|
for (let r = 0; r < SIZE; r++) {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
const val = grid[r][c];
|
|
if (val === 0) continue;
|
|
|
|
const tile = document.createElement('div');
|
|
const tileClass = val <= 2048 ? `tile-${val}` : 'tile-super';
|
|
tile.className = `tile ${tileClass}`;
|
|
tile.textContent = val;
|
|
|
|
const top = r * (CELL_SIZE + GAP);
|
|
const left = c * (CELL_SIZE + GAP);
|
|
tile.style.top = top + 'px';
|
|
tile.style.left = left + 'px';
|
|
|
|
if (newTilePos && newTilePos.r === r && newTilePos.c === c) {
|
|
tile.classList.add('tile-new');
|
|
}
|
|
if (mergedCoords.some(m => m.r === r && m.c === c)) {
|
|
tile.classList.add('tile-merged');
|
|
}
|
|
|
|
layer.appendChild(tile);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Core 2048 Movement Logic
|
|
function slideArray(row) {
|
|
let arr = row.filter(x => x !== 0);
|
|
let gained = 0;
|
|
let mergedIndices = [];
|
|
|
|
for (let i = 0; i < arr.length - 1; i++) {
|
|
if (arr[i] === arr[i + 1]) {
|
|
arr[i] *= 2;
|
|
gained += arr[i];
|
|
mergedIndices.push(i);
|
|
arr.splice(i + 1, 1);
|
|
}
|
|
}
|
|
while (arr.length < SIZE) arr.push(0);
|
|
return { arr, gained, mergedIndices };
|
|
}
|
|
|
|
function move(dir) {
|
|
if (isOver) return false;
|
|
let moved = false;
|
|
let roundScore = 0;
|
|
let highestMerged = 0;
|
|
let mergedPositions = [];
|
|
|
|
const backupGrid = grid.map(r => [...r]);
|
|
|
|
if (dir === 'left') {
|
|
for (let r = 0; r < SIZE; r++) {
|
|
const { arr, gained, mergedIndices } = slideArray(grid[r]);
|
|
if (arr.some((v, idx) => v !== grid[r][idx])) moved = true;
|
|
grid[r] = arr;
|
|
roundScore += gained;
|
|
mergedIndices.forEach(c => {
|
|
mergedPositions.push({ r, c });
|
|
if (grid[r][c] > highestMerged) highestMerged = grid[r][c];
|
|
});
|
|
}
|
|
} else if (dir === 'right') {
|
|
for (let r = 0; r < SIZE; r++) {
|
|
const rev = [...grid[r]].reverse();
|
|
const { arr, gained, mergedIndices } = slideArray(rev);
|
|
const finalArr = arr.reverse();
|
|
if (finalArr.some((v, idx) => v !== grid[r][idx])) moved = true;
|
|
grid[r] = finalArr;
|
|
roundScore += gained;
|
|
mergedIndices.forEach(idx => {
|
|
const col = SIZE - 1 - idx;
|
|
mergedPositions.push({ r, c: col });
|
|
if (grid[r][col] > highestMerged) highestMerged = grid[r][col];
|
|
});
|
|
}
|
|
} else if (dir === 'up') {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
const col = [grid[0][c], grid[1][c], grid[2][c], grid[3][c]];
|
|
const { arr, gained, mergedIndices } = slideArray(col);
|
|
if (arr.some((v, r) => v !== grid[r][c])) moved = true;
|
|
for (let r = 0; r < SIZE; r++) grid[r][c] = arr[r];
|
|
roundScore += gained;
|
|
mergedIndices.forEach(r => {
|
|
mergedPositions.push({ r, c });
|
|
if (grid[r][c] > highestMerged) highestMerged = grid[r][c];
|
|
});
|
|
}
|
|
} else if (dir === 'down') {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
const col = [grid[3][c], grid[2][c], grid[1][c], grid[0][c]];
|
|
const { arr, gained, mergedIndices } = slideArray(col);
|
|
const finalCol = arr.reverse();
|
|
if (finalCol.some((v, r) => v !== grid[r][c])) moved = true;
|
|
for (let r = 0; r < SIZE; r++) grid[r][c] = finalCol[r];
|
|
roundScore += gained;
|
|
mergedIndices.forEach(idx => {
|
|
const row = SIZE - 1 - idx;
|
|
mergedPositions.push({ r: row, c });
|
|
if (grid[row][c] > highestMerged) highestMerged = grid[row][c];
|
|
});
|
|
}
|
|
}
|
|
|
|
if (moved) {
|
|
previousState = {
|
|
grid: backupGrid,
|
|
score: score,
|
|
isWon: isWon,
|
|
isOver: isOver
|
|
};
|
|
const btnUndo = document.getElementById('btnUndo');
|
|
if (btnUndo) btnUndo.disabled = false;
|
|
|
|
score += roundScore;
|
|
updateScoresUI();
|
|
|
|
if (highestMerged > 0) {
|
|
playSound('merge', highestMerged);
|
|
} else {
|
|
playSound('slide');
|
|
}
|
|
|
|
const spawned = spawnTile();
|
|
renderTiles(spawned, mergedPositions);
|
|
|
|
checkGameStatus();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function canMove() {
|
|
if (getEmptyCells().length > 0) return true;
|
|
for (let r = 0; r < SIZE; r++) {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
const v = grid[r][c];
|
|
if (c < SIZE - 1 && v === grid[r][c + 1]) return true;
|
|
if (r < SIZE - 1 && v === grid[r + 1][c]) return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
function checkGameStatus() {
|
|
// Check 2048 Win
|
|
if (!isWon && !keepPlayingAfterWin) {
|
|
for (let r = 0; r < SIZE; r++) {
|
|
for (let c = 0; c < SIZE; c++) {
|
|
if (grid[r][c] === 2048) {
|
|
isWon = true;
|
|
showOverlay(true);
|
|
playSound('win');
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Check Game Over
|
|
if (!canMove()) {
|
|
isOver = true;
|
|
showOverlay(false);
|
|
playSound('over');
|
|
}
|
|
}
|
|
|
|
function showOverlay(isVictory) {
|
|
const ov = document.getElementById('gameOverlay');
|
|
const title = document.getElementById('ovTitle');
|
|
const desc = document.getElementById('ovDesc');
|
|
const btnKeep = document.getElementById('btnOvKeep');
|
|
const btnReplay = document.getElementById('btnOvReplay');
|
|
const dict = I18N_2048[currentLang] || I18N_2048.fr;
|
|
|
|
if (isVictory) {
|
|
title.textContent = dict.win_title;
|
|
desc.textContent = dict.win_desc;
|
|
btnKeep.style.display = 'inline-flex';
|
|
btnKeep.textContent = dict.keep_going;
|
|
btnReplay.textContent = dict.replay;
|
|
} else {
|
|
title.textContent = dict.over_title;
|
|
desc.textContent = dict.over_desc;
|
|
btnKeep.style.display = 'none';
|
|
btnReplay.textContent = dict.replay;
|
|
}
|
|
ov.style.display = 'flex';
|
|
}
|
|
|
|
function keepPlaying() {
|
|
keepPlayingAfterWin = true;
|
|
document.getElementById('gameOverlay').style.display = 'none';
|
|
}
|
|
|
|
function startNewGame() {
|
|
grid = emptyGrid();
|
|
score = 0;
|
|
isWon = false;
|
|
keepPlayingAfterWin = false;
|
|
isOver = false;
|
|
previousState = null;
|
|
|
|
const btnUndo = document.getElementById('btnUndo');
|
|
if (btnUndo) btnUndo.disabled = true;
|
|
|
|
document.getElementById('gameOverlay').style.display = 'none';
|
|
updateScoresUI();
|
|
|
|
spawnTile();
|
|
spawnTile();
|
|
renderTiles();
|
|
}
|
|
|
|
// Touch & Swipe Controls
|
|
let touchStartX = 0, touchStartY = 0;
|
|
const gc = document.getElementById('gridContainer');
|
|
|
|
gc.addEventListener('touchstart', (e) => {
|
|
if (e.touches.length === 1) {
|
|
touchStartX = e.touches[0].clientX;
|
|
touchStartY = e.touches[0].clientY;
|
|
}
|
|
}, { passive: true });
|
|
|
|
gc.addEventListener('touchend', (e) => {
|
|
if (e.changedTouches.length === 1) {
|
|
const dx = e.changedTouches[0].clientX - touchStartX;
|
|
const dy = e.changedTouches[0].clientY - touchStartY;
|
|
const absX = Math.abs(dx);
|
|
const absY = Math.abs(dy);
|
|
|
|
if (Math.max(absX, absY) > 25) {
|
|
if (absX > absY) {
|
|
move(dx > 0 ? 'right' : 'left');
|
|
} else {
|
|
move(dy > 0 ? 'down' : 'up');
|
|
}
|
|
}
|
|
}
|
|
}, { passive: true });
|
|
|
|
// Keyboard Controls
|
|
window.addEventListener('keydown', (e) => {
|
|
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight', ' '].includes(e.key)) {
|
|
e.preventDefault();
|
|
}
|
|
|
|
if (e.key === 'ArrowLeft' || e.key === 'q' || e.key === 'Q' || e.key === 'a' || e.key === 'A') {
|
|
move('left');
|
|
} else if (e.key === 'ArrowRight' || e.key === 'd' || e.key === 'D') {
|
|
move('right');
|
|
} else if (e.key === 'ArrowUp' || e.key === 'z' || e.key === 'Z' || e.key === 'w' || e.key === 'W') {
|
|
move('up');
|
|
} else if (e.key === 'ArrowDown' || e.key === 's' || e.key === 'S') {
|
|
move('down');
|
|
} else if (e.key === 'u' || e.key === 'U') {
|
|
undoMove();
|
|
} else if (e.key === 'r' || e.key === 'R') {
|
|
startNewGame();
|
|
}
|
|
});
|
|
|
|
// Start game on load
|
|
startNewGame();
|
|
</script>
|
|
</body>
|
|
</html>
|