Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
164 lines (140 loc) · 3.82 KB
/
Copy pathscript.js
File metadata and controls
164 lines (140 loc) · 3.82 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import Grid from "./grid.js"
import Tile from "./tile.js"
const gameBoard = document.getElementById("game-board")
const scoreElement = document.getElementById("score")
const grid = new Grid(gameBoard)
grid.randomEmptyCell().tile = new Tile(gameBoard)
grid.randomEmptyCell().tile = new Tile(gameBoard)
setupInput()
const resetButton = document.querySelector(".fancy-button");
resetButton.addEventListener("click", () => {
window.location.reload(); // Refreshes the page
});
const homeButton = document.querySelector(".home-button");
homeButton.addEventListener("click", () => {
window.location.href = "https://2048-landing-page.vercel.app"; // Open the home page
});
function setupInput() {
window.addEventListener("keydown", handleInput, { once: true })
}
document.querySelector("#up").addEventListener("click", async () => {
handleInput({ key: "ArrowUp" });
});
document.querySelector("#down").addEventListener("click", async () => {
handleInput({ key: "ArrowDown" });
});
document.querySelector("#left").addEventListener("click", async () => {
handleInput({ key: "ArrowLeft" });
});
document.querySelector("#right").addEventListener("click", async () => {
handleInput({ key: "ArrowRight" });
});
async function handleInput(e) {
switch (e.key) {
case "ArrowUp":
if (!canMoveUp()) {
setupInput();
return;
}
await moveUp();
break;
case "ArrowDown":
if (!canMoveDown()) {
setupInput();
return;
}
await moveDown();
break;
case "ArrowLeft":
if (!canMoveLeft()) {
setupInput();
return;
}
await moveLeft();
break;
case "ArrowRight":
if (!canMoveRight()) {
setupInput();
return;
}
await moveRight();
break;
default:
setupInput();
return;
}
finalizeMove();
}
function finalizeMove(){
grid.cells.forEach(cell => cell.mergeTiles())
const newTile = new Tile(gameBoard)
grid.randomEmptyCell().tile = newTile
if (!canMoveUp() && !canMoveDown() && !canMoveLeft() && !canMoveRight()) {
newTile.waitForTransition(true).then(() => {
alert("You lose")
})
return
}
setupInput()
}
function moveUp() {
return slideTiles(grid.cellsByColumn)
}
function moveDown() {
return slideTiles(grid.cellsByColumn.map(column => [...column].reverse()))
}
function moveLeft() {
return slideTiles(grid.cellsByRow)
}
function moveRight() {
return slideTiles(grid.cellsByRow.map(row => [...row].reverse()))
}
function slideTiles(cells) {
return Promise.all(
cells.flatMap(group => {
const promises = []
for (let i = 1; i < group.length; i++) {
const cell = group[i]
if (cell.tile == null) continue
let lastValidCell
for (let j = i - 1; j >= 0; j--) {
const moveToCell = group[j]
if (!moveToCell.canAccept(cell.tile)) break
lastValidCell = moveToCell
}
if (lastValidCell != null) {
promises.push(cell.tile.waitForTransition())
if (lastValidCell.tile != null) {
lastValidCell.mergeTile = cell.tile
} else {
lastValidCell.tile = cell.tile
}
cell.tile = null
}
}
return promises
})
)
}
function canMoveUp() {
return canMove(grid.cellsByColumn)
}
function canMoveDown() {
return canMove(grid.cellsByColumn.map(column => [...column].reverse()))
}
function canMoveLeft() {
return canMove(grid.cellsByRow)
}
function canMoveRight() {
return canMove(grid.cellsByRow.map(row => [...row].reverse()))
}
function canMove(cells) {
return cells.some(group => {
return group.some((cell, index) => {
if (index === 0) return false
if (cell.tile == null) return false
const moveToCell = group[index - 1]
return moveToCell.canAccept(cell.tile)
})
})
}