Skip to content

Instantly share code, notes, and snippets.

@johan
Created October 27, 2011 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save johan/1318699 to your computer and use it in GitHub Desktop.
Save johan/1318699 to your computer and use it in GitHub Desktop.
Conway's Game of Life
<!DOCTYPE html>
<html>
<head>
<link href="reset.css" rel="stylesheet" type="text/css"></link>
<script src="https://github.com/mbostock/d3/raw/master/d3.js"></script>
<style>
rect { fill: #FFF; stroke: none; }
.life { fill: #ACE; }
rect:hover { stroke: #BDF; }
</style>
</head>
<body>
<script src="life.js"></script>
</body>
</html>
var w = 960 // bl.ocks.org viewport width
, h = 500 // bl.ocks.org viewport height
, cw = 8 // cellWidth
, ch = 8 // cellHeight
, m = false // toggle mode on mousedown/mouseup
, ccx = w/cw // cell count x
, ccy = h/ch // cell count y
, del = 100 // ms between generations
, xs = d3.scale.linear().domain([0, ccx]).rangeRound([0, ccx * cw])
, ys = d3.scale.linear().domain([0, ccy]).rangeRound([0, ccy * ch])
, states = []
;
d3.range(ccx * ccy).forEach(function(c) {
states[c] = Math.random() > .8;
});
var vis = d3.select('body').append('svg:svg')
.attr('width', w)
.attr('height', h);
vis.selectAll('rect')
.data(states)
.enter().append('svg:rect')
.attr('width', cw)
.attr('height', ch)
.attr('x', function(d, i) { return xs(i % ccx); })
.attr('y', function(d, i) { return ys(i / ccx | 0); })
.on('mouseup', function() { m = false; })
.on('mousedown', function() { m = true; })
.on('mousemove', function(d, i) { if (m) states[i] = !states[i]; })
.classed('life', function(d) { return d; });
function createNewGeneration() {
var c, x, y, t, r, b, l, n, nextState = [];
for (x = 0; x < ccx; x++) {
l = x - 1;
r = x + 1;
for (y = 0; y < ccy; y++) {
t = y - 1;
b = y + 1;
n = states[coord(l,t)] + states[coord(x,t)] + states[coord(r,t)]
+ states[coord(l,y)] + states[coord(r,y)]
+ states[coord(l,b)] + states[coord(x,b)] + states[coord(r,b)];
nextState[c = coord(x,y)] = states[c] ? n == 2 || n == 3 : n == 3;
}
}
return nextState;
}
function coord(x, y) {
return coord[x +','+ y] ||
(coord[x +','+ y] = ccx * ((ccy + y) % ccy) + ((ccx + x) % ccx));
}
function animate() {
d3.selectAll('rect')
.data(states = createNewGeneration())
.classed('life', function(d) { return d; });
}
setInterval(animate, del);
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment