-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
189 lines (155 loc) · 4.77 KB
/
game.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import number from "./number.js";
// An object containing the functionalities of the game
const game = {
containerElement: document.getElementsByClassName("container")[0],
cells: [],
playable: false,
won: false,
directionRoots: {
UP: [1, 2, 3, 4],
RIGHT: [4, 8, 12, 16],
DOWN: [13, 14, 15, 16],
LEFT: [1, 5, 9, 13],
},
// Start the game and initialize spawn 2 tiles
init: function () {
const cellElements = document.getElementsByClassName("cell");
let cellIndex = 1;
for (let cellElement of cellElements) {
game.cells[cellIndex] = {
element: cellElement,
top: cellElement.offsetTop,
left: cellElement.offsetLeft,
number: null,
};
cellIndex++;
}
this.playable = true;
number.spawn();
number.spawn();
},
// after each action, reset the merged flag to false
resetMerged: function () {
game.cells.forEach(function (cell) {
if (cell.number) {
cell.number.dataset.value = false;
}
});
},
// returns an empty index
randomEmptyCellIndex: function () {
let emptyCells = [];
for (let i = 1; i < this.cells.length; i++) {
if (this.cells[i].number === null) {
emptyCells.push(i);
}
}
if (emptyCells.length === 0) {
return false;
}
return emptyCells[Math.floor(Math.random() * emptyCells.length)];
},
// check for possible moves in a given direction
checkMovesInDirection: function (direction) {
const roots = this.directionRoots[direction];
let increment = direction === "RIGHT" || direction === "DOWN" ? -1 : 1;
increment *= direction === "UP" || direction === "DOWN" ? 4 : 1;
// start checking from further most end for each row/column
for (let i = 0; i < roots.length; i++) {
const root = roots[i];
let adjCellIndex = roots[i];
// checks if the farthest and the adjacent tile are same
for (let j = 1; j < 4; j++) {
const cellIndex = root + j * increment;
const cell = this.cells[cellIndex];
const adjCell = this.cells[adjCellIndex];
if (cell.number && adjCell.number) {
if (cell.number.innerText === adjCell.number.innerText) {
return true;
}
adjCellIndex = cellIndex;
} else {
// zero exists so movement possible
return true;
}
}
}
return false;
},
// check if a new tile could be spawned or if movement exist any direction
checkIfGameLost: function (hasMoved) {
if (number.spawn(hasMoved) === false) {
let movesInUp = this.checkMovesInDirection("UP");
let movesInDown = this.checkMovesInDirection("DOWN");
let movesInLeft = this.checkMovesInDirection("LEFT");
let movesInRight = this.checkMovesInDirection("RIGHT");
return !(movesInUp || movesInDown || movesInRight || movesInLeft);
}
return false;
},
// check if the game should be further continued
checkGameStatus: function (hasMoved) {
if (game.won) {
alert("Game won!");
return;
}
if (game.checkIfGameLost(hasMoved)) {
alert("Game lost");
return;
} else {
game.playable = true;
}
},
// slide tile in the given direction
slide: function (direction) {
if (!this.playable) {
return false;
}
this.playable = false;
const roots = this.directionRoots[direction];
let increment = direction === "RIGHT" || direction === "DOWN" ? -1 : 1;
increment *= direction === "UP" || direction === "DOWN" ? 4 : 1;
let hasMoved = false;
for (let i = 0; i < roots.length; i++) {
const root = roots[i];
for (let j = 1; j < 4; j++) {
const cellIndex = root + j * increment;
const cell = this.cells[cellIndex];
if (cell.number !== null) {
let moveToCell = null;
for (let k = j - 1; k >= 0; k--) {
const toCellIndex = root + k * increment;
const toCell = this.cells[toCellIndex];
if (toCell.number === null) {
moveToCell = toCell;
} else if (toCell.number.dataset.value === true) {
break;
} else if (cell.number.innerText === toCell.number.innerText) {
moveToCell = toCell;
break;
} else {
break;
}
}
if (moveToCell !== null) {
number.moveTo(cell, moveToCell);
hasMoved = true;
}
}
}
}
this.resetMerged();
setTimeout(function () {
game.checkGameStatus(hasMoved);
}, 500);
},
// resets the game
reset: function () {
document.querySelectorAll(".number").forEach((e) => e.remove());
game.cells = [];
game.playable = false;
game.won = false;
game.init();
},
};
export default game;