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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jsgames/minesweeper

v0.4.4

Published

JS Engine for Minesweeper game

Downloads

2

Readme

Minesweeper

JS Engine for Minesweeper game. Powered by JS with Typescript and 0 dependencies. It can be implemented in any UI engine with JS support like React, React-Native, Vue, Angular, Electron, even CLI.

A CLI built in coming soon

Go ahead and include it in your proyect as something funny for your users. Is MIT licenced.

Install

yarn add @jsgames/minesweeper

It include its own type definition, you don't need to install something like @types/...

Usage

import Minesweeper, { Board } from '@jsgames/minesweeper';

// Get an empty board to draw in my UI
const emptyBoard = Minesweeper.emptyBoard<null>(10, null);

// create a new instance for the game
const game = new Minesweeper();

// start new game with a new position (This is to avoid click a bomb in the first move)
let board: Board = game.newGame([4, 5]);

// get the game status
let gameStatus = game.status; // active

// make a move and update the board and the game status
board = game.reveal([5, 5]);
gameStatus = game.status; // Lets assume you click a bomb, then the status will be loose

Also for more responsive behavior listeners can be setted

import Minesweeper, { Board, GameStatus } from '@jsgames/minesweeper';

const onBoard = (board: Board) => updateTheBoard(board);
const onGame = (status: GameStatus) => updateGameStatus(status);
const onError = (error: Error) => handleErrors(error);

const game = new Minesweeper();

// If a new board is created, it dispatch with the last board
game.on('board', onBoard);
// If the game status change, it dispatch with the last game status
game.on('game', onGame);
// If the an error happen in the game, it dispatch the error
game.on('error', onError);

API

class Minesweeper([size = 10, level = 'medium']): Minesweeper

Class of the game engine, return a Minesweeper instance.

|Argument|Required|Default|Type|Description |---|:-:|:-:|:-:|---| |size|false|10|number|Size of the board| |level|false|medium|GameLevel|Level to define mines count| |WARNING winnerBoard|false||Board|Set a winner board for experimental uses, can be get by createWinnerBoard().board|

Minesweeper.emptyBoard<[T]>(size[, fillWith]): T[][]

Static method to create an empty board with a given fill and type. Returns a Board like but with a specific type.

|Argument|Required|Default|Type|Description |---|:-:|:-:|:-:|---| |type T|false|null||Type definition of the fields| |size|true||number|Size of the board| |fillWith|false|null|T|Values to fill with|

Minesweeper.prototype.board: Board

Property to get the last board of the current game. Return Board.

Minesweeper.prototype.status: GameStatus

Property to get the current game status. Return GameStatus.

Minesweeper.prototype.on(eventType: GameEvent, callback: EventsCallback): void

Instance method to suscribe an event listener. Those events could be:

  • 'board': Event when the current game board change.
  • 'game': Event when the game status change.
  • 'error': Event when an error is throw.

|Argument|Required|Default|Type|Description |---|:-:|:-:|:-:|---| |eventType|true||GameEvent|Event name to suscribe| |callback|true||EventsCallback|Callback function to execute once the event is fired|

Minesweeper.prototype.newGame(firstMove: Move): Board

Instance method to create a new game with a first revel to avoid press a bomb in the first move by accident. It return a new Board with the move revealed. And emit the 'board' and 'game' events.

|Argument|Required|Default|Type|Description |---|:-:|:-:|:-:|---| |firstMove|true||Move|Coordinates [x, y] of the first move|

Minesweeper.prototype.reveal(move: Move): Board

Instance method to reveal a specific point in the game board, if the point is a bomb, the game event will be called with 'loose' and the method will return the Board resolved; if the point is a 0 the method will return the Board with all the values around the 0 and recursiveley.

|Argument|Required|Default|Type|Description |---|:-:|:-:|:-:|---| |move|true||Move|Coordinates [x, y] of the move|

Minesweeper.prototype.surrender(): Board

Instance method to surrender in the game, it will call the 'game' method with 'loose'. Also will return the Board resolved.

Types

Move

Type who define how a move should be: An array with x and y coordinates

[number, number]

Board

Type who define how a board should be: An 2D array containing number or null;

Array<Array<number | null>>

A board returned by a newGame() looks like (If the size is 10 and the first move is [3, 4]):

||||||||||| |:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:|:-:| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|1|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null| |null|null|null|null|null|null|null|null|null|null|

GameStatus

Type who define the different Game Status

'active' | 'loose' | 'win' | 'cheater'

GameEvent

Type who define the different Game Events

'board' | 'error' | 'game'

GameLevel

Type who define the different Game Levels

'hard' | 'medium' | 'easy'

EventsCallback

Type who define the different Events Callbacks

((board: Board) => void) | ((error: Error) => void) | ((status: GameStatus) => void)

Solution

Solution object to query result

interface {
  query: (x: number, y: number) => number;
  getSolution: () => Board;
}

Licence

MIT