Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Game of Life

by jimt (Chaplain)
on Sep 21, 2006 at 18:47 UTC ( #574225=obfuscated: print w/replies, xml ) Need Help??

I don't know if this would be considered an obfuscation or a golfing exercise, but nonetheless, in only 451 characters, I present you with John Conway's Game Of Life.

my($a,$c,$g,@b)=(40,10);$\=$/;for my$r(0..$a){$b [$r][$_]=!int(rand($c))?'*':$"for(0..$a)}sub g{ my($d,@e);for my$r(0..$a){for(0..$a){my$h=0;for my$i(-1..1){for my$j(-1..1){my($f,$g)=($r+$i,$_ +$j);next if$f<0||$f>$a||$g<0||$g>$a||!($i||$j); $h++if$b[$f][$g]ne$"}}$e[$r][$_]=($b[$r][$_]ne$" &&$h==2)||$h==3?'*':$";$d++if$e[$r][$_]ne$b[$r][ $_]}}@b=@e;$d}do{print`clear`,++$g;for my$r(0.. $a){print map{$b[$r][$_]} (0..$a)}select $&,$&, $&,.4}while(&g)

Update - Using a few tricks I picked up from teamster_jr's wonderful version below (and a few golf tricks I always seem to forget to implement, I knocked it down to 366 characters.

$a=40;$\=$/;@a=0..$a;for$r(@a){$b[$r][$_] =!int rand 10?0:$"for@a}sub g{my($d,@e); for$r(@a){for(@a){$h=0;for$i(-1..1){for$j (-1..1){$f=$r+$i;$k=$_+$j;$h++if$b[$f][$k ]ne$"&&!($f<0||$f>$a||$k<0||$k>$a||!$i&&! $j)}}$*=$e[$r][$_]=$h==3?0:$b[$r][$_]ne$" &&$h==2?0:$";$d++if$*ne$b[$r][$_]}}@b=@e; $d}{print"\ec",++$g,$\,map{@{$b[$_]},$\} @a;select$&,$&,$&,.1;redo if&g}

Final update - by eliminating a few niceties (printing the generation number, allowing the program to terminate by itself), and accidentally creating a fractal, I chopped it down to 282 characters (not counting new lines).

@a=0..40;for$r(@a){$b[$r][$_]=!int rand 9?0:$"for@a}{print"\ec",map{@{$b[$_]}, $/}@a;select$&,$&,$&,.1;my@e;for$r(@a){ for(@a){$h=0;for$i($r-1..$r+1){for$j($_ -1..$_+1){$h++if$b[$i][$j]ne$"&!($i<0|$i >40|$j<0|$j>40)&($i^$r||$j^$_)}}$e[$r][ $_]=$h==3|$h==2&$b[$r][$_]ne$"?0:$"}}@b= @e;redo}

Final, final update Fine, fine, since this node is still getting some play and I have continued to tinker with it, here's a 270 character version. How low can I go, indeed.

@a=0..40;$b[$_]=[map{rand 9<1?0:$"}@a] for@a;{print"\ec",map{@{$b[$_]},$/}@a; select$&,$&,$&,.1;@e=@b;for$r(@a){for(@a ){$h=0;for$i($r-1..$r+1){for$j($_-1..$_+1) {$h++if$e[$i][$j]ne$"&!($i<0|$i>40|$j<0| $j>40)&($i^$r||$j^$_)}}$b[$r][$_]=$h==3| $h==2&$e[$r][$_]ne$"?0:$"}}redo}

Now on to other things.

Replies are listed 'Best First'.
Re: Game of Life
by ikegami (Patriarch) on Sep 21, 2006 at 19:21 UTC

    Windows users, replace

Re: Game of Life
by teamster_jr (Curate) on Sep 22, 2006 at 09:27 UTC
    print"\ec";for$b(0..15){$e[$b].=int 2*rand for 0..($h=30)}{$b=-1;for$f +(@ a=@e){$e[++$b]="";print"\e[$b\H$f$/";for$c(0..$h){$l=0;for($b-1..$b+1) +{$ _=$_>0&&$_<$h?substr$a[$_],$c?$c-1:$c,$c==$h?2:$c?3:2:1;$l+=tr/0//}$l- +=$ d=!substr$f,$c,1;$e[$b].=$l==3||$l==2&&$d?0:$"}}select$q,$q,$q,0.1;red +o}
    288 chars ;)
    from here

    edit: ps. probably needs vt100 terminal

Re: Game of Life
by halley (Prior) on Sep 28, 2006 at 13:16 UTC
    Thank goodness it was you, and not The Damian. The mental strain of imagining what would happen with Damian Conway meeting John Conway. ACME::Life would probably involve nothing but ampersands and white space, yet allow simulated glider cells to spawn subprocesses which interpret Parrot bytecodes constituting a Klingon translator. Or something.

    [ e d @ h a l l e y . c c ]

Re: Game of Life
by tybalt89 (Monsignor) on Nov 14, 2019 at 21:48 UTC
    print"\ec";$_=($t=1x32 .$/).((1 .2x30 ."1\n")x15).$t;s/2/0|rand 2/ge; s/(?<=(...)\H{30}(.)).(?=(.)\H{30}(...))/substr"11$&0".1x5, "$1$2$3$4"=~tr|0||,1/ge,select$q,$q,$q,.1while print"\e[H",y/1/ /r

    194 ?

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: obfuscated [id://574225]
Approved by Paladin
Front-paged by grinder
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2023-06-11 00:54 GMT
Find Nodes?
    Voting Booth?
    How often do you go to conferences?

    Results (40 votes). Check out past polls.