-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
157 lines (131 loc) · 3.37 KB
/
script.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Grid from "./javascript/Grid.js";
import Tile from "./javascript/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", handleKeyBoard, { once: true });
window.addEventListener("click", handleClick);
}
function handleClick(e) {
let x = e.x - window.innerWidth / 2;
let y = e.y - window.innerHeight / 2;
var key;
if (x y >= 0) {
key = x - y >= 0 ? "right" : "down";
} else {
key = x - y >= 0 ? "up" : "left";
}
handleInput(key);
}
function handleKeyBoard(e) {
handleInput(e.key.replace("Arrow", "").toLowerCase());
}
async function handleInput(key) {
switch (key) {
case "up":
if (!canMoveUp()) {
setupInput();
return;
}
await moveUp();
break;
case "down":
if (!canMoveDown()) {
setupInput();
return;
}
await moveDown();
break;
case "left":
if (!canMoveLeft()) {
setupInput();
return;
}
await moveLeft();
break;
case "right":
if (!canMoveRight()) {
setupInput();
return;
}
await moveRight();
break;
default:
setupInput();
return;
}
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);
});
});
}