node package manager

@mitchallen/connection-grid-core

@mitchallen/connection-grid-core

Connection grid core

Version Coverage Status Version


Installation

You must use npm 2.7.0 or higher because of the scoped package name.

$ npm init
$ npm install @mitchallen/connection-grid-core --save

Usage

"use strict";

var gridFactory = require("@mitchallen/connection-grid-core"),
				  gridSquare = require('@mitchallen/grid-square')

var sourceGrid = gridSquare.create({ x: 5, y: 6 });

var _dirMap = { 
    "N": 0x010, 
    "S": 0x020, 
    "E": 0x040, 
    "W": 0x080 };

let _oppositeMap = { "E": "W", "W": "E", "N": "S", "S": "N" };

var cg = gridFactory.create({  
    grid: sourceGrid,     
    dirMap: _dirMap,
    oppositeMap: _oppositeMap 
})

Browser Usage:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Connection-Grid-Core Example</title>
    <meta name="description" content="Connection Grid Core Example">
    <!-- either cdn should work -->
    <!--
    <script src="https://cdn.rawgit.com/mitchallen/connection-grid-core/v0.1.10/dist/connection-grid-core.min.js"></script>
    -->
    <script src="https://unpkg.com/@mitchallen/connection-grid-core@0.1.10/dist/connection-grid-core.min.js"></script>
    <script src="https://unpkg.com/@mitchallen/grid-square@0.1.8/dist/grid-square.min.js"></script>
    <script>
      var factory = window.MitchAllen.ConnectionGridCore;
      var squareFactory = window.MitchAllen.GridSquare;
      console.log(factory);
      var xSize = 10, ySize = 5;
      var sourceGrid = squareFactory.create({ x: xSize, y: ySize });
      var _dirMap = { 
        "N": 0x010, 
        "S": 0x020, 
        "E": 0x040, 
        "W": 0x080 };
    let _oppositeMap = { "E": "W", "W": "E", "N": "S", "S": "N" };
    var cg = factory.create({ 
        grid: sourceGrid, 
        dirMap: _dirMap,
        oppositeMap: _oppositeMap 
      });
      console.log(cg); 
    </script>
  </head>
  <body>
    <h1>Connection Grid Core Example</h1>
  </body>
</html>

Modules

connection-grid-core@mitchallen/grid-core

Connection Grid Core generated by create

connection-grid-core-factory

A factory for generating connection grid core objects

External

@mitchallen/grid-core

Grid Core

connection-grid-core ⇐ @mitchallen/grid-core

Connection Grid Core generated by create

Extends: @mitchallen/grid-core

connection-grid-core.isDir(dir) ⇒ boolean

Returns true if string is found in DIR_MAP array.

Kind: instance method of connection-grid-core

Param Type Description
dir string A string representing a direction

Example (usage)

if(core.isDir("N")) ...

connection-grid-core.getOppositeDir(dir) ⇒ string

Returns opposite direction based on OPPOSITE array.

Kind: instance method of connection-grid-core

Param Type Description
dir string A string representing a direction

Example (usage)

core.getOppositeDir("N").should.eql("S");

connection-grid-core.getNeighbor(x, y, dir) ⇒ string

Returns the neighbor in a particular direction for a cell at x,y. This should be overriden by derived class

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
dir string A string representing a direction

Example (usage)

var neighbor = core.getNeighbor(1,2,"N");

connection-grid-core.getNeighborDirs(x, y)

Returns the neighbor directions for a cell at x,y. This should be overriden by derived class. Classic square grids ignore x and y, but other derived classes, like hexagon, may not.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

var neighbors = core.getNeighborDirs(1,2);

connection-grid-core.getShuffledNeighborDirs(x, y)

Returns a shuffled list of neighbors for a cell at x,y. Useful for generating random mazes.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

var neighbors = core.getShuffledNeighborDirs(1,2);

connection-grid-core.markVisited(x, y) ⇒ boolean

Marks a cell at x,y as visited.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

core.markVisited(1,2);

connection-grid-core.visited(x, y) ⇒ boolean

Returns true if a cell at x,y exists and it has been marked as visited.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

if(core.visited(x)) ...

