-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
294 lines (254 loc) · 5.51 KB
/
model.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
var data;
var TableSize; //height, width
var win = 0;
var gameOver = 0;
var turn = 0;
var score = 0;
var Win_val = 2048;
function getTile(row,col)
{
if ((row < 0) || (row >= TableSize[0]) || (col < 0) || (col >= TableSize[1]))
throw "tile requested outside of range";
return data[row][col];
}
function initialize(height,width)//sets the game grid to all 0's
{
TableSize = [height,width];
var ful = [];//the full table data
var row = [];//the data for one row
for (j = 0; j<width; j++)
{
row.push(0);
}
for (i = 0;i<height;i++)
{
var temp = [...row];
ful.push(temp);
}
data = ful;
}
function isWin()
{
if (win > 0)
{return true;}
else {return false;}
}
function up()
{
var mov = 0; //has this function changed data yet?
for (i = 1;i < TableSize[0]; i++) //top to bottom
{
for (j = 0; j < TableSize[1]; j++) //from left to right
{
if (data[i][j] == 0) // skips unnecesary movements
{ continue;}
mov |= moveTile("up", i,j); //main purpose is to run moveTile, but it also sets mov to 1 if anything has been moved
}
}
if (!(mov ==0))
{
addRandomTile();
}
}
function moveTile(dir, row, col)
{
if (data[row][col] == 0) //skips unnecesary computation
{return;}
switch (dir){
case ("up"):
if (row - 1 >= 0)//verifies that there is a space to move the tile to
{
next = data[row-1][col];
if (next == 0) //checks to see if next tile is blank
{
data[row-1][col] += data[row][col];//moves it
data[row][col] = 0;
moveTile(dir, row-1,col);//checks to see if it can move again
return 1;
}
if (data[row][col] == next)//checks to see if it can merge
{
data[row-1][col] += data[row][col];
score += 2* next;
data[row][col] = 0;
return 1;
}
}
break;
case ("down")://see comments on up
if (row + 1 < TableSize[0])
{
next = data[row+1][col];
if (next == 0)
{
data[row+1][col] += data[row][col];
data[row][col] = 0;
moveTile(dir, row+1,col);
return 1;
}
if (data[row][col] == next)
{
score+= 2*next;
data[row+1][col] += data[row][col];
data[row][col] = 0;
return 1;
}
}
break;
case ("left")://see comments on up
if (col - 1 >= 0)
{
next = data[row][col-1];
if (next == 0)
{
data[row][col-1] += data[row][col];
data[row][col] = 0;
moveTile(dir, row, col-1);
return 1;
}
if (data[row][col] == next)
{
score+= 2*next;
data[row][col-1] += data[row][col];
data[row][col] = 0;
return 1;
}
}
break;
case ("right")://see comments on up
if (col + 1 < TableSize[1])
{
next = data[row][col+1];
if (next == 0)
{
data[row][col+1] += data[row][col];
data[row][col] = 0;
moveTile(dir, row,col+1);
return 1;
}
if (data[row][col] == next)
{
score+= 2*next;
data[row][col+1] += data[row][col];
data[row][col] = 0;
return 1;
}
}
break;
}
}
function newGame()
{
initialize(TableSize[0],TableSize[1]);
addRandomTile();
win = 0;
gameOver = 0;
score = 0;
}
function getScore()
{
return score;
}
function addRandomTile()
{
for (i = 0; i < 16; i++) //tries to find a random tile, up to 16 times
{
var r = Math.floor(Math.random() * TableSize[0]);//random row
var c = Math.floor( Math.random() * TableSize[1]);//random column
if (data[r][c] == 0)
{
data[r][c] = 2 * Math.floor(Math.random() * 2 + 1);
return;
}
}
for (i = 0; i<TableSize[0];i++)// if a random tile could not be found, place it in the top left most open tile.
{
for (j = 0; j<TableSize[1];j++)
{
if (data[i][j] == 0)
{
data[i][j] = 2 * Math.floor((Math.random() * 2) + 1);
return;
}
}
}
}
function right()
{
var mov = 0;
for (j = TableSize[1] -1; j >= 0; j--) //from right to left
{
for (i = 0; i < TableSize[0]; i++) //from top to bottom
{
mov |= moveTile("right",i,j);
}
}
if (!(mov == 0))//don't add a tile if the previous move didn't move anything
addRandomTile();
}
function left()
{
var mov = 0;//a bool to see if anything has moved
var saveData = data;
for (i = 0;i<TableSize[0] ;i++) //from left to right
{
for (j = 0; j < TableSize[1]; j++) //from top to bottom
mov |= moveTile("left",i,j);
}
if (!(mov == 0))
addRandomTile();
}
function down()
{
var mov = 0;
var saveData = data;
for (i = TableSize[0] -1; i>=0;i--) //from top to bottom
{
for (j = 0; j < TableSize[1]; j++) //from left to right
{
if (data[i][j] == 0) // skips unnecesary movements. Doesn't change result, just saves time
{ continue;}
mov |= moveTile("down", i,j);
}
}
if (!(mov == 0))
addRandomTile();
}
function checkLose() //checks to see if there is a valid move by going through each row, then each column
{
var t = 0;
for (i = 0; i < TableSize[0]; i++) //checks every row
{
for (j = 0; j < TableSize[1]; j++)
{
if (data[i][j] >= 2048)
{
win = 1;
}
if (t == data[i][j])// if element is the same as previous one, return false
{
return false;
}
if (data[i][j] == 0)// checks to see if there is a 0 anywhere, might as well be here so I don't have to write another for loop!
{
return false;
}
t = data[i][j];
}
t = 0; //resets t to avoid it thinking "line jumps" are valid moves
}
for (j = 0; j < TableSize[1]; j++) //checks every column
{
for (i = 0; i < TableSize[0]; i++)
{
if (t == data[i][j])// if element is the same as previous one, return false
{
return false;
}
t = data[i][j];
}
t = 0; //resets t to avoid it thinking "line jumps" are valid moves
}
gameOver = 1;
return true;
}