diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 15:25:06 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 15:25:06 +0000 |
| commit | 9f83ab70cfd5f2c41a23593f8ee5065b1015e242 (patch) | |
| tree | d5bc21382cf3e5e38db6af5ab0f6e2ad5ef37c86 /frontend/src/util | |
| parent | ef7a5e4cc01cfdc4a2594cb408a1dfd889ebb51f (diff) | |
Add frontend
Diffstat (limited to 'frontend/src/util')
| -rw-r--r-- | frontend/src/util/tetris.ts | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/frontend/src/util/tetris.ts b/frontend/src/util/tetris.ts new file mode 100644 index 0000000..3547108 --- /dev/null +++ b/frontend/src/util/tetris.ts @@ -0,0 +1,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; + }); + }); +} |
