forked from CPSquad1/2048
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
140 lines (122 loc) · 3.69 KB
/
script.js
File metadata and controls
140 lines (122 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// === Constants & DOM Elements ===
const GRID_SIZE = 4;
const gridCells = document.querySelectorAll(".grid-cell");
const newGameBtn = document.getElementById("new-game");
const tryAgainBtn = document.getElementById("retry");
const continueBtn = document.getElementById("continue");
const messageBox = document.getElementById("message");
const messageText = messageBox.querySelector("p");
const scoreElement = document.getElementById("score");
const bestScoreElement = document.getElementById("best-score");
let grid = [];
let score = 0;
let bestScore = 0;
let gameWon = false;
// === Initialize Game ===
document.addEventListener("DOMContentLoaded", startNewGame);
newGameBtn.addEventListener("click", startNewGame);
tryAgainBtn.addEventListener("click", () => startNewGame());
continueBtn.addEventListener("click", () => {
messageBox.style.display = "none"; // Hide win message
});
// === Start / Reset Game ===
function startNewGame() {
grid = Array.from({ length: GRID_SIZE }, () => Array(GRID_SIZE).fill(0));
score = 0;
gameWon = false;
updateScore();
messageBox.style.display = "none";
// Spawn two starting tiles
spawnRandomTile();
spawnRandomTile();
renderGrid();
}
// === Core Utility Functions ===
function getEmptyCells() {
const empty = [];
for (let r = 0; r < GRID_SIZE; r++) {
for (let c = 0; c < GRID_SIZE; c++) {
if (grid[r][c] === 0) empty.push({ row: r, col: c });
}
}
return empty;
}
function spawnRandomTile() {
const emptyCells = getEmptyCells();
if (emptyCells.length === 0) return false;
const { row, col } = emptyCells[Math.floor(Math.random() * emptyCells.length)];
const newValue = Math.random() < 0.9 ? 2 : 4;
grid[row][col] = newValue;
return true;
}
// === Rendering ===
function renderGrid() {
gridCells.forEach((cell, index) => {
const row = Math.floor(index / GRID_SIZE);
const col = index % GRID_SIZE;
const value = grid[row][col];
cell.textContent = value === 0 ? "" : value;
cell.style.backgroundColor = getTileColor(value);
cell.style.color = value <= 4 ? "#776e65" : "#f9f6f2";
});
}
// Tile colors
function getTileColor(value) {
const colors = {
0: "#cdc1b4",
2: "#eee4da",
4: "#ede0c8",
8: "#f2b179",
16: "#f59563",
32: "#f67c5f",
64: "#f65e3b",
128: "#edcf72",
256: "#edcc61",
512: "#edc850",
1024: "#edc53f",
2048: "#edc22e",
};
return colors[value] || "#3c3a32";
}
// === Score Handling ===
function updateScore() {
scoreElement.textContent = score;
if (score > bestScore) {
bestScore = score;
bestScoreElement.textContent = bestScore;
}
}
// === Arrow Key Handling (simplified placeholder) ===
document.addEventListener("keydown", (e) => {
if (!["ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].includes(e.key)) return;
// FUTURE: Add move + merge logic here
// For now, just spawn a random tile after key press
const moved = spawnRandomTile();
if (moved) renderGrid();
// Check win condition (2048)
if (!gameWon && grid.some(row => row.includes(2048))) {
gameWon = true;
messageText.textContent = "You Win!";
messageBox.style.display = "flex";
}
// Check game over
if (isGameOver()) {
messageText.textContent = "Game Over!";
messageBox.style.display = "flex";
}
});
// === Game Over Check ===
function isGameOver() {
if (getEmptyCells().length > 0) return false;
// Check for possible merges
for (let r = 0; r < GRID_SIZE; r++) {
for (let c = 0; c < GRID_SIZE; c++) {
const current = grid[r][c];
if ((r < GRID_SIZE-1 && grid[r+1][c] === current) ||
(c < GRID_SIZE-1 && grid[r][c+1] === current)) {
return false;
}
}
}
return true; // No empty cells and no possible merges
}