-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetris.js
190 lines (167 loc) · 5.23 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
var math = require('mathjs');
var mechanics = require('./modules/mechanics')
var pieces = require('./modules/pieces')
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
process.exit();
}
else {
switch (key.name) {
case 'up':
if (piece.id != 4) {
if (!mechanics.detectSpinCollision(piece, board)) {
piece = mechanics.spin(piece);
displayPieceOnBoard(board);
}
}
break;
case 'down':
step();
break;
case 'left':
if (!mechanics.detectCollision(piece, board, 'left')) {
piece.left -= 1;
displayPieceOnBoard(board);
}
break;
case 'right':
if (!mechanics.detectCollision(piece, board, 'right')) {
piece.left += 1;
displayPieceOnBoard(board);
}
break;
case "space":
while (piece.top > 0) {
dropPiece();
}
}
}
});
function setBoard() {
var board = math.zeros(28, 16);
//set up the board with a solid perimeter
board.forEach(function(value, index, matrix) {
if (index[1] == 0 || index[1] == 1 || index[1] == 2 || index[1] == 13 || index[1] == 14 || index[1] == 15) {
board._data[index[0]][index[1]] = 1;
}
if (index[0] > 23) {
board._data[index[0]][index[1]] = 1;
}
});
return board;
}
// displays the current board
function showBoard(theBoard) {
process.stdout.write('\033c');
for (var i = 0; i < theBoard._size[0]; i++) {
if (i > 2 && i < 25) {
console.log(theBoard._data[i].toString()
.replace(/,/g, " ")
.replace(/0/g, " ")
.replace(/2\s/g, "██")
.replace(/1\s?/g, "██")
.replace(/3\s/g, "\x1b[33m██\x1b[0m")
.replace(/4\s/g, "\x1b[34m██\x1b[0m")
.replace(/5\s/g, "\x1b[35m██\x1b[0m")
.replace(/6\s/g, "\x1b[36m██\x1b[0m")
.replace(/7\s/g, "\x1b[32m██\x1b[0m")
.replace(/8\s/g, "\x1b[31m██\x1b[0m")
);
}
}
}
// See if we can drop the piece, and return true if we did.
// Return false if the piece cannot go any lower.
function dropPiece() {
var hitSomething = mechanics.detectCollision(piece, board, 'down');
if (hitSomething == true) {
if (piece.top < 2) {
clearInterval(looper);
lockPiece();
showBoard(board);
console.log("GAME OVER, LOSER!");
}
else {
lockPiece();
checkLines();
spawnPiece();
}
}
else {
// Did not hit any other piece, or the bottom, so let's drop down
piece.top += 1;
}
return !hitSomething;
}
// Lock the piece in place on the main board
function lockPiece() {
piece.matrix.forEach(function(value, index, matrix) {
// console.log('value:', value, 'index:', index);
if (value == piece.id)
board = board.subset(math.index(index[0] + piece.top, index[1] + piece.left), piece.id);
});
}
function spawnPiece() {
piece = pieces.getNewPiece();
}
function checkLines() {
let lines = 0;
for (let i = piece.top; i < piece.top + 5; i++) {
if (i > 23) break;
if (board._data[i].indexOf(0) === -1) {
clearLine(board, i);
lines++;
}
}
calcPoints(lines);
}
function calcPoints(lines) {
if (lines === 1) {
gameScore += 100;
}
else {
gameScore += (lines * 200);
}
}
function clearLine(board, row) {
var temp = math.subset(board, math.index(math.range(0, row), math.range(3, 13)));
board.subset(math.index(math.range(1, row + 1), math.range(3, 13)), temp);
}
function displayPieceOnBoard() {
var tempBoard = board.clone();
// Copies a piece onto the board
piece.matrix.forEach(function(value, index, matrix) {
// console.log('value:', value, 'index:', index);
if (index[0] + piece.top >= 0 && value == piece.id) {
tempBoard.subset(math.index(index[0] + piece.top, index[1] + piece.left), piece.id);
}
});
showBoard(tempBoard);
console.log("score: ", gameScore);
console.log("level: ", level);
// console.log("interval: ", interval);
}
function step() {
if (dropPiece()) {
displayPieceOnBoard();
if (gameScore > (level * 1000)) {
interval /= 2;
looper = setInterval(step, interval)
level++;
}
}
}
// set the initial board
var board = setBoard();
var piece = pieces.getNewPiece();
var next = pieces.getNewPiece();
displayPieceOnBoard();
// This calls the step() function [to drop the current piece] each <interval> seconds
var interval = 600;
var looper = setInterval(step, interval);
var looping = true;
var gameScore = 0;
var level = 1;