-
Notifications
You must be signed in to change notification settings - Fork 2
/
tetris.js
495 lines (416 loc) · 12.9 KB
/
tetris.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
//global variables section
//
gameloop = false; //will hold closure to call at intervals
endgame = false; //will hold id of interval, so it can be cleared
seed = new Date().getTime(); // piece sequence seed
random = new Random(seed); //seeded random number generator
board_height = 18; //height in grid units of board
board_width = 10; //width in grid units of board
prev_height = 4; //height in grid units of preview pane
prev_width = 6; //width in grid units of preview pane
glbl_width = 20; //width of block
border_width = 3; //width of border around board
board_top = 40; //top offset of board
board_left = 40; //left offset of board
prev_top = 100; //top offset of preview box
prev_left = 280; //left offset of preview box
level = 0; //keeps track of the player's level
lines = 0; //number of lines a player has cleared
level_timing = [2, 4, 6, 8, 10, 12, 14, 16, 18, 32, 64]; //number of times per second a block falls
glbl_board = false; //global board variable
current_piece = false; //user-manipulable currently-falling piece
next_piece = false; //the next piece
// keymaps
var arrowkeymap = {
37: shiftleft,
39: shiftright,
40: fall,
32: plummet,
38: rotatecw
};
var ijklmap = {
74: shiftleft, // j
76: shiftright, // l
75: fall, // k
79: plummet, // o
73: rotatecw // i
};
var combinedmap = {
37: shiftleft, // <-
74: shiftleft, // j
39: shiftright, // ->
76: shiftright, // l
40: fall, // \/
75: fall, // k
79: plummet, // o
32: plummet, // <space>
38: rotatecw, // ^
73: rotatecw, // i
85: rotateccw, // u
};
current_keymap = arrowkeymap; // default keymap
SQUARE = 0; //piece types
TEE = 1;
STRAIGHT = 2;
RZEE = 3;
LZEE = 4;
RELL = 5;
LELL = 6;
piece_colors = ["yellow", "purple", "lightblue", "green", "red", "orange", "blue"];
init_positions = [ //intial positions of blocks, with center block first
[[5,17], //square - 0
[6,17],
[5,16],
[6,16]],
[[5,17], //tee - 1
[4,17],
[6,17],
[5,16]],
[[5,17], //straight - 2
[4,17],
[6,17],
[7,17]],
[[5,17], //rzee - 3
[6,17],
[5,16],
[4,16]],
[[5,17], //lzee - 4
[4,17],
[5,16],
[6,16]],
[[5,17], //rell - 5
[4,17],
[6,17],
[4,16]],
[[5,17], //lell - 6
[4,17],
[6,17],
[6,16]]
];
prev_positions = [ //initial positions of preview pieces
[[1,2], //square - 0
[2,2],
[1,1],
[2,1]],
[[2,2], //tee - 1
[1,2],
[3,2],
[2,1]],
[[2,2], //straight - 2
[1,2],
[3,2],
[4,2]],
[[2,2], //rzee - 3
[3,2],
[1,1],
[2,1]],
[[2,2], //lzee - 4
[1,2],
[2,1],
[3,1]],
[[2,2], //rell - 5
[1,2],
[1,1],
[3,2]],
[[2,2], //lell - 6
[1,2],
[3,2],
[3,1]]
];
//
//end of global variables section
//Piece class
function Piece(type) {
this.type = type;
this.blocks = new Array();
this.addBlock = function(b) {
this.blocks.push(b);
}
}
//construct a single div element
function createblock(type) {
var blk = document.createElement("div");
blk.className = "block";
blk.style.backgroundColor = piece_colors[type];
return blk;
}
//construct a board
function createboard() {
var board = new Array(board_width);
for(var i = 0; i < board_width; i++) {
board[i] = new Array(board_height);
}
return board;
}
function translatePos(blocks, top_offset, left_offset, grid_height) {
for(var i = 0; i < blocks.length; i++) {
var blk = blocks[i];
var x = blk.x;
var y = blk.y;
blk.style.top = glbl_width * (grid_height - 1 - y) + top_offset;
blk.style.left = glbl_width * x + left_offset + border_width;
}
}
function drawonboard(piece) {
translatePos(piece.blocks, board_top, board_left, board_height);
}
function drawinpreview(piece) {
translatePos(piece.blocks, prev_top, prev_left, prev_height);
}
//checks a grid position for out of bounds
//or already containg a block
function blocklegal(x, y, board) {
if(x >= board_width || x < 0 || y < 0 || board[x][y])
return false;
return true;
}
//checks that all of the x,y pairs in
//positions are legal positions
function checkposset(positions, board) {
for(var i = 0; i < positions.length; i++) {
if(!blocklegal(positions[i][0], positions[i][1], board))
return false;
}
return true;
}
//if set of positions are legal,
//first four become the positions
//of piece's blocks, and true returned
//otherwise, false
function checkandset(piece, positions, board) {
if(checkposset(positions, board)) {
for(var i = 0; i < 4; i++) {
piece.blocks[i].x = positions[i][0];
piece.blocks[i].y = positions[i][1];
}
return true;
}
return false;
}
//tries to move a piece downward
//one grid unit, returns false
//if it can't, true if it can and does
function fall(piece, board) {
function onebelow(blk) {
return [blk.x, blk.y - 1];
}
var fell = checkandset(piece, map(onebelow, piece.blocks), board);
if(fell)
drawonboard(piece);
return fell;
}
//drops a piece as far down as it can
//go instantly, does not return a value
function plummet(piece, board) {
function setpos(blk, pos) {
blk.x = pos[0];
//adjusts for incorrect y depth
blk.y = pos[1] + 1;
}
var depth = 0;
while(true) {
function nbelow(blk) {
return [blk.x, blk.y - (depth + 1)];
}
var posset = map(nbelow, piece.blocks);
if(!checkposset(posset, board)) {
map(setpos, piece.blocks, posset);
break;
}
depth++;
}
drawonboard(piece);
}
//transfers a movable piece onto
//its final position on the board
function settle(piece, board) {
function mvtoboard(blk) {
board[blk.x][blk.y] = blk;
}
map(mvtoboard, piece.blocks);
}
//moves a piece in the horizontal plane
function shift(dist, piece, board) {
function hshift(blk) {
return [blk.x + dist, blk.y];
}
var shifted = checkandset(piece, map(hshift, piece.blocks), board);
if(shifted)
drawonboard(piece);
}
function shiftleft(piece, board) {
shift(-1, piece, board);
}
function shiftright(piece, board) {
shift(1, piece, board);
}
//rotates a piece 90 deg. clockwise
function rotatecw(piece, board) {
if(piece.type == SQUARE) //no need to rotate squares
return;
var aroundx = piece.center.x;
var aroundy = piece.center.y;
//check for center next to side
//which will prevent pieces from rotating
//this technique does not work flawlessly for straight pieces
if(((aroundx + 1 >= board_width) ||
(aroundx - 1 < 0)) && (piece.type != STRAIGHT))
aroundx = aroundx - 1 < 0 ? aroundx + 1 : aroundx - 1;
function rotcoord(blk) {
var relx = blk.x - aroundx;
var rely = blk.y - aroundy;
return [aroundx + rely, aroundy - relx];
}
var rotated = checkandset(piece, map(rotcoord, piece.blocks), board);
if(rotated)
drawonboard(piece);
}
//rotates a piece 90 deg. counterclockwise
function rotateccw(piece, board) {
function rotcoord(blk) {
var relx = blk.x - piece.center.x;
var rely = blk.y - piece.center.y;
return [piece.center.x - rely, piece.center.y + relx];
}
var rotated = checkandset(piece, map(rotcoord, piece.blocks), board);
if(rotated)
drawonboard(piece);
}
//checks the entire board for any
//completed lines and executes the
//given handler for it
function fulline(board, clearfn) {
var c;
var toclear = new Array();
for(var r = 0; r < board_height; r++) {
for(c = 0; c < board_width; c++) {
if(!board[c][r])
break;
}
if(c >= board_width) {
toclear.push(r);
}
}
clearfn(toclear, board);
}
function dropabove(row, board) {
//collect all blocks above row
function findblk(blk) {
return blk ? blk.y > row : false;
}
var allabove = rfilter(findblk, board);
//drop all by one unit
function boarddown(blk) {
board[blk.x][blk.y] = false;
blk.y -= 1;
board[blk.x][blk.y] = blk;
}
map(boarddown, allabove);
//draw the newly positioned blocks
translatePos(allabove, board_top, board_left, board_height);
}
function nextlevel() {
level++;
clearInterval(endgame);
endgame = setInterval("gameloop()", 1000/level_timing[level]);
}
function clearallines(rows, board) {
//clear from the top down
rows.sort(function(a,b){return b - a;}); //sort into reverse order
for(var r = 0; r < rows.length; r++)
clearline(rows[r], board);
}
function clearline(row, board) {
lines++;
updatelines(lines);
//remove blocks from board
for(var col = 0; col < board_width; col++) {
var blk = board[col][row]
board[col][row] = false;
document.body.removeChild(blk);
}
dropabove(row, board);
if(lines > 1 && lines % 10 == 0)
nextlevel();
}
//returns a random Piece object
function nextpiece() {
var p = random.nextInt(7);
return new Piece(p);
}
//tries to put a pieces blocks in their initial positions
//will return false if not possible, true otherwise
function initialplace(piece, board) {
//get initial positions for piece type
var blkpos = init_positions[piece.type];
//check that positions are available
if(!checkposset(blkpos, board))
return false;
function setblkpos(blk, pos) {
blk.x = pos[0];
blk.y = pos[1];
}
map(setblkpos, piece.blocks, blkpos);
drawonboard(piece);
return true;
}
//gets the next piece type,
//creates its blocks
//draws them in the preview box
//and returns the piece
function previewplace() {
//get next piece from random sequence
var piece = nextpiece();
//get preview positions for piece type
var blkpos = prev_positions[piece.type];
function blockcreate(pos) {
var blk = createblock(piece.type);
blk.x = pos[0];
blk.y = pos[1];
piece.addBlock(blk);
document.body.appendChild(blk);
}
//map creation function across initial positions
map(blockcreate, blkpos);
piece.center = piece.blocks[0];
drawinpreview(piece);
return piece;
}
function startgame() {
glbl_board = createboard();
current_piece = previewplace();
initialplace(current_piece, glbl_board);
next_piece = previewplace();
document.onkeydown = function(event) {
var code = window.event ? window.event.keyCode : event.which;
keyhandler(code);
};
function falloop() {
if(!fall(current_piece, glbl_board)) {
settle(current_piece, glbl_board);
fulline(glbl_board, clearallines);
current_piece = next_piece;
if(!initialplace(current_piece, glbl_board)) { //if this fails, the player loses
gameover();
return;
}
next_piece = previewplace();
}
}
gameloop = falloop;
endgame = setInterval("gameloop()", 1000/level_timing[0]);
}
function gameover() {
clearInterval(endgame);
alert("Game Over.");
}
function keyhandler(keycode) {
var fn = current_keymap[keycode];
if(fn)
fn(current_piece, glbl_board);
}
function updatelines(lines) {
var span = document.getElementById("lines");
var text = document.createTextNode("" + lines);
span.replaceChild(text, span.firstChild);
}