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.
Note that this spoiler applies to the original 451 character version. This one's pretty straightforward once you re-format:
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)
But I'll do you one better! It's even more straightforward when you rename the variables to something useful.
my ($board_size, $random_seed, $generation, @board) = (40, 10);
$\=$/;
for my $r (0..$board_size) {
$board[$r][$_] = ! int(rand($random_seed))
? '*'
: $" for(0..$board_size)
}
sub generate {
my ($evolved, @new_board);
for my $r (0..$board_size) {
for (0..$board_size) {
my $neighbors=0;
for my $i (-1..1) {
for my $j (-1..1) {
my ($neighbor_row, $neighbor_col) = ($r + $i, $_ + $j);
next if $neighbor_row < 0 || $neighbor_row > $board_size
|| $neighbor_col < 0 || $neighbor_col > $board_size
|| !( $i || $j );
$neighbors++
if $board[$neighbor_row][$neighbor_col] ne $"
}
}
$new_board[$r][$_] =
( $board[$r][$_] ne $" && $neighbors == 2 ) || $neighbors == 3
? '*'
: $";
$evolved++ if $new_board[$r][$_] ne $board[$r][$_]
}
}
@board=@new_board;
$evolved
}
do{
print`clear`, ++$generation;
for my $r (0..$board_size) {
print map { $board[$r][$_] } (0..$board_size)
}
select $&, $&, $&, .4
} while (&generate)
At this point, I'll leave the rest of the explanation as an exercise for the reader.