{"id":644,"date":"2012-10-12T18:32:43","date_gmt":"2012-10-12T18:32:43","guid":{"rendered":"http:\/\/psyphi.net\/blog\/?p=644"},"modified":"2012-10-16T13:57:13","modified_gmt":"2012-10-16T13:57:13","slug":"conways-game-of-life-in-perl","status":"publish","type":"post","link":"https:\/\/psyphi.net\/blog\/2012\/10\/conways-game-of-life-in-perl\/","title":{"rendered":"Conway&#8217;s Game of Life in Perl"},"content":{"rendered":"<p>I wanted a quick implementation of <a href=\"http:\/\/www.math.com\/students\/wonders\/life\/life.html\" title=\"Conway's Game of Life\" target=\"_blank\">Conway&#8217;s Game of Life<\/a> this evening to muck about with, with the boys. Whipped this up in simple Perl for running in a terminal \/ on the commandline. It&#8217;s not the fastest implementation on the planet but that&#8217;s most likely just the way I&#8217;ve coded it.<\/p>\n<p>Throw some 1s and 0s in the DATA block at the end to modify the start state, change the $WIDTH and $HEIGHT of the area, or uncomment the random data line in init() and see what you see.<\/p>\n<pre><code>#!\/usr\/local\/bin\/perl\r\n# -*- mode: cperl; tab-width: 8; indent-tabs-mode: nil; basic-offset: 2 -*-\r\n# vim:ts=8:sw=2:et:sta:sts=2\r\n#########\r\n# Author:        rmp\r\n# Created:       2012-10-12\r\n# Last Modified: $Date: 2012-10-12 19:09:00 +0100 (Fri, 12 Oct 2012) $\r\n# Id:            $Id$\r\n# $HeadURL$\r\n#\r\nuse strict;\r\nuse warnings;\r\nuse Time::HiRes qw(sleep);\r\nuse Readonly;\r\nuse English qw(-no_match_vars);\r\nuse Carp;\r\n\r\nReadonly::Scalar my $WIDTH      =&gt; 78;\r\nReadonly::Scalar my $HEIGHT     =&gt; 21;\r\nReadonly::Scalar my $TURN_DELAY =&gt; 0.1;\r\n\r\nour $VERSION = '0.01';\r\n\r\nmy $grid  = init();\r\nmy $turns = 0;\r\nwhile(1) {\r\n  render($grid);\r\n  $grid = turn($grid);\r\n  sleep $TURN_DELAY;\r\n  $turns++;\r\n}\r\n\r\nsub init {\r\n  #########\r\n  # initialise with a manual input from the DATA block below\r\n  #\r\n  local $RS = undef;\r\n  my $data  = &lt;data&gt;;\r\n  my $out   = [\r\n\t       map { [split \/\/smx, $_] }\r\n\t       map { split \/\\n\/smx, $_ }\r\n\t       $data\r\n\t      ];\r\n\r\n  #########\r\n  # fill the matrix with space\r\n  #\r\n  for my $y (0..$HEIGHT-1) {\r\n    for my $x (0..$WIDTH-1) {\r\n      $out-&gt;[$y]-&gt;[$x] ||= 0;\r\n#      $out-&gt;[$y]-&gt;[$x] = rand &gt;= 0.2 ? 0 : 1; # initialise with some random data\r\n    }\r\n  }\r\n  return $out;\r\n}\r\n\r\n#########\r\n# draw to stdout\/screen\r\n#\r\nsub render {\r\n  my ($in) = @_;\r\n  system $OSNAME eq 'MSWin32' ? 'cls' : 'clear';\r\n\r\n  print q[+], q[-]x$WIDTH, \"+\\n\" or croak qq[Error printing: $ERRNO];\r\n  for my $y (@{$in}) {\r\n    print q[|] or croak qq[Error printing: $ERRNO];\r\n    print map { $_ ? q[O] : q[ ] } @{$y} or croak qq[Error printing: $ERRNO];\r\n    print \"|\\r\\n\" or croak qq[Error printing: $ERRNO];\r\n  }\r\n  print q[+], q[-]x$WIDTH, \"+\\n\" or croak qq[Error printing: $ERRNO];\r\n\r\n  return 1;\r\n}\r\n\r\n#########\r\n# the fundamental Game of Life rules\r\n#\r\nsub turn {\r\n  my ($in) = @_;\r\n  my $out  = [];\r\n\r\n  for my $y (0..$HEIGHT-1) {\r\n    for my $x (0..$WIDTH-1) {\r\n      my $topedge    = $y-1;\r\n      my $bottomedge = $y+1;\r\n      my $leftedge   = $x-1;\r\n      my $rightedge  = $x+1;\r\n\r\n      my $checks = [\r\n\t\t    grep { $_-&gt;[0] &gt;= 0 &amp;&amp; $_-&gt;[0] &lt; $HEIGHT } # Y boundary checking\r\n\t\t    grep { $_-&gt;[1] &gt;= 0 &amp;&amp; $_-&gt;[1] &lt; $WIDTH }  # X boundary checking\r\n\t\t    [$topedge,    $leftedge],\r\n\t\t    [$topedge,    $x],\r\n\t\t    [$topedge,    $rightedge],\r\n\t\t    [$y,          $leftedge],\r\n\t\t    [$y,          $rightedge],\r\n\t\t    [$bottomedge, $leftedge],\r\n\t\t    [$bottomedge, $x],\r\n\t\t    [$bottomedge, $rightedge],\r\n\t\t   ];\r\n\r\n      my $alive = scalar\r\n\t          grep { $_ }\r\n\t          map { $in-&gt;[$_-&gt;[0]]-&gt;[$_-&gt;[1]] }\r\n\t\t  @{$checks};\r\n\r\n      $out-&gt;[$y]-&gt;[$x] = (($in-&gt;[$y]-&gt;[$x] &amp;&amp; $alive == 2) ||\r\n\t\t\t  $alive == 3);\r\n    }\r\n  }\r\n  return $out;\r\n}\r\n\r\n__DATA__\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0\r\n0000000010\r\n0000000111\r\n0000000101\r\n0000000111\r\n0000000010\r\n&lt;\/data&gt;<\/code><\/pre>\n<p>p.s. WordPress is merrily swapping &#8220;DATA&#8221; for &#8220;data&#8221; on line 38 and adding an extra \/data tag at the end of that code snippet. Fix line 38 and don&#8217;t copy &#038; paste the close tag. Damn I hate WordPress :(<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wanted a quick implementation of Conway&#8217;s Game of Life this evening to muck about with, with the boys. Whipped this up in simple Perl for running in a terminal \/ on the commandline. It&#8217;s not the fastest implementation on the planet but that&#8217;s most likely just the way I&#8217;ve coded it. Throw some 1s &hellip; <a href=\"https:\/\/psyphi.net\/blog\/2012\/10\/conways-game-of-life-in-perl\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Conway&#8217;s Game of Life in Perl&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":652,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[11],"tags":[825,824,198,809,21],"class_list":["post-644","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-programming","tag-commandline","tag-conway","tag-fun","tag-life","tag-perl"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/comments?post=644"}],"version-history":[{"count":9,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions"}],"predecessor-version":[{"id":650,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/posts\/644\/revisions\/650"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/media\/652"}],"wp:attachment":[{"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/media?parent=644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/categories?post=644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/psyphi.net\/blog\/wp-json\/wp\/v2\/tags?post=644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}