-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
331 lines (300 loc) · 8.81 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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var grid =[];
var size = 4;
var gameOver = false;
var score = 0;
var dc = 0; //debug counter.
//Tiles
function Tile(xPosition, yPosition, value) {
//this.tileID = tileID;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.value = value;
this.isMerged = false;
/*
this.onclick = function () {
console.log(
"id: "+this.tileID
+" xPosition: "+this.xPosition
+" yPosition: "+this.yPosition
+" value: "+this.value
);
}*/
}
document.addEventListener("DOMContentLoaded", function () {
//tests
/*
var arr = [1,2,3,4,5,6,7,8];
arr = arr.filter(function (val) {
return val >=4;
});
console.log(arr);*/
//add interaction (keyboard and buttons)
addInteraction();
initializeGrid();
console.log(grid);
printGrid();
//addRandomTile();
drawDemoGrid()
});
function printGrid() {
var counter = 1;
for (var i = 0; i < grid.length; i++) {
var output = "line"+counter+": ";
for (var j = 0; j < grid[i].length; j++) {
if (grid[i][j]!=null) output+=grid[i][j].value+' | ';
//else output+=grid[j][i]+' | ';
else output+="0"+' | ';
}
console.log(output);
output='';
counter++;
}
console.log("_______________________________")
console.log(grid);
/*
console.log(grid[0][0].value+"|"+grid[0][1].value+"|"+grid[0][2].value+"|"+grid[0][3].value+"|\n"
+grid[1][0].value+"|"+grid[1][1].value+"|"+grid[1][2].value+"|"+grid[1][3].value+"|\n"
+grid[2][0].value+"|"+grid[2][1].value+"|"+grid[2][2].value+"|"+grid[2][3].value+"|\n"
+grid[3][0].value+"|"+grid[3][1].value+"|"+grid[3][2].value+"|"+grid[3][3].value+"|");
*/
/*
console.log(grid[0][0]+"|"+grid[0][1]+"|"+grid[0][2]+"|"+grid[0][3]+"|\n"
+grid[1][0]+"|"+grid[1][1]+"|"+grid[1][2]+"|"+grid[1][3]+"|\n"
+grid[2][0]+"|"+grid[2][1]+"|"+grid[2][2]+"|"+grid[2][3]+"|\n"
+grid[3][0]+"|"+grid[3][1]+"|"+grid[3][2]+"|"+grid[3][3]+"|");*/
/*
console.log(grid[0].length);
console.log(grid.length);
for (var i=0;i<size;i++) {
for (var j=0;j<size;j++) {
console.log(grid[i][j]);
}
}
console.log(grid);*/
}
function initializeGrid() {
for (var i = 0; i<size; i++) {
grid[i]=[];
for (var j = 0; j<size; j++) {
var id = i*4+j;
grid[i][j]=null;
if (i==0 && j==0 || i==0 && j==3) {
grid[i][j]=new Tile(i,j,2);
}
if (j==0&&i==2||i==2&&j==2) grid[i][j]=new Tile(i,j,4);
//new Tile(id, i,j,0);
}
}
addRandomTile();
addRandomTile();
}
/*
function drawGrid() {
var game = $("#game");
for (var i=0;i<size;i++) {
for(var j=0;j<size;j++) {
var tile = grid[j][i];
var value = tile.value;
var id = tile.tileID;
console.log("id: "+id);
//appending
game.append("<div id='tile"+id+ "' class='tile color"+value+"'><div class='value'></div>"+value+"</div>");
$("#tile"+id).on("click",function () {
console.log(id);
this.remove();
})
}
}
}*/
function drawGrid() {
var game = $("#game");
for (var i=0;i<size;i++) {
for(var j=0;j<size;j++) {
var value = 0;
currentTile = grid[i][j];
if (currentTile!=null) value = currentTile.value;
//appending
game.append("<div class='tile color"+value+"'><div class='value'></div>"+value+"</div>");
}
}
}
function drawDemoGrid() {
var game2 = $("#demoGame");
for (var i=0;i<size;i++) {
for(var j=0;j<size;j++) {
//appending
var values = "j:"+j+" i:"+i//switch due to first switch
game2.append("<div id='tile"+ (i*4+j+1) + "' class='tile'><div class='value'></div>"+values+"</div>")
}
}
}
function addRandomTile() {
var tilePlaced = false;
while (!tilePlaced && !gameOver) {
console.log("inf");
var randRow = Math.floor(Math.random() * size);
var randColumn = Math.floor(Math.random() * size);
if (!grid[randRow][randColumn]) {
//a value either 2 or 4. 75% of times it's 2.
var value = Math.random();
if (value<=0.75) value = 2;
else {
value = 4;
}
grid[randRow][randColumn] = new Tile(randRow,randColumn,value);
tilePlaced = true;
updateGrid();
}
else checkGameOver();
}
}
function checkGameOver() {
var zeroFound = false;
for (var i=0;i<size;i++) {
for (var j = 0; j < size; j++) {
if (!grid[j][i] || grid[j][i].value==0) {
zeroFound = true;
break;
}
}
}
if (!zeroFound) {
gameOver = true;
alert("game over!");
}
}
function updateGrid() {
$("#game").empty();
drawGrid();
}
function addInteraction() {
//buttons
$("#newTileBtn").on("click",function () {
addRandomTile();
});
//keyboard
$(window).on("keyup", function (e) {
switch (e.keyCode) {
case 37:
moveLeft(); break;
case 38:
moveUp(); break;
case 39:
moveRight(); break;
case 40:
moveDown(); break;
case 85: //U key
//updateScoreDemo();
break;
}
})
}
//even closer. Doesnt work on a 2-2-4 = 8 should be 4-4 or a 4-4-4 = 4-8 should be 8-4
function moveUp2() {
//var moved = false;
//must run from top to button for it to work
for (var i = size-1; i>=0; i--) {
for (var j = size-1; j>=0; j--) {
var currentTile = grid[j][i];
var aboveTile = grid[j][i-1];
var underTile = grid[j][i+1];
if (currentTile!=null && underTile!=null && currentTile.value==underTile.value && !currentTile.isMerged) {
//merge
//update score
currentTile.value+=underTile.value;
currentTile.isMerged=true;
grid[j].splice(i+1,1);
grid[j].push(null);
}
else if (currentTile!=null && aboveTile!=null && currentTile.value==aboveTile.value && !aboveTile.isMerged) {
//merge
//update score
aboveTile.value+=currentTile.value;
aboveTile.isMerged=true;
grid[j].splice(i,1);
grid[j].push(null);
}
else if (grid[j][i]==null) {
grid[j].splice(i,1);
grid[j].push(null);
}
}
}
addRandomTile();
printGrid();
}
//WORKS!!!!19/3_1740.
function moveLeft() {
//var moved = false;
for (var i = 0; i<size; i++) {
removeElementAddNull(grid[i],null); //pops nulls, then pushes nulls. Like a queue.
//printGrid();
for (var j = 0; j<size; j++) {
var currentTile = grid[i][j];
var aboveTile = grid[i][j+1];
if(currentTile!=null && aboveTile!=null && currentTile.value==aboveTile.value) {
//merge
//update score
updateScore(currentTile.value+aboveTile.value);
currentTile.value+=aboveTile.value;
grid[i].splice(j+1,1); //splices aboveTile
grid[i].push(null);
}
}
}
addRandomTile();
printGrid();
}
function moveDown() {
rotateGrid90Clockwise(grid);
moveLeft();
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
updateGrid();
}
function moveRight() {
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
moveLeft();
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
updateGrid();
}
function moveUp() {
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
rotateGrid90Clockwise(grid);
moveLeft();
rotateGrid90Clockwise(grid);
updateGrid();
}
function removeElementAddNull(arr,val)
{
var i = arr.length;
while (i--) {
if (arr[i] === val) {
arr.splice(i, 1);
arr.push(null);
}
}
return arr;
}
function updateScore(add) {
score += add;
$("#scoreValue").text(score);
}
function rotateGrid90Clockwise(arr) {
//transposed
for (var i = 0; i < arr.length; i++) {
for (var j = 0; j <i; j++) {
//swap element[i,j] and element[j,i]
var temp = arr[i][j];
arr[i][j] = arr[j][i];
arr[j][i] = temp;
}
}
for (var i = 0; i < arr.length; i++) {
arr[i].reverse();
}
}