connection-grid-core.mask(x, y) ⇒ boolean

Marks a cell at x,y as masked. Useful for maze generators to mark cells to skip

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

core.mask(1,2)

connection-grid-core.isMasked(x, y) ⇒ boolean

Returns true if a cell at x,y has been marked using mask.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

if(core.isMasked(1,2)) ...

connection-grid-core.hasConnections(x, y) ⇒ boolean

Returns true if a cell at x,y has connections.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate

Example (usage)

if(core.hasConnections(1,2)) ...

connection-grid-core.open(x, y, dir)

Maps a connection for a cell at x,y in a particular direction. Unlike connect a cell in the direction does not have to exist. Useful for mazes that need to open up border walls.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
dir string A string representing a direction

Example (usage)

core.open(0,0,"N");

connection-grid-core.connect(x, y, dir) ⇒ boolean

Maps a connection for a cell at x,y in a particular direction. Returns false if the cell in the target direction does not exist.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
dir string A string representing a direction

Example (usage)

if(core.connect(1,2,"N")) ...

connection-grid-core.connectUndirected(x, y, dir) ⇒ boolean

Maps a connection for a cell at x,y in a particular direction. Also maps a connection from the target cell back to the source cell. Returns false if the cell in the target direction does not exist.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
dir string A string representing a direction

Example (usage)

if(core.connectUndirected(1,2,"N")) ...

connection-grid-core.connects(x, y, dir) ⇒ boolean

Returns true if a cell connects to a neighbor cell in a particular direction. It does not matter if a the target cell exists such as when open maps a connection to a non-existant cell.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
dir string A string representing a direction

Example (usage)

if(core.connects(1,2,"N")) ...

connection-grid-core.connectsAny(x, y, list) ⇒ boolean

Returns true if a cell connects to a neighbor cell in any direction in the list.

Kind: instance method of connection-grid-core

Param Type Description
x number The x coordinate
y number The y coordinate
list array An array of strings that each represent a direction

Example (usage)

if(core.connectsAny(1,2,["N","W"]) ...

connection-grid-core-factory

A factory for generating connection grid core objects

connection-grid-core-factory.create(options) ⇒ connection-grid-core

Factory method that returns a connection grid core object. It takes one spec parameter that must be an object with named parameters.

Kind: static method of connection-grid-core-factory

Param Type Description
options Object Named parameters for generating a connection grid core
options.grid grid Grid based on @mitchallen/grid-core
options.dirMap dirMap Direction map containing bit map flags for directions
options.oppositeMap oppositeMap Opposite direction map

Example (Creating a connection-grid-core)

"use strict";
var gridFactory = require("@mitchallen/connection-grid-core"),
    gridSquare = require('@mitchallen/grid-square')
var sourceGrid = gridSquare.create({ x: 5, y: 6 });
var _dirMap = { 
    "N": 0x010, 
    "S": 0x020, 
    "E": 0x040, 
    "W": 0x080 };
let _oppositeMap = { "E": "W", "W": "E", "N": "S", "S": "N" };
var cg = gridFactory.create({  
    grid: sourceGrid,     
    dirMap: _dirMap,
    oppositeMap: _oppositeMap 
});

@mitchallen/grid-core

Grid Core

Kind: global external
See: @mitchallen/grid-core


Testing

To test, go to the root folder and type (sans $):

$ npm test

Repo(s)


Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.


Version History

Version 0.1.10

  • updated .npmignore

Version 0.1.9

  • integrated travis-ci and codecov.io
  • uses latest version of @mitchallen/shuffle
  • updated license to MIT
  • refactored test cases to bring code coverage to 100%

Version 0.1.8

  • corrected reference to derived class in documentation

Version 0.1.7

  • refactored documentation

Version 0.1.6

  • fixed issue with documentation

Version 0.1.5

  • fixed issue with documentation

Version 0.1.4

  • added web-server to npm scripts
  • added message in client example HTML to check JavaScript console

Version 0.1.3

  • fixed issue with documentation tag

Version 0.1.2

  • added open method
  • integrated jsdoc
  • updated documentation

Version 0.1.1

  • added browser example

Version 0.1.0

  • initial release