Skip to content

Commit

Permalink
Refactor currentOn y-axis
Browse files Browse the repository at this point in the history
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 committed Mar 9, 2014
1 parent 3c29f72 commit 173017b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
26 changes: 14 additions & 12 deletions spec/LifeSpec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ describe('Life class', function() {
{ x: 3, y: 3 }, { x: 3, y: 3 },
{ x: 3, y: 2 } { 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);
}); });
}); });


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


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


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


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


it('should update priorOn', function() { it('should update priorOn', function() {
life.doTurn(); 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);
}); });
}); });


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


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


}); });
Expand Down
6 changes: 3 additions & 3 deletions spec/LifedomSpec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ describe('Lifedom class', function() {
}); });


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


Expand Down
29 changes: 12 additions & 17 deletions src/life.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var life = (function life() {
var newEnabled = {}; var newEnabled = {};


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


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


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


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


_.each(currentOn, function(yAxes, xAxis) { _.each(currentOn, function(yAxes, xAxis) {
xAxis = parseInt(xAxis, 10); xAxis = parseInt(xAxis, 10);
_.each(yAxes, function(yAxis) { _.each(yAxes, function(placeholder, yAxis) {
yAxis = parseInt(yAxis, 10); yAxis = parseInt(yAxis, 10);
// Mucho copy-pasteo // Mucho copy-pasteo
plots.push({x: xAxis-1, y: yAxis-1}); plots.push({x: xAxis-1, y: yAxis-1});
Expand All @@ -151,20 +151,15 @@ var life = (function life() {
/** /**
* Converts an array of individual plots into an object indexed * Converts an array of individual plots into an object indexed
* by X-Axis. This is done for optimization purposes * by X-Axis. This is done for optimization purposes
* TODO: Future optimization would keep inner arrays sorted to
* speed up lookups.
*/ */
groupByX: function(plots) { groupByX: function(plots) {
var i, key, obj = {}, y; 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); key = parseInt(plots[i].x, 10);
y = parseInt(plots[i].y, 10); y = parseInt(plots[i].y, 10);
if (obj[key]) { if (!obj[key]) obj[key] = {};
obj[key].push(y); obj[key][y] = 1;
} else {
obj[key] = [ y ];
}
} }
return obj; return obj;
}, },
Expand Down Expand Up @@ -195,7 +190,7 @@ var life = (function life() {
*/ */
isOn: function(point) { isOn: function(point) {
return (currentOn[point.x] && return (currentOn[point.x] &&
currentOn[point.x].indexOf(point.y)!==-1) ? currentOn[point.x][point.y] === 1) ?
true: false; true: false;
} }
}; };
Expand Down

0 comments on commit 173017b

Please sign in to comment.