My first obfuscated code! It's a pretty straightforward implementation of Conway's Game of Life for any terminal. Probably not as obfuscated as it could be, but then, I'm a pretty clean coder.
use Storable qw(dclone);$r=23;$c=79;for $y(0..$r){for $x(0..$c){$l[$x] +[$y]=0;if(int(rand(7))==0){$l[$x][$y]=1;}}}while(0==0){my @f=@{dclone +(\@l)};print"\033[2J";for $y(0..$r){for $x(0..$c){if($l[$x][$y]){prin +t"O";}else{print" ";}my $n;for $h(-1..1){for $s(-1..1){next if (($s== +0)&&($h==0));$n+=$l[($x+$s)%$c][($y+$h)%$r];}}if($l[$x][$y]==1){if($n +<=1){$f[$x][$y]=0;}elsif($n>=4){$f[$x][$y]=0;}}elsif($n==3){$f[$x][$y +]=1;}}print"\n";}@l=@{dclone(\@f)};sleep 1;}
Alex G. Musician, Perl Hacker, etc.

Replies are listed 'Best First'.
Re: Life!
by jonadab (Parson) on Mar 19, 2004 at 04:37 UTC

    Nice. You've managed to be fundamentally a lot terser than my version, probably by not generalizing so much. Yours can still be golfed down a good bit by using the usual dirty tricks, of course (like, using the trinary ?: operator instead of regular if/elsif/else blocks), but it's already about half as long as mine and probably more portable to boot :-)


    ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print
      you mean, something like this? :)
      use Storable 'dclone';for$y(0..23){$l[$_][$y]=int(rand 7)?0:1for(0..79 +)}while(1){@f=@{dclone\@l};print"\033[2J";for$y(0..23){for$x(0..79){p +rint$l[$x][$y]?O:" ";$n=0;for $h(-1..1){!$_&&!$h or$n+=$l[($x+$_)%79] +[($y+$h)%23]for-1..1}$l[$x][$y]?$n<2||$n>3?$f[$x][$y]=0:0:$n==3?$f[$x +][$y]=1:0}print$/}@l=@{dclone\@f};sleep 1}

      cLive ;-)

        Yeah, something like that. I managed to save another dozen strokes, though...

        use Storable 'dclone';@l=map{[map{int rand 7?0:1}0..23]}0..79;while(1){@f=@{dclone\@l};print"\033[2J";for$y(0..23){for$x(0..79){print$l[$x][$y]?O:$";$n=0;for$h(-1..1){!$_&&!$h or$n+=$l[($x+$_)%79][($y+$h)%23]for-1..1}$l[$x][$y]?$n<2||$n>3?$f[$x][$y]=0:0:$n==3?$f[$x][$y]=1:0}print$/}@l=@{dclone\@f};sleep 1}

        It's probably possible to do still better by devious means, but that's as short as I can make it without changing the logic. I'm not really a golf pro.


        ;$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$;[-1]->();print
      or indeed my version, but it's a hell of a lot more portable!
      ++.
      al
Re: Life!
by Anonymous Monk on Mar 19, 2004 at 20:33 UTC
    This is your exact code, I just did it a little more obfuscated. My only goal was to confuse it up.
    use Storable(dclone);$r=0b10111;$c=0b1001111;for$y(0..$r){for$x(0..$c) +{$l[$x][$y]=0;int(rand(0b111))==0and(do{$l[$x][$y]=1})}}while(1){my(@ +f)=@{dclone(\@l)};print("\e[2J");for$y(0..$r){for$x(0..$c){$l[$x][$y] +?do{print(chr(0b1001111))}:do{print(chr(0b100000))};my($n);for$h(-1.. +1){for$s(-1..1){$s==0and$h==0and(next);$n+=$l[($x+$s)%$c][($y+$h)%$r] +;}}$l[$x][$y]==1?do{$n<=1?do{$f[$x][$y]=0}:$n>=4&&do{$f[$x][$y]=0}}:$ +n==0b11&&do{$f[$x][$y]=1};}print("\n");}@l=@{dclone(\@f);};sleep(1);}