blob: 6f9d8276c28f020636f37d9ecda256f8ddb3bf84 (
plain)
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
|
import type { Board } from "@/model/board";
import { allTetrominoes, type Tetromino } from "@/model/tetrominoes";
export function mergeTetrominoWithBoard(board: Board, tetromino: Tetromino): Board {
return board.map((row, rowIndex) => {
if (rowIndex >= tetromino.row && rowIndex < tetromino.row + tetromino.shapes[tetromino.rotation].length) {
return row.map((col, colIndex) => {
if (colIndex >= tetromino.col && colIndex < tetromino.col + tetromino.shapes[tetromino.rotation][0].length) {
return tetromino.shapes[tetromino.rotation][rowIndex - tetromino.row][colIndex - tetromino.col] || col;
}
return col;
});
}
return row;
});
}
export function tetrominoCollidesWithBoard(board: Board, tetromino: Tetromino): boolean {
return tetromino.shapes[tetromino.rotation].some((row, rowIndex) => {
return row.some((col, colIndex) => {
if (col) {
const boardRow = board[rowIndex + tetromino.row];
if (!boardRow) {
return true;
}
const boardCol = boardRow[colIndex + tetromino.col];
if (boardCol) {
return true;
}
if (colIndex + tetromino.col < 0 || colIndex + tetromino.col >= boardRow.length) {
return true;
}
if (rowIndex + tetromino.row < 0 || rowIndex + tetromino.row >= board.length) {
return true;
}
}
return false;
});
});
}
export function randomiseNextTetrominoes(): Array<Tetromino> {
const tetrominoes = Object.values(allTetrominoes);
let currentIndex = tetrominoes.length,
randomIndex;
while (currentIndex > 0) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
[tetrominoes[currentIndex], tetrominoes[randomIndex]] = [
tetrominoes[randomIndex],
tetrominoes[currentIndex],
];
}
return tetrominoes;
}
|