<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jason Mooberry &#187; game of life</title>
	<atom:link href="http://blog.jasonmooberry.com/tag/game-of-life/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jasonmooberry.com</link>
	<description>Stuff I did for you.</description>
	<lastBuildDate>Sat, 29 Oct 2011 23:41:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.2</generator>
		<item>
		<title>Drawing with Cellular Automata and HTML5 Canvas</title>
		<link>http://blog.jasonmooberry.com/2011/05/death-valley-cellular-automata-and-html5-canvas/</link>
		<comments>http://blog.jasonmooberry.com/2011/05/death-valley-cellular-automata-and-html5-canvas/#comments</comments>
		<pubDate>Wed, 11 May 2011 03:00:22 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Me]]></category>
		<category><![CDATA[cellular automata]]></category>
		<category><![CDATA[game of life]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[PHI]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=227</guid>
		<description><![CDATA[This weekend I set out to do some canvas drawings. The first one ended up being the centerpiece of my home page. It was inspired by the leaves of a tree on my street. Oh it&#8217;s good to be back in the deciduous loving climate. The second drawing was inspired by a recent campout in [...]]]></description>
			<content:encoded><![CDATA[<p>This weekend I set out to do some canvas drawings.  The first one ended up being the centerpiece of my <a href="http://jasonmooberry.com" target="_blank">home page</a>.  It was inspired by the leaves of a tree on my street.  Oh it&#8217;s good to be back in the deciduous loving climate.</p>
<p>The second drawing was inspired by a recent campout in Death Valley.  The contrast of the blue sky, desert floor, and red coleman tent leave a strong imprint on me every time.</p>
<div style="text-align:center;">
<a href="http://jasonmooberry.com/dev/img/dv.jpg" target="_blank" ><img src="http://jasonmooberry.com/dev/img/dv_sm.jpg" style="margin:10px;" /></a> <a href="http://jasonmooberry.com/dev/dv_drawing.html" target="_blank"><img src="http://jasonmooberry.com/dev/img/dv_drawing.jpg" style="margin:10px;width:300px;height:225px;" /></a>
</div>
<p>The end result should resemble the <a href="http://jasonmooberry.com/dev/dv_drawing.html" target="_blank">picture on the right.</a>  The page load time is a few seconds on my MBP and it looks quite a lot better in Chrome.  The canvas size is determined at load so if you have the horses, full-screen your browser.  </p>
<p>I say resemble because the drawing is generated using canvas and cellular automata, and derived from a random data set every time it is rendered.  </p>
<p>One of the difficulties of drawing a picture of landscape is the seemingly random variations found in it.  Not totally random because each element interacts and affects the others.  The stone pattern on the drawing was the first thing I set out to solve.</p>
<p>Initially I was hoping to hit on some semi-random scatter pattern using PHI and a lot of trial and error.  This produced a fairly uniform random scatter of stones.  What I wanted to draw resembled clumps more than anything.  </p>
<p>While scatter patterning away I happened to over hear a gentlemen at the coffee shop mentioning cellular automata and emergent systems as they explain the state of the universe.  It&#8217;s kindof a hippie coffee shop but this guy is clearly in a league of his own.  At first I dismissed it and then the idea grew on me that I could generate these clumps with CA.  I had used CA for Conway&#8217;s game of life and a few other toy systems to experiment in the past and it was always fun.</p>
<p>So I generated a map of pixels that were randomly turned on.  I weighted the initial data set to be very switched on (baby!).  My reasoning was that I wanted grouping to occur within a few mutations and a seriously overcrowded starting set should do just that.  The size of the canvas element was prohibitively large so I added a resolution factor.  It causes striations in the starting set view but it doesn&#8217;t affect the CA mutations.</p>
<pre>
// set up psuedo random cell set
for (var i=0; i < total; i++) {
  if (Math.random() > .2) {
    cells[i] = 1;
  }
}
</pre>
<div style="text-align:center;"><a href="http://jasonmooberry.com/dev/ca_helper.html" target="_blank"><img src="http://jasonmooberry.com/dev/img/dv_initial_set.gif" /></a><br />
with resolution factor:<br />
<a href="http://jasonmooberry.com/dev/ca_helper.html" target="_blank"><img src="http://jasonmooberry.com/dev/img/dv_initial_set_r.gif" /></a></div>
<p>Let&#8217;s grind this set down with Conway&#8217;s game of life rules and see what comes out:</p>
<pre>
// mutate m number of times
while (m--) {
  var next = {};
  for (p in cells) { next[p] = 1; }

  for (var i=0; i < total; i++) {
    var above = i-w < 0 ? i-w+total : i-w,  // wrap
      below = i+w > total ? i+w-total : i+w, // wrap
      neighbors = [above-1,above,above+1,i-1,i+1,below-1,below,below+1],
      active = 0,
      j = neighbors.length-1;

    while (j--) {
      active += (typeof cells[neighbors[j]] === 'number' &#038;&#038; cells[neighbors[j]] === 1)|0;
    }
    // rule check
    // Any dead cell with exactly three live neighbours becomes a live cell.
    if (active === 3 &#038;&#038; typeof cells[i] === 'undefined') {
      next[i] = 1;
    }
    // Any live cell with two or three live neighbours lives on to the next generation.
    // Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
    // Any live cell with more than three live neighbours dies, as if by overcrowding.
    else if (active < 2 || active > 3) {
      delete next[i];
    }
  }
  var cells = {};
  for (p in next) { cells[p] = 1; }
}
</pre>
<div style="text-align:center;"><a href="http://jasonmooberry.com/dev/ca_helper.html" target="_blank"><img src="http://jasonmooberry.com/dev/img/dv_pass_1.gif" /></p>
<p><img src="http://jasonmooberry.com/dev/img/dv_pass_3.gif" /></p>
<p><img src="http://jasonmooberry.com/dev/img/dv_pass_8.gif" /></a></div>
<p>As you can see the first pass cleared out a bunch of em.  The resolution factor has spaced the pixels apart but you can see the basics of game of life players emerging.  By pass 3 it&#8217;s in a much more spare state but still larger groups than I want.  By pass 8 the groups have moved away from each other a bit more and formed a decent pattern. Since this pattern is generated fresh from a random data set every time, we just need something that&#8217;s right most of the time.  </p>
<p>What I like about this approach is that it gives some credence to the hippie coffee shop guy&#8217;s words.  I don&#8217;t think the world runs on CA rules, but iterative, interactive rules seem to give rise to interesting &#8220;random&#8221; patterns.</p>
<p>The dataset produced from these mutations can now be used to render a rock pattern with a little magic for the coloring/size.  </p>
<pre>
for (var i in cells) {
  c.beginPath();
  var x = (i*r)%w,
    y = (i*r)/w|0;
  c.strokeStyle = (['rgba(116,116,108,.7);','rgba(87,93,55,.9);'])[i%15===0|0];
  c.lineWidth = Math.max(30,50*(y/h));
  c.arc(x,y,3,0,360,false);
  c.stroke();
}
</pre>
<div style="text-align:center;"><a href="http://jasonmooberry.com/dev/ca_helper.html" target="_blank"><img src="http://jasonmooberry.com/dev/img/dv_render.gif" /></a></div>
<h3>tl dr;</h3>
<p>Using Cellular Automata rules to mutate random datasets can produce interesting and unique landscape patterns.  This approach was super useful for me in drawing an impressionist picture of my favorite camping spot.</p>
<p>I wrote a <a href="http://jasonmooberry.com/dev/ca_helper.html" target="_blank">simple tool</a> to experiment with different factors for generating these patterns.</p>
<p>And <a href="http://jasonmooberry.com/dev/dv_drawing.html" target="_blank">here&#8217;s the picture</a> that is generated from random CA patterns.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2011/05/death-valley-cellular-automata-and-html5-canvas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geoff &#8211; Game Life!</title>
		<link>http://blog.jasonmooberry.com/2010/06/geoff-game-life/</link>
		<comments>http://blog.jasonmooberry.com/2010/06/geoff-game-life/#comments</comments>
		<pubDate>Sun, 27 Jun 2010 00:31:05 +0000</pubDate>
		<dc:creator>Jason Mooberry</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[game of life]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://blog.jasonmooberry.com/?p=61</guid>
		<description><![CDATA[I've been fascinated with Conway's Game of Life for years now.  I love how there is an apparent order to things that is reproducible as well as simple in it's requirements.  Recently there was a discovery that made the tech news about a new pattern that replicated itself.  I'll be honest, the demo of the replicating code wasn't as sexy as I had hoped.  But it did remind me how much I want to make this game.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been fascinated with <a href="http://en.wikipedia.org/wiki/Conway's_Game_of_Life" target="_blank">Conway&#8217;s Game of Life</a> for years now.  I love how there is an apparent order to things that is reproducible as well as simple in it&#8217;s requirements.  Recently there was a discovery that made the tech news about a <a href="http://www.newscientist.com/article/mg20627653.800-first-replicating-creature-spawned-in-life-simulator.html" target="_blank">new pattern that replicated itself</a>.  I&#8217;ll be honest, the demo of the replicating code wasn&#8217;t as sexy as I had hoped.  But it did remind me how much I want to make this game.</p>
<p>I&#8217;ve been on a recent tear through javascript, browser and server side (node.js is just fun).  So I set out to build a little game of life with js and html.  jQuery aided with some of the lifting too.  :)</p>
<p>After a couple afternoons Geoff was born.  He&#8217;s a simple lad.  Just gives you the basics, but still fun to play with non-the-less.  I have only tested him on Chrome, FF, and Safari.  Interestingly this is a great way to see how fast your js engine is.  Chrome handles it the best on my laptop.  FF the worst.</p>
<p><strong>A little bit about the code:</strong></p>
<p>jQuery&#8217;s <a href="http://api.jquery.com/live/" target="_blank">live()</a> event handling is perfect for this situation.  Since I opted to turn a crap-ton of b tags into large pixels of my game grid, I needed to bind some mouseover/mouseout and click events to each item.  There is no need to do this to each of the ~2k tags.  Instead, using live(), jQuery will bind to the document and as events bubble up, check to see if they&#8217;re relevant to my event handlers.  This cuts memory usage as well as bind time.</p>
<p>Recursion with setTimeout().  Initially when I built this I was using setInterval() to fire my life function and advance Geoff another step.  On Chrome it was fast enough that I didn&#8217;t notice any problems.  But when I tested in Safari and FF, I found that I was &#8220;dropping frames&#8221; when I cranked up the speed.  This had weird effects.  So I used a recursive function that pauses at the end of it&#8217;s execution.  This will ensure that I don&#8217;t lose an iteration, while giving reasonable control over the speed of execution.</p>
<p>Pseudo-casting as integer.  This is a hack that I picked up from <a href="http://james.padolsey.com/javascript/double-bitwise-not/">James Padolsey&#8217;s blog</a>.  For my purposes it&#8217;s a nice way to take a boolean expression and convert it into a 0 or 1.</p>
<pre class="brush: js">
// sum up the active neighbors
active += ~~(typeof current[neighbors[j]] === 'number' &amp;&amp; current[neighbors[j]] === 1);
</pre>
<p>Best practices.  I&#8217;ve violated them .. a bit.  :)</p>
<p>Well I hope you have fun playing.  I tried drawing moo and it lived a healthy and rich life.  ;)</p>
<p><strong><a href="http://jasonmooberry.com/dev/geoff.html" target="_blank">Enjoy Geoff!</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.jasonmooberry.com/2010/06/geoff-game-life/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.091 seconds -->

