blob: 3547108eb3a161c42a8dc4dcb4ec8615d2622713 (
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
|
import type { Board } from "@/model/board";
import 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;
});
});
}
|