Skip to content

Instantly share code, notes, and snippets.

@tlimpanont
Last active January 1, 2016 10:38
Show Gist options
  • Save tlimpanont/8132332 to your computer and use it in GitHub Desktop.
Save tlimpanont/8132332 to your computer and use it in GitHub Desktop.
Class for creating simple 2-dimensional grid with columns en rows. It provides query methods for searching a specific cell, row or column. jasmine test specs are also included.
/* required
underscore : http://underscorejs.org/underscore-min.js
*/
(function(window, _) {
/**
* Class for creating simple 2-dimensional grid with columns en rows. It provides query methods for searching a specific cell, row or column.
* @param {Object} options Setting columns and rows
*/
GridManager = function(options) {
/**
* Number of columns
* @type {int}
*/
this.columns = options.columns;
/**
* Number of rows
* @type {int}
*/
this.rows = options.rows;
/**
* Total grid cells based on colums and rows
* @type {int}
*/
this.cells = this.columns * this.rows;
/**
* The grid containing GridCell objects
* @type {Array}
*/
this.grid = new Array();
};
/**
* GridCell represent each cell of the grid.
*/
GridCell = function() {
this.coordinates_id = "";
this.coordinates = new Object();
this.value = null;
}
/**
* Create Grid based on the defined columns and rows
* @return {Array} Array of GridCell objects
*/
GridManager.prototype.createGrid = function() {
for (var x = 0; x < this.columns; x++)
{
this.grid[x] = new Array();
for (var y = 0; y < this.rows; y++)
{
var gridCell = new GridCell();
gridCell.coordinates_id = x.toString()+y.toString() ;
gridCell.coordinates = {x: x, y: y};
this.grid[x][y] = gridCell;
}
}
return this.grid;
}
/**
* Get GridCell object
* @param {int} columnIndex
* @param {int} rowIndex
* @return {GridCell}
*/
GridManager.prototype.getCell = function(columnIndex, rowIndex) {
return this.grid[columnIndex][rowIndex];
}
/**
* Get all the GridCell objects of a specific row
* @param {int} rowIndex
* @return {Array} Array of GridCell objects
*/
GridManager.prototype.getRow = function(rowIndex) {
var row = new Array();
for (var x = 0; x < this.columns; x++)
{
for (var y = 0; y < this.rows; y++)
{
if(rowIndex == y)
row.push(this.grid[x][y]);
}
}
return row;
}
/**
* Get all the GridCell objects of a specific column
* @param {int} columnIndex
* @return {Array} Array of GridCell objects
*/
GridManager.prototype.getColumn = function(columnIndex) {
var column = new Array();
for (var x = 0; x < this.columns; x++)
{
for (var y = 0; y < this.rows; y++)
{
if(columnIndex == x)
column.push(this.grid[x][y]);
}
}
return column;
}
/**
* Loop through all cells from most upper left cell to most bottom right cell and return GridCell objects
* @return {Array} Array of GridCell objects
*/
GridManager.prototype.getLeftToRight = function() {
if(this.columns != this.rows)
throw new Error("Columns must be Equal to Rows if getting diagonal cells. The grid should be a square grid.");
var left_to_right = new Array();
for (var x = 0; x < this.columns; x++)
{
for (var y = 0; y < this.rows; y++)
{
left_to_right.push(this.grid[x][y]);
x++;
x + y;
}
}
return left_to_right;
}
/**
* Loop through all cells from most upper right cell to most bottom left cell and return GridCell objects
* @return {Array} Array of GridCell objects
*/
GridManager.prototype.getRightToLeft = function() {
if(this.columns != this.rows)
throw new Error("Columns must be Equal to Rows if getting diagonal cells. The grid should be a square grid.");
var right_to_left = new Array();
for (var x = this.columns - 1; x >= 0; x--)
{
for (var y = 0; y < this.rows; y++)
{
right_to_left.push(this.grid[x][y]);
x--;
}
}
return right_to_left;
}
/**
* Search a specifc GridCell by coordinates_id. Grid cells are instantiating with some default properties like coordinates_id (string).
* @param {string} coordinates_id Coordinates Id like "00" or "22" where left value is x coordinate and right value is y coordinate
* @return {GridCell} A specific GridCell object
*/
GridManager.prototype.getCellByCoordinatesId = function(coordinates_id) {
return _.findWhere(_.flatten(this.grid), {coordinates_id : coordinates_id });
}
/**
* Sum or concatenating the value of all the GridCells in the given array
* @param {Array} Array of GridCell objects
* @return {int|string} The sum or concatenating string of GridCell.value
*/
GridManager.prototype.countTotalValue = function(cells) {
var total = null;
_.each(cells, function(cell, index) {
total += cell.value;
});
return total;
}
})(window, _);
describe("GridManager", function() {
var gridManager;
beforeEach(function() {
gridManager = new GridManager({
rows: 3, columns: 3
}); gridManager.createGrid();
});
it(".getCell should return GridCell type of Object", function() {
expect(
(gridManager.getCell(0,0) instanceof GridCell)
).toBe(true);
});
it("getLeftToRight or getRightToLeft should only permitted when columns and rows are equal (the grid should be square grid)", function() {
gridManager = new GridManager({
rows: 5, columns: 3
}); gridManager.createGrid();
expect( function() { gridManager.getLeftToRight() } ).toThrow();
});
it("should expect the total cells of nine", function() {
expect(gridManager.cells).toEqual(9);
});
it("the cell of the first column and first row should return coordinates {x: 0, y: 0}", function() {
expect(gridManager.getCell(0,0).coordinates).toEqual({x: 0, y: 0});
});
it("the cell of the last column and last row should return coordinates {x: 2, y: 2}", function() {
expect(gridManager.getCell(gridManager.columns - 1, gridManager.rows - 1).coordinates).toEqual({x: 2, y: 2});
});
it("the second cell of the first row should return coordinates {x: 1, y: 0} ", function() {
expect(gridManager.getRow(0)[1].coordinates).toEqual({x: 1, y: 0});
});
it("the last cell of the last column should return coordinates {x: 2, y: 2} ", function() {
expect(gridManager.getColumn(gridManager.columns - 1)[gridManager.rows - 1].coordinates).toEqual({x: 2, y: 2});
});
it("the last cell of left to right diagonal cells should return coordinates {x: 2, y: 2}", function() {
expect(gridManager.getLeftToRight()[gridManager.getLeftToRight().length - 1].coordinates).toEqual({x: 2, y: 2});
});
it("the last cell of right to left diagonal cells should return coordinates {x: 0, y: 2}", function() {
expect(gridManager.getRightToLeft()[gridManager.getRightToLeft().length - 1].coordinates).toEqual({x: 0, y: 2});
});
it("coordinate x of coordinate string id 00 should be {x: 0, y: 0}", function() {
expect(gridManager.getCellByCoordinatesId("00").coordinates).toEqual({x: 0, y: 0});
});
it("should return total value of 16 when each cell value of the first row is set to 5 2 and 9 ", function() {
gridManager.getCellByCoordinatesId("00").value = 5;
gridManager.getCellByCoordinatesId("10").value = 2;
gridManager.getCellByCoordinatesId("20").value = 9;
expect(
gridManager.countTotalValue(gridManager.getRow(0))
).toEqual(16);
});
it("should expect exactly one row that has the toal value of 16", function() {
gridManager.getCellByCoordinatesId("00").value = 5;
gridManager.getCellByCoordinatesId("10").value = 2;
gridManager.getCellByCoordinatesId("20").value = 9;
var total_value = 16;
var rows_has_total_value = 0;
for(var i = 0; i < gridManager.rows; i++)
{
if(gridManager.countTotalValue(gridManager.getRow(i)) == total_value)
{
rows_has_total_value++;
}
}
expect(rows_has_total_value).toEqual(1);
});
it("should expect exactly one row when concat the string of X three times and test with /X{3}/ regex pattern", function() {
gridManager.getCellByCoordinatesId("00").value = "X";
gridManager.getCellByCoordinatesId("10").value = "X";
gridManager.getCellByCoordinatesId("20").value = "X";
var rows_has_total_value = 0;
var pattern = "X{"+gridManager.rows+"}";
var patt = new RegExp(pattern);
for(var i = 0; i < gridManager.rows; i++)
{
if(gridManager.countTotalValue(gridManager.getRow(i)).toString().match(patt))
{
rows_has_total_value++;
}
}
expect(rows_has_total_value).toEqual(1);
});
it("setting all left to right diagonal cell to 1 should return the sum of 3", function() {
_.each(gridManager.getLeftToRight(), function(cell, index) {
cell.value = 1;
});
var total_value = 0;
_.each(gridManager.getLeftToRight(), function(cell, index) {
total_value += cell.value;
});
expect(total_value).toEqual(3);
expect(
gridManager.getCellByCoordinatesId("00").value +
gridManager.getCellByCoordinatesId("11").value +
gridManager.getCellByCoordinatesId("22").value
).toEqual(3);
expect(
gridManager.getCellByCoordinatesId("10").value
).toBe(null);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment