-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.js
309 lines (286 loc) · 8.54 KB
/
2048.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//initializing global variables
var board;
var score = 0;
const rows = 4;
const columns = 4;
//starting function
window.onload = function() {
startGame();
}
//initializes the board
function startGame() {
board = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
let tile = document.createElement("div");
tile.id = r.toString() + "-" + c.toString();
let number = board[r][c];
updateTile(tile, number);
//returns board logic to HTML file
document.getElementById("game").append(tile);
}
}
//places 2 2's on the board to begin
spawnTwo();
spawnTwo();
}
//places a 2 on the board randomly
function spawnTwo() {
if (boardFull()) {
gameLost();
return; //do nothing
}
let empty = true;
while (empty) {
let r = Math.floor(Math.random() * rows);
let c = Math.floor(Math.random() * columns);
//if that spot is empty
if (board[r][c] == 0) {
board[r][c] = 2;
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
empty = false;
}
}
}
//checks if game is lost
function gameLost() {
if (!possibleMovesCheck()) {//if no more moves, must be lost
coverScreen.classList.remove("hide");
bottom.classList.add("hide");
container.classList.add("hide");
result.innerText = "Score: " + score.toString();
}
}
//checks if the board is fully occupied
function boardFull() {
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
if (board[r][c] == 0) {
return false;
}
}
}
return true;
}
//updates text and stlying of the tile
function updateTile(tile, number) {
tile.innerText = "";
tile.classList.value = ""; //clears the class assigned
tile.classList.add("tile");
if (number > 0) {
tile.innerText = number;
if (number <= 4096) {
tile.classList.add("t" + number.toString());
}
else {
tile.classList.add("t8192");
}
}
}
/*keyboard inputs for moving tiles around*/
document.addEventListener("keydown", (a) => {
if ((a.code == "ArrowLeft") || (a.code == "KeyA")) {
moveLeft();
spawnTwo();
}
else if ((a.code == "ArrowRight") || (a.code == "KeyD")) {
moveRight();
spawnTwo();
}
else if ((a.code == "ArrowUp") || (a.code == "KeyW")) {
moveUp();
spawnTwo();
}
else if ((a.code == "ArrowDown") || (a.code == "KeyS")) {
moveDown();
spawnTwo();
}
updateScore();
});
function moveLeft() {
for (let r = 0; r < rows; r++) {
let row = board[r];
row = shift(row);
board[r] = row;
//updates value at the tile
for (let c = 0; c < columns; c++) {
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
}
}
}
function moveRight() {
for (let r = 0; r < rows; r++) {
let row = board[r];
row = shift(row.reverse());
board[r] = row.reverse();
//updates value at the tile
for (let c = 0; c < columns; c++) {
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
}
}
}
function moveUp() {
for (let c = 0; c < columns; c++) {
let row = [board[0][c], board[1][c], board[2][c], board[3][c]];
row = shift(row);
//updates value at the tile
for (let r = 0; r < rows; r++) {
board[r][c] = row[r]; //assigning array to be a column
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
}
}
}
function moveDown() {
for (let c = 0; c < columns; c++) {
let row = [board[0][c], board[1][c], board[2][c], board[3][c]];
row = shift(row.reverse());
row = row.reverse(); //reverse back
//updates value at the tile
for (let r = 0; r < rows; r++) {
board[r][c] = row[r]; //assigning array to be a column
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
}
}
}
function shift(row) {
row = row.filter(number => number != 0); //removes 0 from array
//shifts values over
for (let i = 0; i < row.length-1; i++) {
if (row[i] == row[i+1]) {//if adjacent values are the same
row[i] *= 2; //one gets doubled
row[i+1] = 0; //other turns into 0, or empty
score += row[i]; //increase score
}
}
row = row.filter(number => number != 0); //removes 0 from array again
//re-add 0's
while (row.length < columns) {
row.push(0);
}
return row;
}
//updates the score
function updateScore() {
document.getElementById("score").innerText = score;
}
/*cover screen stuff*/
let replayButton = document.getElementById("replayButton");
let coverScreen = document.getElementById("coverScreen");
let result = document.getElementById("result");
let bottom = document.getElementById("bottom");
let container = document.getElementById("container");
//checks adjacent tiles for matches
function adjacentCheck(arr) {
for (let i = 0; i < arr.length-1; i++) {
if (arr[i] == arr[i+1]) {
return true;
}
}
return false;
}
//checks for possible moves
function possibleMovesCheck() {
//checks in rows
for (let r in board) {
if (adjacentCheck(board[r])) {
return true;
}
}
//checks in columns
for (let c = 0; c < columns; c++) {
let columarray = [];
for (let r = 0; r < rows; r++) {
columarray.push(board[r][c]);
}
if (adjacentCheck(columarray)) {
return true;
}
}
return false;
}
//button function
replayButton.addEventListener("click", () => {
coverScreen.classList.add("hide");
bottom.classList.remove("hide");
container.classList.remove("hide");
score = 0;//resets score
updateScore();//resets score being shown
board = [//resets board
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
//resets the classes and text of the tiles to default
for (let r = 0; r < rows; r++) {
for (let c = 0; c < columns; c++) {
let tile = document.getElementById(r.toString() + "-" + c.toString());
let number = board[r][c];
updateTile(tile, number);
}
}
//spawn 2 tiles to begin game
spawnTwo();
spawnTwo();
});
/* touch movement controls */
//touch event variables
var touchStartX, touchStartY;
//touch listeners
document.getElementById("game").addEventListener("touchstart", handleTouchStart, false);
document.getElementById("game").addEventListener("touchmove", handleTouchMove, false);
function handleTouchStart(event) {//setting initial touch position
touchStartX = event.touches[0].clientX;
touchStartY = event.touches[0].clientY;
}
function handleTouchMove(event) {
event.preventDefault();
//end movement variables and change in X, Y
var touchEndX = event.touches[0].clientX;
var touchEndY = event.touches[0].clientY;
var dX = touchEndX - touchStartX;
var dY = touchEndY - touchStartY;
//detecting touch movement thresholds
if ((Math.abs(dX) > 150) || (Math.abs(dY) > 150)) {//touchscreen deadzone
//determine direction of swipe
if (Math.abs(dX) > Math.abs(dY)) {//X movement > than Y
if (dX > 0) {
moveRight(); //swipe right
spawnTwo();
}
else {
moveLeft(); //swipe left
spawnTwo();
}
}
else if (Math.abs(dX) < Math.abs(dY)) {//otherwise, must be Y movement
if (dY > 0) {
moveDown(); //swipe down
spawnTwo();
}
else {
moveUp(); //swipe up
spawnTwo();
}
}
//update starting touch positions to reset
touchStartX = touchEndX;
touchStartY = touchEndY;
updateScore();
}
}