0.4.4 • Published 5 years ago

@jsgames/minesweeper v0.4.4

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

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.

ArgumentRequiredDefaultTypeDescription
sizefalse10numberSize of the board
levelfalsemediumGameLevelLevel to define mines count
WARNING winnerBoardfalseBoardSet 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.

ArgumentRequiredDefaultTypeDescription
type TfalsenullType definition of the fields
sizetruenumberSize of the board
fillWithfalsenullTValues 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.
ArgumentRequiredDefaultTypeDescription
eventTypetrueGameEventEvent name to suscribe
callbacktrueEventsCallbackCallback 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.

ArgumentRequiredDefaultTypeDescription
firstMovetrueMoveCoordinates 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.

ArgumentRequiredDefaultTypeDescription
movetrueMoveCoordinates 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]):

nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnull1nullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnullnullnull

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

0.4.4

5 years ago

0.4.3

5 years ago

0.4.2

5 years ago

0.4.1

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago