-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumber.js
65 lines (47 loc) · 1.64 KB
/
number.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
import game from "./game.js";
const WINNING_SCORE = 2048;
const number = {
// spawn a new tile
spawn: function (shouldSpawn = true) {
const emptyCellIndex = game.randomEmptyCellIndex();
if (emptyCellIndex === false) {
return false;
}
if (shouldSpawn) {
const numberElement = document.createElement("div");
const numberValue = Math.random() > 0.5 ? 2 : 4;
numberElement.innerText = numberValue;
numberElement.dataset.value = false;
numberElement.classList.add("number");
numberElement.style.top = `${game.cells[emptyCellIndex].top}px`;
numberElement.style.left = `${game.cells[emptyCellIndex].left}px`;
game.cells[emptyCellIndex].number = numberElement;
game.containerElement.append(numberElement);
}
return true;
},
// move a tile from one index to another
moveTo: function (fromCell, toCell) {
const number = fromCell.number;
if (toCell.number === null) {
number.style.top = `${toCell.top}px`;
number.style.left = `${toCell.left}px`;
toCell.number = number;
fromCell.number = null;
} else if (number.innerText === toCell.number.innerText) {
number.style.top = `${toCell.top}px`;
number.style.left = `${toCell.left}px`;
setTimeout(function () {
game.containerElement.removeChild(number);
}, 500);
const newNumberValue = parseInt(toCell.number.innerText) * 2;
toCell.number.dataset.value = true;
toCell.number.innerText = newNumberValue;
fromCell.number = null;
if (newNumberValue === WINNING_SCORE) {
game.won = true;
}
}
},
};
export default number;