Functional tetris
A functional (pure, side-effect free) tetris logic libary.
NOTE: warning I've spent only a couple hours on this so far:
- likely to change
- no tests
- sparse example
Works with 2 dimensional arrays ([0][0] = top left) to represent:
-
the
board[rows][cols]: each cell is a number which specifies a color (specific to each type of block) -
a
block[rows][cols]: a tetris block (see./blocks.js)
const {
blocks,
createBoard,
printBoard,
canAddBlockToBoard,
addBlockToBoard,
rotateBlock,
changeCell,
mapCells,
getCompletedRows,
} = require('./tetris') ;
blocks: exposes tetris blocks defined inblocks.jscreateBoard(rows, cols): board: create a board with size ofrowsxcolsprintBoard(board): board:console.logsboardcanAddBlockToBoard(board, block, row, col): boolean: canblockbe added toboardat positionrow,col?addBlockToBoard(board, block, row, col): board: create new board withblockadded toboardat positionrow,colrotateBlock(block): block: creates a newblockturned clockwisechangeCell(board, row, col, cell): board: create new board withcelladded toboardat positionrow,colmapCells(board, callback): board: create new board by applyingcallbackon each cell inboardgetCompletedRows(board): indexes: returns an array of row indexes that are complete for givenboard
-
tetris.js: logic to 'mutate' boards and blocks -
example.js: shows how to usetetris.js -
blocks.js: the different types of tetris blocks -
2dArrayUtils.js: common low-level functions to 'mutate' boards and blocks