Git Product home page Git Product logo

jmatch3's Introduction

jMatch3

Javascript Match-3 Game Lib - Node.JS and Browser compatible

How to use ?

Create a new Grid with width, height and gravity parameters :

var grid = new jMatch3.Grid({
    width: 6,
    height: 7,
    gravity: "down"
});

Get a piece inside the grid

var piece = grid.getPiece({ x: 0, y: 0 });

Update a piece

A piece has a void object by default, but you can change it with your own

piece.object = { type: "gem" };-

Or revert to the void object

piece.clear();

The void Object type is "empty"

Display the grid (Debug)

You can log the grid with a map of symbols

grid.debug({
    empty: "-",
    gem: "g"
});

Handle matches

You can get all current matches

var matches = grid.getMatches();

Clear matches to transform all matching pieces object to void object

grid.clearMatches();

Apply gravity to fall down your pieces

grid.applyGravity();

API Documentation

Grid

/*
 * options:
 * - width (default 10)
 * - height (default 10)
 * - gravity (default false): "up", "right", "down", "left", or false 
 */
var grid = new jMatch3.Grid({
    width: 6,
    height: 7,
    gravity: "down"
});

Instance methods

.coordsInWorld(coords)

Return if given coords are in the grid

grid.coordsInWorld({ x: 10, y: 10 }); // return false
.getPiece(coords)

Return the piece from given coords

var piece = grid.getPiece({ x: 4, y: 4 });
.neighbourOf(piece, direction)

Return the piece neighbour of another piece from a given direction

var neighbour = grid.neighbourOf(piece, "left");
.neighboursOf(piece)

Return a Hash of pieces by direction

// return { up: theUpPiece, down: theDownPiece, right: theRightPiece, left: theLeftPiece }
var neighbours = grid.neighboursOf(piece);
.forEachMatch(callback)

Execute a callback for each current match

grid.forEachMatch(function() {
  // Your scoring stuff
});
.getMatches()

Return an array of matches or false

var matches = grid.getMatches();
.getRow(row, reverse)

Return an Array of pieces

var row = grid.getRow(0);
.getColumn(column, reverse)

Return an Array of pieces

var column = grid.getColumn(0);
.clearMatches()

Destroy all matches and update the grid

grid.clearMatches();
.swapPieces(piece1, piece2)

Swap 2 pieces object

grid.swapPieces(piece1, piece2);
.applyGravity()

Apply gravity to fall down your pieces and return an Array of falling pieces

var fallingPieces = grid.applyGravity();
.debug(symbols)

Log the current grid with symbols

grid.debug({
    empty: "-",
    gem: "g"
});

Class method

.getLastEmptyPiece()

Get last empty piece from an Array of pieces

var lastEmpty = jMatch3.Grid.getLastEmptyPiece(pieces);

Piece

Private Class

/*
 * Params:
 * - grid
 * - x
 * - y
 */
new Piece(grid, 0, 0);

Instance methods

.clear()

Replace the piece object by a the void object

piece.clear();
.relativeCoordinates(direction, distance)

Return relatives coordinates to the piece

var relativeCoordinates = piece.relativeCoordinates("right", 1); // return { x: 1, y: 0 }
.neighbour(direction)

Return neighbour of the piece from a given direction

var neighbour = piece.neighbour("right");
.neighbours()

Return a Hash of pieces by direction

// return { up: theUpPiece, down: theDownPiece, right: theRightPiece, left: theLeftPiece }
var neighbours = piece.neighbours();
.matchingNeighbours()

Return an Array of direct Matching Neighbours

var matchingNeighbours = piece.matchingNeighbours();
.deepMatchingNeighbours()

Return an Array of deep Matching Neighbours

var deepMatchingNeighbours = piece.deepMatchingNeighbours();

jmatch3's People

Contributors

hugeen avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.