aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'frontend/src/util')
-rw-r--r--frontend/src/util/tetris.ts40
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;
+ });
+ });
+}