Created
July 14, 2011 22:48
-
-
Save michaelfeathers/1083654 to your computer and use it in GitHub Desktop.
Node test pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sys = require('sys'); | |
function Cell(x,y) { | |
this.x = x; | |
this.y = y; | |
this.state = (((x - 10) * (x - 10) + (y - 10) * (y - 10)) % 5) == 0 | |
} | |
Cell.prototype.run = function() { | |
if (this.state == true) | |
mark(this.x, this.y); | |
else | |
unmark(this.x, this.y); | |
this.state = !this.state; | |
} | |
clearScreen = function() { | |
sys.print("\x1B[2J"); | |
}; | |
goto = function(x,y) { | |
sys.print("\x1B[" + y + ";" + x + "H"); | |
} | |
mark = function(x,y) { | |
goto(x,y); | |
sys.print("X"); | |
}; | |
unmark = function(x,y) { | |
goto(x,y); | |
sys.print(" "); | |
} | |
runCells = function(cells) { | |
for(var index = 0; index < cells.length; index++) | |
cells[index].run(); | |
} | |
start = function() { | |
var cells = new Array(); | |
var index = 0; | |
var limit = 50; | |
for(var x = 0; x < limit; x++) { | |
for(var y = 0; y < limit; y++) { | |
cells[index++] = new Cell(x,y); | |
} | |
} | |
return cells; | |
} | |
clearScreen(); | |
var cells = start(); | |
function runner() { | |
setTimeout(function() { | |
runCells(cells); | |
runner(); | |
}, 50); | |
} | |
runner(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment