-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiece.js
592 lines (432 loc) · 16.7 KB
/
piece.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
;(function() {
'use strict';
function Piece() {
this.currentPosition={};
this.selectShape();
this.selectColour();
this.allowedMoves={
'left':true,
'right':true,
'down':true,
'rotate':true
};
this.stopped=false;
this.devOutput();
}
Piece.prototype = {
// stores what moves can be made from the current piece position
allowedMoves:{
'left':true,
'right':true,
'down':true,
'rotate':true
},
// stores the colour of this piece
colour:'',
// the set of colours this piece may take, needs to correspond to _colours.scss
colours:{
0:'blue',
1:'green',
2:'yellow',
3:'red',
4:'grey',
5:'black',
6:'purple',
7:'orange',
8:'turquoise'
},
// the current set of co-ordinates for this piece, four x/ys
currentPosition:{},
// the current orientation of this piece
currentOrientation:0,
// as above two but for next
nextPosition:{},
nextOrientation:0,
// the shape of this piece
shape:{},
// shapes including their initial positions on a ten wide grid
// positions can be centred for other grid widths by incrementing
// or decrementing all x co-ordinates
shapes:{
// I 0123456789 0123456789
// 0 .....#.... ..........
// 1 .....#.... ...####...
// 2 .....#.... ..........
// 3 .....#.... ..........
0:
{
0:{0:{x:5,y:0},1:{x:5,y:1},2:{x:5,y:2},3:{x:5,y:3}},
1:{0:{x:3,y:1},1:{x:4,y:1},2:{x:5,y:1},3:{x:6,y:1}}
},
// J 0123456789 0123456789 0123456789 0123456789
// 0 .....#.... ....#..... .....##... ..........
// 1 .....#.... ....###... .....#.... ....###...
// 2 ....##.... .......... .....#.... ......#...
// 3 .......... .......... .......... ..........
1:
{
0:{0:{x:5,y:0},1:{x:5,y:1},2:{x:4,y:2},3:{x:5,y:2}},
1:{0:{x:4,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:6,y:1}},
2:{0:{x:5,y:0},1:{x:6,y:0},2:{x:5,y:1},3:{x:5,y:2}},
3:{0:{x:4,y:1},1:{x:5,y:1},2:{x:6,y:1},3:{x:6,y:2}}
},
// L 0123456789 0123456789 0123456789 0123456789
// 0 .....#.... .......... ....##.... ......#...
// 1 .....#.... ....###... .....#.... ....###...
// 2 .....##... ....#..... .....#.... ..........
// 3 .......... .......... .......... ..........
2:
{
0:{0:{x:5,y:0},1:{x:5,y:1},2:{x:5,y:2},3:{x:6,y:2}},
1:{0:{x:4,y:1},1:{x:5,y:1},2:{x:6,y:1},3:{x:4,y:2}},
2:{0:{x:4,y:0},1:{x:5,y:0},2:{x:5,y:1},3:{x:5,y:2}},
3:{0:{x:6,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:6,y:1}}
},
// O 0123456789
// 0 ....##....
// 1 ....##....
// 2 ..........
// 3 ..........
3:
{
0:{0:{x:4,y:0},1:{x:5,y:0},2:{x:4,y:1},3:{x:5,y:1}}
},
// S 0123456789 0123456789
// 0 .....##... ....#.....
// 1 ....##.... ....##....
// 2 .......... .....#....
// 3 .......... ..........
4:
{
0:{0:{x:5,y:0},1:{x:6,y:0},2:{x:4,y:1},3:{x:5,y:1}},
1:{0:{x:4,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:5,y:2}}
},
// T 0123456789 0123456789 0123456789 0123456789
// 0 .......... .....#.... .....#.... .....#....
// 1 ....###... ....##.... ....###... .....##...
// 2 .....#.... .....#.... .......... .....#....
// 3 .......... .......... .......... ..........
5:
{
0:{0:{x:4,y:1},1:{x:5,y:1},2:{x:6,y:1},3:{x:5,y:2}},
1:{0:{x:5,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:5,y:2}},
2:{0:{x:5,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:6,y:1}},
3:{0:{x:5,y:0},1:{x:5,y:1},2:{x:6,y:1},3:{x:5,y:2}}
},
// Z 0123456789 0123456789
// 0 ....##.... .....#....
// 1 .....##... ....##....
// 2 .......... ....#.....
// 3 .......... ..........
6:
{
0:{0:{x:4,y:0},1:{x:5,y:0},2:{x:5,y:1},3:{x:6,y:1}},
1:{0:{x:5,y:0},1:{x:4,y:1},2:{x:5,y:1},3:{x:4,y:2}}
}
},
// is this piece stopped, ie rached as far down as it will go and so a new piece is required
stopped:false,
/**
* countElements() - count the elements in an object
*
* @param count - integer count of object properties
*/
countElements: function(obj)
{
return Object.keys(obj).length;
},
/**
* selectShape() - selects a shape and orientation
*/
selectShape:function()
{
// select one of seven pieces at random
this.shape=this.shapes[this.selectRandom(this.shapes)];
// select a current orientation from that shape
this.currentOrientation=this.selectRandom(this.shape);
// clone that orientation into the current position property so
// shapes array is not edited on piece move
this.cloneOrientation();
},
/**
* selectColour() - selects a colour for this piece
*/
selectColour:function()
{
this.colour=this.colours[this.selectRandom(this.colours)];
},
/**
* selectRandom() - randomly selects a ref num from a count of object properties
*
* @param count - integer count of object properties
*/
selectRandom: function (obj)
{
return Math.floor(Math.random() * this.countElements(obj));
},
/**
* cloneOrientation() - copy the orientation into the object from the shapes
*/
cloneOrientation: function()
{
this.currentPosition={
0:{
x:this.shape[this.currentOrientation][0].x,
y:this.shape[this.currentOrientation][0].y
},
1:{
x:this.shape[this.currentOrientation][1].x,
y:this.shape[this.currentOrientation][1].y
},
2:{
x:this.shape[this.currentOrientation][2].x,
y:this.shape[this.currentOrientation][2].y
},
3:{
x:this.shape[this.currentOrientation][3].x,
y:this.shape[this.currentOrientation][3].y
}
}
},
/**
* getNextOrientation() - looks at the next orientation for this piece
*/
getNextOrientation: function()
{
var count=this.countElements(this.shape);
if (this.currentOrientation === (count - 1))
{
var orientation=this.shape[0];
this.nextOrientation=0;
return orientation;
}
else
{
var orientation=this.shape[this.currentOrientation + 1];
this.nextOrientation=this.currentOrientation + 1;
return orientation;
}
},
/**
* displayPiece() - set the current piece to be visible on the screen in its current
* position
* @param cells - the array of cells
*/
displayPiece:function(cells)
{
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
cells[coordinates.y][coordinates.x].markCell(this.colour);
}
},
/**
* displayPreviewPiece() - place a piece in the preview window
*/
displayPreviewPiece: function(previewCells)
{
for (var y=0; y<5; y++)
{
for (var x=0; x<5; x++)
{
previewCells[y][x].unmarkCell();
}
}
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
previewCells[coordinates.y+1][coordinates.x-3].markCell(this.colour);
}
},
/**
* movePiece() - moves a piece one cell in the given direction
* @param cells - array of cells for redrawing
* @param direction - left, right or down
* @param interval - timing interval for pausing set stopped action
*/
movePiece:function(cells,direction,interval)
{
if (false === this.checkMove('down') &&
'down' === direction)
{
// delay set stopped so that piece can be moved either side
var pauseInterval=interval-1; // remove one millisecond so as not to interfere with next interval
var t=this;
setTimeout(function() { t.setStopped(); },pauseInterval);
}
else
{
if (true === this.checkMove(direction))
{
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
cells[coordinates.y][coordinates.x].unmarkCell();
this.setNewCoordinates(coordinates,direction);
}
this.setAllowedMoves(cells);
this.displayPiece(cells);
}
}
},
/**
* setNewCoordinates() - sets the new coordinates based on direction
* @param currentCoordinates - the cell coordinates to work on
* @param direction - direction the piece should move
*/
setNewCoordinates:function (currentCoordinates,direction)
{
switch(direction)
{
case 'left':
currentCoordinates.x--;
break;
case 'right':
currentCoordinates.x++;
break;
case 'down':
currentCoordinates.y++;
break;
}
},
/**
* rotate() - rotate the current piece clockwise
*/
rotate:function(cells)
{
if (true === this.checkMove('rotate'))
{
// get the next orientation
this.nextPosition=this.getNextOrientation();
// compute cell differences, i.e. the relationship between current and next
// use the original orientation
var compareOrientation=this.shape[this.currentOrientation];
// get x and y offset from the original orientation position
var xoffset=this.currentPosition[0].x - compareOrientation[0].x;
var yoffset=this.currentPosition[0].y - compareOrientation[0].y;
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
cells[coordinates.y][coordinates.x].unmarkCell();
this.currentPosition[index].x=(this.nextPosition[index].x + xoffset);
this.currentPosition[index].y=(this.nextPosition[index].y + yoffset);
}
this.setAllowedMoves(cells);
this.displayPiece(cells);
this.currentOrientation=this.nextOrientation;
}
},
/**
* setStopped() - set this piece as stopped, this is checked by the interval timer
* and used to generate the new pieces
*/
setStopped: function ()
{
this.stopped=true;
},
/**
* resetAllowedMoves() - sets all allowed moves to true - required so that a piece
* move made before into an unmovable position doesn't block a legal move now
*/
resetAllowedMoves: function()
{
this.allowedMoves['left']=true;
this.allowedMoves['right']=true;
this.allowedMoves['down']=true;
this.allowedMoves['rotate']=true;
},
/**
* setAllowedMoves() - takes the current position and checks for the allowed moves
* @param cells - the array of cells
*/
setAllowedMoves: function(cells)
{
this.resetAllowedMoves();
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
if (0 === coordinates.x ||
1 === cells[coordinates.y][(coordinates.x - 1)].state)
{
this.allowedMoves['left']=false;
}
if ((Config.size.width - 1) === coordinates.x ||
1 === cells[coordinates.y][(coordinates.x + 1)].state)
{
this.allowedMoves['right']=false;
}
if ((Config.size.height - 1) === coordinates.y ||
1 === cells[(coordinates.y + 1)][coordinates.x].state)
{
this.allowedMoves['down']=false;
}
}
// rotate
// get the next orientation
var nextPositionTest=this.getNextOrientation();
// compute cell differences, i.e. the relationship between current and next
// use the original orientation
var compareOrientation=this.shape[this.currentOrientation];
// get x and y offset from the original orientation position
var xoffset=this.currentPosition[0].x - compareOrientation[0].x;
var yoffset=this.currentPosition[0].y - compareOrientation[0].y;
for (var index in this.currentPosition)
{
var coordinates=this.currentPosition[index];
var nextPosX=nextPositionTest[index].x + xoffset;
var nextPosY=nextPositionTest[index].y + yoffset;
if (nextPosX<0 ||
nextPosX>=Config.size.width)
{
this.allowedMoves['rotate']=false;
break;
}
if (nextPosY<0 ||
nextPosY>=Config.size.height)
{
this.allowedMoves['rotate']=false;
break;
}
if (1 === cells[nextPosY][nextPosX].state)
{
this.allowedMoves['rotate']=false;
break;
}
}
},
/**
* checkMove() - looks at allowed moves to see if the move event is viable
*/
checkMove:function(move)
{
return this.allowedMoves[move];
},
/**
* devOutput() - adds this pieces data to the piece array dev output
*/
devOutput: function()
{
var devPieceOut=document.createElement("div");
var coordinates="<span class='dev-coordinates'>";
var c=1;
for (var coords in this.currentPosition)
{
coordinates+=c+"y:"+this.currentPosition[coords].y+" x:"+this.currentPosition[coords].x+"; ";
c++;
}
coordinates+="</span><br/><br/>";
devPieceOut.innerHTML=coordinates;
var allowedMoves="<span class='dev-allowed'>";
for (var allowed in this.allowedMoves)
{
allowedMoves+=this.allowedMoves[allowed]+"; ";
c++;
}
allowedMoves+="</span><br/><br/>";
devPieceOut.innerHTML=JSON.stringify(this,null,4)+"<br/><br/>";
//devPieceOut.innerHTML=JSON.stringify(this.shapes,null,4)+"<br/><br/>";
//document.getElementById("js-piece-data").appendChild(devPieceOut);
}
};
window.Piece = Piece;
}());