-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
365 lines (306 loc) · 8.35 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
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
'use strict';
const PIECES = [
/*0*/{ clr: '#eaeaea' }, // background
/*I*/{ clr: '#00bfb2', blk: [[-1,+0], [+0,+0], [+1,+0], [+2,+0]] },
/*O*/{ clr: '#f7f052', blk: [[+0,+0], [+1,+0], [+1,+1], [+0,+1]] },
/*T*/{ clr: '#6f2dbd', blk: [[+0,+0], [-1,+0], [+0,+1], [+1,+0]] },
/*J*/{ clr: '#2191fb', blk: [[-1,+1], [-1,+0], [+0,+0], [+1,+0]] },
/*L*/{ clr: '#f6511d', blk: [[-1,+0], [+0,+0], [+1,+0], [+1,+1]] },
/*S*/{ clr: '#7ac74f', blk: [[-1,+0], [+0,+0], [+0,+1], [+1,+1]] },
/*Z*/{ clr: '#e23863', blk: [[-1,+1], [+0,+1], [+0,+0], [+1,+0]] },
];
function pick_piece() {
return Math.trunc((PIECES.length - 1) * Math.random()) + 1;
}
// See https://tetris.wiki/SRS#How_Guideline_SRS_Really_Works
function offset_tbl(piece_name, state) {
switch (piece_name) {
case 1: // I
return [
[[+0,+0], [-1,+0], [+2,+0], [-1,+0], [+2,+0]],
[[-1,+0], [+0,+0], [+0,+0], [+0,+1], [+0,-2]],
[[-1,+1], [+1,+1], [-2,+1], [+1,+0], [-2,+0]],
[[+0,+1], [+0,+1], [+0,+1], [+0,-1], [+0,+2]],
][state];
break;
case 2: // O
return [
[[+0,+0]],
[[+0,-1]],
[[-1,-1]],
[[-1,+0]],
][state];
break;
default:
return [
[[+0,+0], [+0,+0], [+0,+0], [+0,+0], [+0,+0]],
[[+0,+0], [+1,+0], [+1,-1], [+0,+2], [+1,+2]],
[[+0,+0], [+0,+0], [+0,+0], [+0,+0], [+0,+0]],
[[+0,+0], [-1,+0], [-1,-1], [+0,+2], [-1,+2]],
][state];
break;
}
}
function get_offsets(piece_name, prev_st, next_st) {
let r = []; // return val
const ps = offset_tbl(piece_name, prev_st);
const ns = offset_tbl(piece_name, next_st);
for (let i = 0; i < ns.length; ++i) {
let [px, py] = ps[i];
let [nx, ny] = ns[i];
r[i] = [px - nx, py - ny];
}
return r;
}
class Field {
constructor(id, [width, height]) {
this.width = width;
this.height = height;
this.cnv = document.getElementById(id);
this.ctx = this.cnv.getContext('2d', { alpha: false });
this.field = new Array(width).fill(0)
.map(() => new Uint8Array(height).fill(0));
this.reset_dims();
}
reset_dims() {
let winRatio = window.innerWidth / window.innerHeight;
let fieldRatio = this.width / this.height;
let w =
winRatio >= fieldRatio
? window.innerHeight / this.height
: window.innerWidth / this.width
this.blk_width = Math.trunc(.99 * w);
this.cnv.width = this.blk_width * this.width;
this.cnv.height = this.blk_width * this.height;
}
redraw() {
this.field
.forEach((a, x) =>
a.forEach((c, y) => this.coords_fill([[x,y]], c)));
}
clear() {
this.field = this.field.map((x) => x.fill(0));
this.ctx.fillStyle = PIECES[0].clr;
this.ctx.fillRect(0, 0, this.cnv.width, this.cnv.height);
}
coord_free([x, y]) {
return x >= 0 && x < this.width
&& y >= 0 && y < this.height
&& this.field[x][y] === 0;
}
coords_free(cs) {
return cs.every(p => this.coord_free(p));
}
coord_to_px([x, y]) {
return [
this.blk_width * x,
this.blk_width * (this.height - (y+1)),
];
}
coords_fill(coords, name) {
this.ctx.fillStyle = PIECES[name].clr;
for (let [x, y] of coords.map(c => this.coord_to_px(c)))
this.ctx.fillRect(
x, y,
this.blk_width, this.blk_width
);
}
coords_set(coords, name) {
this.coords_fill(coords, name);
for (const [x, y] of coords)
this.field[x][y] = name;
}
}
class Piece {
constructor(blk, [x, y], state = 0) {
const coords =
blk.map(([dx, dy]) => [x + dx, y + dy]);
Object.defineProperties(this, {
blk: { writable: false, value: blk },
x: { writable: false, value: x },
y: { writable: false, value: y },
st: { writable: false, value: state },
coords: { writable: false, value: coords },
});
}
static rotr(p) {
return new Piece(
p.blk.map(([x, y]) => [y, -x]),
[p.x, p.y],
p.st < 3 ? p.st+1 : 0
)
}
static rotl(p) {
return new Piece(
p.blk.map(([x, y]) => [-y, x]),
[p.x, p.y],
p.st > 0 ? p.st-1 : 3
)
}
static trans(p, [dx, dy]) {
return new Piece(
p.blk,
[p.x + dx, p.y + dy],
p.st
);
}
}
function sleep(ms) {
return new Promise(r => setTimeout(r, ms));
}
class State {
constructor(id, dims) {
this.name = null; // piece name
this.p = null; // piece
this.onLoss = () => {};
this.field = new Field(id, dims);
}
rst() {
this.field.clear();
this.new_pc();
}
grav() {
if (!this.drop())
this.next_pc();
}
hard_drop() {
while (this.drop()); // null statement
this.next_pc();
}
next_pc() { this.set_pc(); this.new_pc(); }
drop() {
const n = Piece.trans(this.p, [0, -1]).coords;
if (this.field.coords_free(n)) {
this.trans([0, -1]);
return true;
}
return false;
}
new_pc() {
this.name = pick_piece();
this.p = new Piece(PIECES[this.name].blk, [4, this.field.height - 2]);
if (!this.field.coords_free(this.p.coords)) {
this.onLoss()
return;
}
this.field.coords_fill(this.p.coords, this.name);
}
set_pc() {
this.field.coords_set(this.p.coords, this.name);
let cleared = this.p.coords
.map(([_, y]) => y)
.filter(y => {
for (let x = 0; x < this.field.width; ++x)
if (this.field.coord_free([x, y]))
return false;
return true;
})
.sort((a, b) => b - a); // reverse sort
for (const y of new Set(cleared)) // uniq
this.clear_row(y)
}
clear_row(y) {
for (; y < this.field.height - 1; ++y)
for (let x = 0; x < this.field.width; ++x)
if (this.field.field[x][y] !== this.field.field[x][y+1])
this.field.coords_set(
[[x, y]],
this.field.field[x][y+1]
);
}
reload() {
this.field.reset_dims();
this.field.redraw();
this.field.coords_fill(this.p.coords, this.name);
}
_mv(f) {
this.field.coords_fill(this.p.coords, 0);
this.p = f(this.p);
this.field.coords_fill(this.p.coords, this.name);
}
trans([dx, dy]) {
this._mv((p) => {
let np = Piece.trans(p, [dx, dy]);
if (this.field.coords_free(np.coords))
return np;
else
return p;
});
}
dorot(f) {
this._mv((p) => {
let np = f(p)
let offs =
get_offsets(this.name, p.st, np.st)
.map(off => Piece.trans(np, off));
for (const x of offs)
if (this.field.coords_free(x.coords))
return x;
return p;
});
}
rotr() { this.dorot((p) => Piece.rotr(p)); }
rotl() { this.dorot((p) => Piece.rotl(p)); }
}
function run(st) {
let keyDown = {};
let gId = null;
let pause = null;
let start = () => { pause = null; gId = setInterval(() => st.grav(), 1000); }
let stop = () => { pause = true; clearInterval(gId); }
let doTrans = (delay, i, trans) => {
if (keyDown[i]) return;
keyDown[i] = true;
(function f() {
if (keyDown[i]) {
trans();
setTimeout(f, delay);
}
})();
}
let keydownCB = (e) => {
switch (e.key) {
case ' ' : st.hard_drop(); break;
case 'ArrowUp' :
case 'x' : st.rotr(); break;
case 'z' : st.rotl(); break;
case 'ArrowLeft' :
doTrans(110, e.key, () => st.trans([-1,+0]));
keyDown['ArrowRight'] = false;
break;
case 'ArrowRight':
doTrans(110, e.key, () => st.trans([+1,+0]));
keyDown['ArrowLeft'] = false;
break;
case 'ArrowDown' : doTrans(65, e.key, () => st.grav()); break;
case 'p' :
pause ? start() : stop();
console.log(`paused: ${!!pause}`)
break;
}
};
let keyupCB = (e) => {
keyDown[e.key] && (keyDown[e.key] = false);
};
st.onLoss = () => {
clearInterval(gId);
document.removeEventListener('keydown', keydownCB);
document.removeEventListener('keyup', keyupCB);
}
document.addEventListener('keydown', keydownCB);
document.addEventListener('keyup', keyupCB);
st.rst();
start();
}
function main(id) {
let st = new State(id, [10, 20]);
window.onload = window.onresize = () => st.reload();
document.addEventListener('keydown', (e) => {
if (e.key === 'r') {
st.onLoss(); // clean up
run(st);
}
});
// Sets callbacks and returns
run(st);
}