-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
141 lines (118 loc) · 4.44 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
import Grid from "./Grid.js";
import Tile from "./Tile.js";
const gameBoard = document.getElementById("game-board");
const grid = new Grid(gameBoard);
// console.log(grid.randomEmptyCell())
grid.randomEmptyCell().tile = new Tile(gameBoard) //Everytime game starts two random cells will be appear of value 2 or 4
grid.randomEmptyCell().tile = new Tile(gameBoard)
setupInput()
//console.log(grid.cellsByColumn);
function setupInput() {
window.addEventListener("keydown", handleInput, { once: true })
}
async function handleInput(e) {
// console.log(e.key);
switch (e.key) {
case "ArrowUp":
if (!canMoveUp()) {
setupInput()
return
}
await moveUp() //waiting for all promises to finish before merging of tiles
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
}
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 Lost!")
})
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( //This will return array of promises
cells.flatMap(group => {
const promises = []
for (let i = 1; i < group.length; i++) {
const cell = group[i]
if (cell.tile == null) continue //To move all the tiles
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) { //if the last cell is not empty then check the tile value0
promises.push(cell.tile.waitForTransition()) //Saving the animations in promises
if (lastValidCell.tile != null) { //and merge the tiles if it has values
lastValidCell.mergeTile = cell.tile //if null then set the tile value
} else {
lastValidCell.tile = cell.tile
}
cell.tile = null //make the previous tile empty
}
}
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 //Top cell cant move upwards
if (cell.tile == null) return false //Null cant be moved
const moveToCell = group[index - 1]
return moveToCell.canAccept(cell.tile) //Check the cell directly above it and if accept is true then return true
})
})
}