0.0.6 • Published 8 years ago
tetrisjs v0.0.6
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.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
Files
tetris.js: logic to 'mutate' boards and blocksexample.js: shows how to usetetris.jsblocks.js: the different types of tetris blocks2dArrayUtils.js: common low-level functions to 'mutate' boards and blocks