https://github.com/akmjenkins/2048
Functional Implementation of 2048 game (for fun!)
https://github.com/akmjenkins/2048
Last synced: about 1 month ago
JSON representation
Functional Implementation of 2048 game (for fun!)
- Host: GitHub
- URL: https://github.com/akmjenkins/2048
- Owner: akmjenkins
- Created: 2020-04-05T00:08:57.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:46:30.000Z (over 2 years ago)
- Last Synced: 2025-02-03T18:15:31.587Z (3 months ago)
- Language: JavaScript
- Size: 1.22 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## 2048
[](https://coveralls.io/github/akmjenkins/2048)
[](https://travis-ci.com/akmjenkins/2048)A functional implementation of (a scalable version of) them [2048](https://play2048.co/) game.
By default this implementation follows 2048 exactly - a 4x4 board is created with a 2 or 4 entered in two randomly chosen blocks. Moves can then be performed on a board using [move](#move).
It can be scaled to any size with options.
## Why?
For fun.
Implementation is just 1kb minified and gzipped and consists of 2 pure functions: [createBoard](#createBoard) and [move](#move).
## API
### createBoard
```js
createBoard(boardSize?: number = 4, startWith?: number = 2): Board`
```Creates a 2048 game board (square) of the optionally given size with the optionally
given number of squares### move
```js
move(board: Board, direction: 'UP' | 'DOWN' | 'LEFT' | 'RIGHT', options?: MoveOptions): MoveResult// MoveOptions
{
numbers?: number[] = [2,4],
winAt?: 2048
}// MoveResult
{
board: Board;
score: number;
won: boolean;
}
```Performs a move on the given board and returns a the next board. Optionally accepts which new numbers could be generated into an empty square.
Return value is an object containing the next board, the points for the move, and whether the move resulted in a win based on the options supplied to the move.
Throws a game over error if no new moves remain.