npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@firefly13/arcade-core

v0.2.0

Published

Pure game logic for 2048: move, spawn, hasMoves, canMove.

Readme

@firefly13/arcade-core

Pure game logic for 2048, written in plain JavaScript.

Every function takes a board and returns a new board or a boolean. Nothing is changed in place, and nothing touches the DOM, a framework, or the browser, so it runs anywhere JavaScript runs: Node, the browser, or a test runner.

Install

npm install @firefly13/arcade-core

Requires Node 18 or later. The package is an ES module.

Quick start

import { move, scoreMove, spawn, canMove, hasMoves } from '@firefly13/arcade-core';

let board = [
	[2, 0, 2, 4],
	[0, 0, 0, 0],
	[0, 4, 0, 4],
	[0, 0, 0, 0]
];
let score = 0;

if (canMove(board, 'left')) {
	score += scoreMove(board, 'left'); // 4 + 8 = 12
	board = move(board, 'left');
	board = spawn(board);
}

if (!hasMoves(board)) {
	console.log('Game over');
}

The board

A board is an array of rows. Each row is an array of numbers, and 0 means an empty cell.

const board = [
	[2, 0, 0, 0], // top row
	[0, 0, 0, 0],
	[0, 0, 4, 0],
	[0, 0, 0, 0] // bottom row
];

board[row][column] is the tile at that position, so board[2][2] is 4 above.

A direction is one of the strings 'left', 'right', 'up' or 'down'.

API

move(board, direction)

Slides every tile as far as it can go in direction, merges equal neighbours, and returns the new board.

Each tile merges at most once per move, and tiles closest to the edge you are moving towards merge first:

move([[2, 2, 2, 2], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'left')[0];
// [4, 4, 0, 0]   (not [8, 0, 0, 0])

move([[4, 4, 8, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'left')[0];
// [8, 8, 0, 0]   (the new 8 does not merge again)

move([[2, 2, 2, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], 'right')[0];
// [0, 0, 2, 4]

Throws an Error if direction is not one of the four directions.

scoreMove(board, direction)

Returns the points that moving in direction would earn, without changing the board. Every merge earns the value of the new tile, so two 2s merging into a 4 earn 4 points. A move with no merges earns 0.

Call it with the board before you move it:

const board = [
	[2, 2, 4, 4],
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0]
];

scoreMove(board, 'left'); // 12   (2 + 2 → 4, and 4 + 4 → 8)
scoreMove(board, 'up'); // 0    (nothing merges vertically)

It follows the same merge rules as move, so [2, 2, 2, 2] earns 8 (two 4s) and [4, 4, 8, 0] earns 8 (the new 8 does not merge again).

Throws an Error if direction is not one of the four directions.

Added in 0.2.0.

spawn(board, rng = Math.random)

Puts a new tile in a random empty cell and returns the new board. The new tile is a 2 or a 4, each with a 50% chance. If the board has no empty cells, it returns an unchanged copy.

spawn([
	[2, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 4]
]);
// one of the 14 empty cells now holds a 2 or a 4

rng is an optional function that returns a number from 0 (inclusive) to 1 (exclusive), like Math.random. Pass your own to control where tiles appear, for example with a seeded random number generator so a game can be replayed. spawn calls it exactly twice:

  1. Which cell: the empty cells are listed row by row, starting at the top left, and the one at index Math.floor(rng() * emptyCells.length) is chosen.
  2. Which value: 2 if rng() < 0.5, otherwise 4.
const values = [0, 0.7];
const fakeRng = () => values.shift();

spawn(
	[
		[2, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0],
		[0, 0, 0, 0]
	],
	fakeRng
);
// [[2, 4, 0, 0], ...]   first empty cell, value 4

canMove(board, direction)

Returns true if moving in direction would change the board, and false if nothing would move or merge. In a game, only a move that changes the board should spawn a new tile.

const board = [
	[2, 4, 8, 16],
	[0, 0, 0, 0],
	[0, 0, 0, 0],
	[0, 0, 0, 0]
];

canMove(board, 'left'); // false
canMove(board, 'down'); // true

hasMoves(board)

Returns true if any of the four directions would still change the board, and false when the game is over.

hasMoves([
	[2, 4, 2, 4],
	[4, 2, 4, 2],
	[2, 4, 2, 4],
	[4, 2, 4, 2]
]);
// false

Development

npm test

Runs the tests in test/ with Node's built-in test runner. There are no dependencies to install.

License

MIT