Created
August 31, 2011 23:49
-
-
Save Zae/1185067 to your computer and use it in GitHub Desktop.
Revisions
-
Zae created this gist
Aug 31, 2011 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ setup = (@c) -> [@h,@w,@a,@b,@m] = [40, 80, 0, 1,[]] spawn() spawn = -> @m[@a] = (Math.floor Math.random() * 2 for i in [0...@w+1] for j in [0...@h+1]) @m[@b] = (0 for i in [0...@w+1] for j in [0...@h+1]) draw = -> lifedeathandshow() neighbours = (i,j) -> @m[@a][i-1][j-1] + @m[@a][i][j-1] + @m[@a][i+1][j-1] + @m[@a][i-1][j] + @m[@a][i][j] + @m[@a][i+1][j] + @m[@a][i-1][j+1] + @m[@a][i][j+1] + @m[@a][i+1][j+1] lifedeathandshow = -> (n = neighbours i,j if @m[@a][i][j] is 1 if n < 2 or n > 3 @m[@b][i][j] = 0 @c.clearRect i*10, j*10, 10, 10 else @m[@b][i][j] = @m[@a][i][j] @c.fillRect i*10, j*10, 10, 10 else if n is 3 @m[@b][i][j] = 1 @c.fillRect i*10, j*10, 10, 10 else @m[@b][i][j] = @m[@a][i][j] @c.clearRect i*10, j*10, 10, 10 [@a, @b] = [@b, @a]) for j in [1...@w-1] for i in [1...@h-1] setup document.getElementById('canvizzle').getContext '2d' setInterval draw, 1000 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,68 @@ (function() { var clear, draw, lifedeathandshow, neighbours, setup, spawn; setup = function(c) { var _ref; this.c = c; _ref = [40, 80, 0, 1, []], this.h = _ref[0], this.w = _ref[1], this.a = _ref[2], this.b = _ref[3], this.m = _ref[4]; return spawn(); }; spawn = function() { var i, j; this.m[this.a] = (function() { var _ref, _results; _results = []; for (j = 0, _ref = this.h + 1; 0 <= _ref ? j < _ref : j > _ref; 0 <= _ref ? j++ : j--) { _results.push((function() { var _ref2, _results2; _results2 = []; for (i = 0, _ref2 = this.w + 1; 0 <= _ref2 ? i < _ref2 : i > _ref2; 0 <= _ref2 ? i++ : i--) { _results2.push(Math.floor(Math.random() * 2)); } return _results2; }).call(this)); } return _results; }).call(this); return this.m[this.b] = (function() { var _ref, _results; _results = []; for (j = 0, _ref = this.h + 1; 0 <= _ref ? j < _ref : j > _ref; 0 <= _ref ? j++ : j--) { _results.push((function() { var _ref2, _results2; _results2 = []; for (i = 0, _ref2 = this.w + 1; 0 <= _ref2 ? i < _ref2 : i > _ref2; 0 <= _ref2 ? i++ : i--) { _results2.push(0); } return _results2; }).call(this)); } return _results; }).call(this); }; draw = function() { return lifedeathandshow(); }; clear = function() { return this.c.clearRect(0, 0, this.w * 10, this.h * 10); }; neighbours = function(i, j) { return this.m[this.a][i - 1][j - 1] + this.m[this.a][i][j - 1] + this.m[this.a][i + 1][j - 1] + this.m[this.a][i - 1][j] + this.m[this.a][i][j] + this.m[this.a][i + 1][j] + this.m[this.a][i - 1][j + 1] + this.m[this.a][i][j + 1] + this.m[this.a][i + 1][j + 1]; }; lifedeathandshow = function() { var i, j, n, _ref, _results; _results = []; for (i = 1, _ref = this.h - 1; 1 <= _ref ? i < _ref : i > _ref; 1 <= _ref ? i++ : i--) { _results.push((function() { var _ref2, _ref3, _results2; _results2 = []; for (j = 1, _ref2 = this.w - 1; 1 <= _ref2 ? j < _ref2 : j > _ref2; 1 <= _ref2 ? j++ : j--) { _results2.push((n = neighbours(i, j), this.m[this.a][i][j] === 1 ? n < 2 || n > 3 ? (this.m[this.b][i][j] = 0, this.c.clearRect(i * 10, j * 10, 10, 10)) : (this.m[this.b][i][j] = this.m[this.a][i][j], this.c.fillRect(i * 10, j * 10, 10, 10)) : (n === 3 ? (this.m[this.b][i][j] = 1, this.c.fillRect(i * 10, j * 10, 10, 10)) : (this.m[this.b][i][j] = this.m[this.a][i][j], this.c.clearRect(i * 10, j * 10, 10, 10)), (_ref3 = [this.b, this.a], this.a = _ref3[0], this.b = _ref3[1], _ref3)))); } return _results2; }).call(this)); } return _results; }; setup(document.getElementById('canvizzle').getContext('2d')); setInterval(draw, 1000); }).call(this); 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,12 @@ <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></title> </head> <body> <p>Use the coffee compiler to compile the conway.coffee script to a usable .js file or use the .js file provided (precompiled)</p> <canvas id="canvizzle"></canvas> <script type="text/javascript" src="js/conway.js"></script> </body> </html>