-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
130 lines (118 loc) · 2.88 KB
/
game.ts
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
import { GamePiece, Player } from "./player.ts";
const pieces = "ILJOTSZ";
let bagOfPieces: string[] = [];
export function collide(gameArena: number[][], piece: GamePiece): boolean {
const [m, o] = [piece.matrix, piece.pos];
for (let y = 0; y < m.length; y++) {
for (let x = 0; x < m[y].length; x++) {
if (
m[y][x] !== 0 &&
(gameArena[y + o.y] && gameArena[y + o.y][x + o.x]) !== 0
) {
return true;
}
}
}
return false;
}
export function merge(gameArena: number[][], currPlayer: Player): void {
currPlayer.matrix.forEach((row, y) => {
row.forEach((value, x) => {
if (value !== 0) {
gameArena[y + currPlayer.pos.y][x + currPlayer.pos.x] = value;
}
});
});
}
export let nextPiece: number[][];
export function playerReset(arena: number[][], currPlayer: Player): number[][] {
if (nextPiece == null) {
nextPiece = generateNextPiece();
}
currPlayer.matrix = nextPiece;
currPlayer.pos.y = 0;
currPlayer.pos.x = ((arena[0].length / 2) | 0) -
((currPlayer.matrix[0].length / 2) | 0);
if (collide(arena, currPlayer)) {
arena.forEach((row) => row.fill(0));
currPlayer.score = 0;
}
nextPiece = generateNextPiece();
return nextPiece;
}
function generateNextPiece(): number[][] {
if (bagOfPieces.length === 0) {
bagOfPieces = [...pieces];
}
const pieceIndex = (bagOfPieces.length * Math.random()) | 0;
const newPiece = createPiece(bagOfPieces[pieceIndex]);
bagOfPieces.splice(pieceIndex, 1);
return newPiece;
}
// Check for completed rows
export function scanArena(arena: number[][], currPlayer: Player): void {
let rowCount = 1;
for (let y = arena.length - 1; y >= 0; y--) {
// Look for a 0 in the row, indicating that the row is incomplete
const completedRow = !arena[y].includes(0);
if (completedRow) {
const row = arena.splice(y, 1)[0].fill(0);
arena.unshift(row);
y++;
currPlayer.score += rowCount * 10;
rowCount *= 2;
}
}
}
export function generateShadow(
arena: number[][],
currPlayer: Player,
): GamePiece {
const shadow = {
pos: { x: currPlayer.pos.x, y: currPlayer.pos.y },
matrix: currPlayer.matrix,
};
while (!collide(arena, shadow)) {
shadow.pos.y++;
}
shadow.pos.y--;
return shadow;
}
function createPiece(type: string): number[][] {
if (type === "T") {
return [
[1, 1, 1],
[0, 1, 0],
];
} else if (type === "O") {
return [
[2, 2],
[2, 2],
];
} else if (type === "L") {
return [
[3, 0],
[3, 0],
[3, 3],
];
} else if (type === "J") {
return [
[0, 4],
[0, 4],
[4, 4],
];
} else if (type === "I") {
return [[5], [5], [5], [5]];
} else if (type === "S") {
return [
[0, 6, 6],
[6, 6, 0],
];
} else if (type === "Z") {
return [
[7, 7, 0],
[0, 7, 7],
];
}
return [];
}