tetrisjs
v0.0.6
Published
Functional tetris logic
Downloads
10
Readme
tetrisjs
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
Structure
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
)
Usage
const {
blocks,
createBoard,
printBoard,
canAddBlockToBoard,
addBlockToBoard,
rotateBlock,
changeCell,
mapCells,
getCompletedRows,
} = require('./tetris') ;
blocks
: exposes tetris blocks defined inblocks.js
createBoard(rows, cols): board
: create a board with size ofrows
xcols
printBoard(board): board
:console.log
sboard
canAddBlockToBoard(board, block, row, col): boolean
: canblock
be added toboard
at positionrow
,col
?addBlockToBoard(board, block, row, col): board
: create new board withblock
added toboard
at positionrow
,col
rotateBlock(block): block
: creates a newblock
turned clockwisechangeCell(board, row, col, cell): board
: create new board withcell
added toboard
at positionrow
,col
mapCells(board, callback): board
: create new board by applyingcallback
on each cell inboard
getCompletedRows(board): indexes
: returns an array of row indexes that are complete for givenboard
Files
tetris.js
: logic to 'mutate' boards and blocksexample.js
: shows how to usetetris.js
blocks.js
: the different types of tetris blocks2dArrayUtils.js
: common low-level functions to 'mutate' boards and blocks