-
Notifications
You must be signed in to change notification settings - Fork 53
/
board.js
169 lines (145 loc) · 3.86 KB
/
board.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
class Board {
constructor(ctx, ctxNext) {
this.ctx = ctx;
this.ctxNext = ctxNext;
this.init();
}
init() {
// Calculate size of canvas from constants.
this.ctx.canvas.width = COLS * BLOCK_SIZE;
this.ctx.canvas.height = ROWS * BLOCK_SIZE;
// Scale so we don't need to give size on every draw.
this.ctx.scale(BLOCK_SIZE, BLOCK_SIZE);
}
reset() {
this.grid = this.getEmptyGrid();
this.piece = new Piece(this.ctx);
this.piece.setStartingPosition();
this.getNewPiece();
}
getNewPiece() {
const { width, height } = this.ctxNext.canvas;
this.next = new Piece(this.ctxNext);
this.ctxNext.clearRect(0, 0, width, height);
this.next.draw();
}
draw() {
this.piece.draw();
this.drawBoard();
}
drop() {
let p = moves[KEY.DOWN](this.piece);
if (this.valid(p)) {
this.piece.move(p);
} else {
this.freeze();
this.clearLines();
if (this.piece.y === 0) {
// Game over
return false;
}
this.piece = this.next;
this.piece.ctx = this.ctx;
this.piece.setStartingPosition();
this.getNewPiece();
}
return true;
}
clearLines() {
let lines = 0;
this.grid.forEach((row, y) => {
// If every value is greater than zero then we have a full row.
if (row.every((value) => value > 0)) {
lines++;
// Remove the row.
this.grid.splice(y, 1);
// Add zero filled row at the top.
this.grid.unshift(Array(COLS).fill(0));
}
});
if (lines > 0) {
// Calculate points from cleared lines and level.
account.score += this.getLinesClearedPoints(lines);
account.lines += lines;
// If we have reached the lines for next level
if (account.lines >= LINES_PER_LEVEL) {
// Goto next level
account.level++;
// Remove lines so we start working for the next level
account.lines -= LINES_PER_LEVEL;
// Increase speed of game
time.level = LEVEL[account.level];
}
}
}
valid(p) {
return p.shape.every((row, dy) => {
return row.every((value, dx) => {
let x = p.x + dx;
let y = p.y + dy;
return value === 0 || (this.isInsideWalls(x, y) && this.notOccupied(x, y));
});
});
}
freeze() {
this.piece.shape.forEach((row, y) => {
row.forEach((value, x) => {
if (value > 0) {
this.grid[y + this.piece.y][x + this.piece.x] = value;
}
});
});
}
drawBoard() {
this.grid.forEach((row, y) => {
row.forEach((value, x) => {
if (value > 0) {
this.ctx.fillStyle = COLORS[value];
this.ctx.fillRect(x, y, 1, 1);
}
});
});
}
getEmptyGrid() {
return Array.from({ length: ROWS }, () => Array(COLS).fill(0));
}
isInsideWalls(x, y) {
return x >= 0 && x < COLS && y <= ROWS;
}
notOccupied(x, y) {
return this.grid[y] && this.grid[y][x] === 0;
}
rotate(piece, direction) {
// Clone with JSON for immutability.
let p = JSON.parse(JSON.stringify(piece));
if (!piece.hardDropped) {
// Transpose matrix
for (let y = 0; y < p.shape.length; ++y) {
for (let x = 0; x < y; ++x) {
[p.shape[x][y], p.shape[y][x]] = [p.shape[y][x], p.shape[x][y]];
}
}
// Reverse the order of the columns.
if (direction === ROTATION.RIGHT) {
p.shape.forEach((row) => row.reverse());
} else if (direction === ROTATION.LEFT) {
p.shape.reverse();
}
}
return p;
}
getLinesClearedPoints(lines, level) {
const lineClearPoints =
lines === 1
? POINTS.SINGLE
: lines === 2
? POINTS.DOUBLE
: lines === 3
? POINTS.TRIPLE
: lines === 4
? POINTS.TETRIS
: 0;
pointsSound.play();
return (account.level + 1) * lineClearPoints;
}
}