Skip to content
Permalink
Browse files

Refactor currentOn y-axis

The y-axis values in currentOn are now an
object not an inner array. This makes currentOn
behave like a true multidimensional array. This
makes it easier to look up coordinates.
  • Loading branch information...
Daniel Deming
Daniel Deming committed Mar 9, 2014
1 parent 3c29f72 commit 173017b777608c0be70b657e865c9e35c8e51c4b
Showing with 29 additions and 32 deletions.
  1. +14 −12 spec/LifeSpec.js
  2. +3 −3 spec/LifedomSpec.js
  3. +12 −17 src/life.js
@@ -28,7 +28,9 @@ describe('Life class', function() {
{ x: 3, y: 3 },
{ x: 3, y: 2 }
]});
expect(life.getCurrentOn()['3'].length).toBe(3);
expect(life.getCurrentOn()['3']['2']).toBe(1);
expect(life.getCurrentOn()['3']['3']).toBe(1);
expect(life.getCurrentOn()['3']['4']).toBe(1);
});
});

@@ -39,7 +41,7 @@ describe('Life class', function() {

it('should group by X', function() {
var results = life.groupByX([{ x: 3, y: 4 }]);
expect(results['3']).toContain(4);
expect(results['3']['4']).toBe(1);
});
});

@@ -113,15 +115,16 @@ describe('Life class', function() {

it('should update currentOn', function() {
life.doTurn();
expect(life.getCurrentOn()['3'].length).toBe(1);
expect(life.getCurrentOn()['2'][0]).toBe(3);
expect(life.getCurrentOn()['3'][0]).toBe(3);
expect(life.getCurrentOn()['4'][0]).toBe(3);
expect(life.getCurrentOn()['2']['3']).toBe(1);
expect(life.getCurrentOn()['3']['3']).toBe(1);
expect(life.getCurrentOn()['4']['3']).toBe(1);
});

it('should update priorOn', function() {
life.doTurn();
expect(life.getPriorOn()['3'].length).toBe(3);
expect(life.getPriorOn()['3']['2']).toBe(1);
expect(life.getPriorOn()['3']['3']).toBe(1);
expect(life.getPriorOn()['3']['4']).toBe(1);
});
});

@@ -137,10 +140,9 @@ describe('Life class', function() {
it('should return only the new enabled plots', function() {
life.doTurn();
life.doTurn();
life.getNewEnabled();
expect(life.getNewEnabled()['3'].length).toBe(2);
expect(life.getNewEnabled()['3'][0]).toBe(2);
expect(life.getNewEnabled()['3'][1]).toBe(4);
expect(life.getNewEnabled()['3'][0]).toBe('2');
expect(life.getNewEnabled()['3'][1]).toBe('4');
});

});
@@ -159,8 +161,8 @@ describe('Life class', function() {
life.doTurn();
expect(life.getNewDisabled()['2'].length).toBe(1);
expect(life.getNewDisabled()['4'].length).toBe(1);
expect(life.getNewDisabled()['2'][0]).toBe(3);
expect(life.getNewDisabled()['4'][0]).toBe(3);
expect(life.getNewDisabled()['2'][0]).toBe('3');
expect(life.getNewDisabled()['4'][0]).toBe('3');
});

});
@@ -76,9 +76,9 @@ describe('Lifedom class', function() {
});

it('should set currentOn', function() {
expect(life.getCurrentOn()['1'][0]).toBe(1);
expect(life.getCurrentOn()['2'][0]).toBe(2);
expect(life.getCurrentOn()['3'][0]).toBe(3);
expect(life.getCurrentOn()['1'][1]).toBe(1);
expect(life.getCurrentOn()['2'][2]).toBe(1);
expect(life.getCurrentOn()['3'][3]).toBe(1);
});
});

@@ -31,8 +31,8 @@ var life = (function life() {
var newEnabled = {};

_.each(currentOn, function(yAxes, key) {
_.each(yAxes, function(yAxis) {
if (priorOn[key] && priorOn[key].indexOf(yAxis)!==-1) {
_.each(yAxes, function(placeholder, yAxis) {
if (priorOn[key] && priorOn[key][yAxis] === 1) {
return;
} else {
if (!newEnabled[key]) newEnabled[key] = [];
@@ -52,8 +52,8 @@ var life = (function life() {
var newDisabled = {};

_.each(priorOn, function(yAxes, key) {
_.each(yAxes, function(yAxis) {
if (currentOn[key] && currentOn[key].indexOf(yAxis)!==-1) {
_.each(yAxes, function(placeholder, yAxis) {
if (currentOn[key] && currentOn[key][yAxis] === 1) {
return;
} else {
if (!newDisabled[key]) newDisabled[key] = [];
@@ -78,13 +78,13 @@ var life = (function life() {

_.each(Xs, function(xAxis) {
if (currentOn[xAxis]) {
if (_.indexOf(currentOn[xAxis], Ys[0])!==-1) count++;
if (currentOn[xAxis][ Ys[0] ] === 1) count++;
if ( xAxis !== Xs[1] &&
_.indexOf(currentOn[xAxis], Ys[1])!==-1
currentOn[xAxis][ Ys[1] ] === 1
) {
count++;
}
if (_.indexOf(currentOn[xAxis], Ys[2])!==-1) count++;
if (currentOn[xAxis][ Ys[2] ] === 1) count++;
}
});

@@ -130,7 +130,7 @@ var life = (function life() {

_.each(currentOn, function(yAxes, xAxis) {
xAxis = parseInt(xAxis, 10);
_.each(yAxes, function(yAxis) {
_.each(yAxes, function(placeholder, yAxis) {
yAxis = parseInt(yAxis, 10);
// Mucho copy-pasteo
plots.push({x: xAxis-1, y: yAxis-1});
@@ -151,20 +151,15 @@ var life = (function life() {
/**
* Converts an array of individual plots into an object indexed
* by X-Axis. This is done for optimization purposes
* TODO: Future optimization would keep inner arrays sorted to
* speed up lookups.
*/
groupByX: function(plots) {
var i, key, obj = {}, y;

for (i=0; i<plots.length; i++) {
for (i=plots.length-1; i>-1; i--) {
key = parseInt(plots[i].x, 10);
y = parseInt(plots[i].y, 10);
if (obj[key]) {
obj[key].push(y);
} else {
obj[key] = [ y ];
}
if (!obj[key]) obj[key] = {};
obj[key][y] = 1;
}
return obj;
},
@@ -195,7 +190,7 @@ var life = (function life() {
*/
isOn: function(point) {
return (currentOn[point.x] &&
currentOn[point.x].indexOf(point.y)!==-1) ?
currentOn[point.x][point.y] === 1) ?
true: false;
}
};

0 comments on commit 173017b

Please sign in to comment.
You can’t perform that action at this time.