#!/usr/bin/perl
use strict;
my $COLS = 80;
my $ROWS = 60;
my $SLEEP = .04;
my $ITERS = 120;
my @SCHEMES = (
' ..::oo0088@@',
' .:o08@@80o:. ',
'-<##>--<##>- ',
' .o@@@@@@@@o.',
' . o@o . o@o',
'#### ++++ ',
);
while(1) {
my @chars = split('', $SCHEMES[rand(@SCHEMES)]);
my $num = @chars;
my $screen = new_screen($ROWS, $COLS, $num);
my $backup = int(rand(2));
my $i;
while ($i++ < $ITERS) {
$screen = update($screen, $ROWS, $COLS, $num, $backup);
show($screen, \@chars);
select(undef, undef, undef, $SLEEP);
}
}
#-----------------------------------------------------------
sub update {
my $s = shift;
my $rows = shift;
my $cols = shift;
my $num = shift;
my $back = shift;
my $new;
for my $r (0..$rows-1) {
for my $c (0..$cols-1) {
if (forward($s, $r, $c, $rows, $cols, $num)) {
$new->[$r][$c] = ($s->[$r][$c] + 1) % $num;
}
elsif ($back) {
$new->[$r][$c] = (($s->[$r][$c] - 1) + $num) % $num;
}
else {
$new->[$r][$c] = $s->[$r][$c];
}
}
}
return $new;
}
#-----------------------------------------------------------
sub forward {
my $s = shift;
my $row = shift;
my $col = shift;
my $rows = shift;
my $cols = shift;
my $num = shift;
for my $ri (-1..1) {
my $r = ($row + $ri + $rows) % $rows;
for my $ci (-1..1) {
my $c = ($col + $ci + $cols) % $cols;
((($s->[$r][$c] + $num) - $s->[$row][$col]) % $num == 1)
and return 1;
}
}
return;
}
#-----------------------------------------------------------
sub show {
my $s = shift;
my $chars = shift;
for my $line (@$s) {
print join('', map { $chars->[$_] } @$line), "\n";
}
}
#-----------------------------------------------------------
sub new_screen {
my $rows = shift;
my $cols = shift;
my $num = shift;
my $s;
for my $r (0..$rows-1) {
for my $c (0..$cols-1) {
$s->[$r][$c] = int(rand($num));
}
}
return $s;
}