diff options
| author | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 17:20:13 +0000 |
|---|---|---|
| committer | Leonardo Bishop <me@leonardobishop.com> | 2023-11-04 17:20:13 +0000 |
| commit | e1633f3348ff7fc5e9131eeae2f2feba09f04838 (patch) | |
| tree | 43b4f4eec439b801b42c0d112f1c68efecbbcefc /frontend/src/util | |
| parent | 9f83ab70cfd5f2c41a23593f8ee5065b1015e242 (diff) | |
More shit
Diffstat (limited to 'frontend/src/util')
| -rw-r--r-- | frontend/src/util/tetris.ts | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/frontend/src/util/tetris.ts b/frontend/src/util/tetris.ts index 3547108..6f9d827 100644 --- a/frontend/src/util/tetris.ts +++ b/frontend/src/util/tetris.ts @@ -1,5 +1,5 @@ import type { Board } from "@/model/board"; -import type { Tetromino } from "@/model/tetrominoes"; +import { allTetrominoes, type Tetromino } from "@/model/tetrominoes"; export function mergeTetrominoWithBoard(board: Board, tetromino: Tetromino): Board { return board.map((row, rowIndex) => { @@ -38,3 +38,21 @@ export function tetrominoCollidesWithBoard(board: Board, tetromino: Tetromino): }); }); } + +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; +}
\ No newline at end of file |
