-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
133 lines (116 loc) · 3.42 KB
/
app.js
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
import Grid from "./Grid.js";
import Tile from "./Tile.js";
const gameBoard = document.getElementById("game-board");
const grid = new Grid(gameBoard);
grid.randomEmptyCell().tile = new Tile(gameBoard);
grid.randomEmptyCell().tile = new Tile(gameBoard);
setupInput();
function setupInput() {
window.addEventListener("keydown", handleInput, { once: true });
}
async function handleInput(e) {
switch (e.key) {
case "ArrowUp":
if (!canMoveUp()) {
setupInput();
return;
}
await moveUp();
break;
case "ArrowRight":
if (!canMoveRight()) {
setupInput();
return;
}
await moveRight();
break;
case "ArrowDown":
if (!canMoveDown()) {
setupInput();
return;
}
await moveDown();
break;
case "ArrowLeft":
if (!canMoveLeft()) {
setupInput();
return;
}
await moveLeft();
break;
default:
setupInput();
break;
}
grid.cells.forEach((cell) => cell.mergeTiles());
const newTile = new Tile(gameBoard);
grid.randomEmptyCell().tile = newTile;
if (!canMoveUp() && !canMoveRight() && !canMoveDown() && !canMoveLeft()) {
newTile.waitForTransition(true).then(() => {
alert("You lose");
});
return;
}
setupInput();
}
function moveUp() {
slideTiles(grid.cellsByColumn);
}
function moveDown() {
slideTiles(grid.cellsByColumn.map((column) => [...column].reverse()));
}
function moveLeft() {
slideTiles(grid.cellsByRow);
}
function moveRight() {
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);
});
});
}