Skip to content

semibran/tetromino

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tetromino

The seven tetrominos

Model tetrominos using minimal data structures, especially for use in Tetris clones.

var tetromino = {
  type: 'L',
  position: [3, 0],
  rotation: 0
}

usage

NPM

const { types, states, blocks } = require('tetromino')

blocks(tetromino)

A tetromino can be represented as an object with the three fields type, rotation, and position. From this information, we can easily infer the location of each of the four cells that the tetromino occupies.

> blocks(tetromino)
[ [ 3, 1 ], [ 4, 1 ], [ 5, 1 ], [ 5, 0 ] ]

type

The type of a tetromino is a single character denoting its unique name. The image at the top of this page lists the tetrominos in the order [I, J, L, O, S, T, Z] - in fact, this particular array is exposed as types:

> types
[ 'I', 'J', 'L', 'O', 'S', 'T', 'Z' ]

A common use case for this array is choosing a random tetromino to spawn.

> types[Math.floor(Math.random() * types.length)]
'S'

position

The position of a tetromino is a two-dimensional vector of the form [x, y]. Since the center point of each tetromino will vary, position is relative to the top-left point of the 4x4 region containing the piece.

S tetromino demonstrating position

rotation

A tetromino's rotation is a zero-based index corresponding to a "rotation state" found in states. Most tetrominos have 4 rotation states, with the O piece being the exception. It only has one state and therefore cannot rotate.

> states.T.length
4

> states.O.length
1

J tetromino demonstrating rotation

states[type][rotation]

The states object details the locations of each cell of a tetromino with a given type and rotation in compliance with the SRS. It is used internally in the blocks function mentioned above.

Use states by specifying both a tetromino type and a rotation index.

> states.J[0]
[ [ 0, 1 ], [ 1, 1 ], [ 2, 1 ], [ 2, 0 ] ]

see also

license

MIT © Brandon Semilla

About

model tetrominos using minimal data structures

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published