Giter Site home page Giter Site logo

fixed-2d-array's Introduction

fixed-2d-array

NPM version Build Status devDependency Status Coverage Status Code Climate

A fixed size 2D array in JavaScript

This module gives you a two-dimensional array with a fixed size. The top left coordinate is (0,0).

Fixed2DArray(rows, columns, defaultValue)

rows is the number of rows the Fixed2DArray will start with, columns is the columns. During the creation of the array the defaultValue will be assigned to all elements.

validateCoords(row, col)

The validateCoords method checks if the given coordinates are valid. (lie inside of the array) If the coordinates are not valid an Error is thrown.

get(row, col)

Returns the value of the given coordinate. The coordinate is checked using validateCoords.

getRow(rowIndex)

Returns an array of the requested row.

getColumn(colIndex)

Returns an array of the requested column.

set(row, col, value)

Sets the value of the given coordinate to value. The coordinate is checked using validateCoords.

setRow(rowIndex, array)

Sets values of the given array as the values of the specified row.

pushRow([array1, array2, ..., arrayN])

Adds one or more arrays as rows to the bottom of the Fixed2DArray. Returns the new width of the Fixed2DArray.

Only arguments that are arrays will be appended as rows.

If the given array is smaller then the height of the Fixed2DArray, undefined will fill the given array until it is the same length as the current row.

setColumn(colIndex, array)

Sets values of the given array as the values of the specified column.

getHeight()

The number of rows corresponds with the height.

getWidth()

The number of columns corresponds to the width.

getNeighbours(row, col, [, distance])

Returns an array containing all values of the cells next to the given coordinate.

For example, distance not set:

[ ][ ][ ][ ][ ]
[ ][*][*][*][ ]
[ ][*][X][*][ ]
[ ][*][*][*][ ]
[ ][ ][ ][ ][ ]

The given coordinate is marked with an X. The function will return an array containing the values for the fields marked with an *.

Example, distance = 2:

[*][*][*][*][*]
[*][*][*][*][*]
[*][*][X][*][*]
[*][*][*][*][*]
[*][*][*][*][*]

The function will return an array containing the values for the fields marked with an '*'. Notice that distance will change what cells count as neighbors.

areNeighbours(row1, col1, row2, col2, [, distance])

Returns true if the given coordinates are neighbors, false otherwise.

The distance between each coordinate must be within distance or one unit away from each other for the given coordinates to be considered neighbors.

For example, distance not set:

   0  1  2 
0 [A][ ][ ]
1 [ ][B][ ]
2 [ ][ ][ ]
3 [ ][ ][ ]

The first given coordinate (0,0) is marked with an A, the second (1,1), a B. A and B are neighbors.

If A and B were instead placed at (0,0) and (2,2) respectively, like this:

   0  1  2 
0 [A][ ][ ]
1 [ ][ ][ ]
2 [ ][ ][B]
3 [ ][ ][ ]

A and B are no longer neighbors.

Example, distance = 2:

   0  1  2 
0 [A][ ][ ]
1 [ ][ ][ ]
2 [ ][ ][B]
3 [ ][ ][ ]

A (0,0) and B (2,2) are neighbors.

forEach(fn, [, thisArg])

Executes a provided function once per array element.

fn is the function to execute for each element, taking three arguments:

  • currentValue: The current element being processed in the array.
  • index: The object index, {x: x, y: y}, of the current element being processed in the 2d array.
  • array: The Fixed2DArray that forEach is being applied to.
  • thisArg: Optional. Value to use as this when executing callback.

fixed-2d-array's People

Contributors

possibly avatar tillarnold avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

possibly inexsu

fixed-2d-array's Issues

Current NPM release does not reflect current code

If this project isn't dead, there is a nasty bug with the constructor where the two dimension args are flipped (from node_modules/fixed-2d-array/lib/fixed-2d-array.js):

var Fixed2DArray = function Fixed2DArray(width, height, defaultValue) {
  this._width = width;
  this._height = height;
  this._grid = [];
  for (var i = 0; i < width; i++) {
    this._grid[i] = [];
    for (var j = 0; j < height; j++) {
      this._grid[i][j] = defaultValue;
    }
  }
};

Huh, height and width are mixed up.

I was showing how Fixed2DArray worked to a friend, and while demoing a Fixed2DArray of size (2,3) a friend point out something so obvious I hadn't seen it before:

var fa = new FixedArray(2,3,0);
console.log(fa._grid);
/*
[
[0, 0, 0],
[0, 0, 0],
]
*/

"Uhm, thats a grid with width three and height 2."

I think she's right, and it's a really simple fix: swap this._height and this._width. A few other things will need adjustment, but I think this change makes more sense in the long run.

Thoughts?

distance()

This is a feature request to add a distance function.

It calculates the distance between two points on the grid.

In my specific use case diagonal tiles are not bordering and should not be counted.

combine()?

So I am making this game (same one as before) and am still using Fixed2DArray. I would like to have my Fixed2DArray grow as the player 'explores' it, kinda like how minecraft generates new terrain as the player travels around the world. Arrays in Javascript have concat(), but i'd like Fixed2DArray to have a concat that works a bit differently. Im thinking about calling Fixed2DArray's concat()-equivalent combine().

For example:

var fixedArray = require('..');
var fa1 = new fixedArray(2,2,0);
var fa2 = new fixedArray(2,2,1);
var fa3 = fa1.combine(fa2);
console.log(fa3._grid); 

Output

[
[0, 0, 1, 1],
[0, 0, 1, 1]
]

HOWEVER, what if I wanted to combine fa2 to the top of fa1, as opposed to the right? Like so:

[
[1, 1],
[1, 1],
[0, 0],
[0, 0],
]

wowowowow.

Then using combine would like more like

var fa3 = fa1.combine(fa2, 'top'); 
//left, right, top, bottom

But what about fixedArrays of a different sizes? For example,

var fixedArray = require('..');
var fa4 = new fixedArray(2,2,4);
var smallerFA5 = new fixedArray(1,1,5);
var fa6 = fa4.combine(smallerFA5);
console.log(fa6._grid); 

What should fa6 be? Outputting a Fixed2DArray might be strange since the combination of a 2x2 Fixed2DArray and a 1x1 Fixed2DArray leads to a "Staggered2DArray", not a fixed2DArray!

Example of "Staggered2DArray":

[
[4, 4, 5]
[4, 4]
]

I have a few ideas, though I am not sure which one is best:

  1. Fixed2DArray's functions still work if the ._grid variable is technically "staggered." Therefore, the output of fa6 can just be a Fixed2DArray that has had its ._grid variable played with.

  2. Only allow combine() to "combine" Fixed2DArrays of the same height and width. I think this is how typical mathematics matrices work?

  3. Keep the ._grid "fixed" but use some filler variable. For example, the output of fa6 could be:

[
[4, 4, 5],
[4, 4, null]
]

if the filler variable was null. combine() could have an optional fillerVar argument as well, so the user can define whatever fillerVar works for them.

I dont like option 1. Option 2 is easiest. Option 3 is kinda cool, but I would like to notify the coders in some way that "hey, these Fixed2DArrays are different if you werent aware!"

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.