Working script

master
Mikael Nordin 8 years ago
parent c3ae7de2b8
commit b24e7dbba0

@ -3,12 +3,13 @@ use strict;
use warnings; use warnings;
use Data::Dumper; use Data::Dumper;
my $rule = $ARGV[0]; my $gens = $ARGV[0];
my $gens = $ARGV[1]; my $width = $ARGV[1];
my $init = $ARGV[2]; my $rule = $ARGV[2];
# Set up all possible rules 0 - 255 # Set up all possible rules 0 - 255
my @rules; my @rules;
for (my $index = 0; $index < 256; $index++) { for (my $index = 255; $index >=0; $index--) {
push @rules, [get_bin($index, 8)]; push @rules, [get_bin($index, 8)];
} }
@ -25,22 +26,47 @@ sub get_bin{
return @binarr; return @binarr;
} }
my @initial = get_bin($init,$gens); sub set_up_init {
my @init;
my $half = $width / 2;
unless ($width % 2 == 0) {
$half = $half + 0.5;
}
for (my $i = 0; $i < $width; $i++) {
if($i == $half - 1) {
$init[$i] = 0;
} else {
$init[$i] = 1;
}
}
return @init;
}
sub usage {
print "$0 <number of generations> <width> <rule number (0-255)>\n";
}
if (scalar @ARGV < 3 or $rule > 255) {
usage;
exit 1;
}
my @initial = set_up_init;
print @initial; print @initial;
print "\n"; print "\n";
for (my $gen = 0; $gen < $gens; $gen++) { for (my $gen = 0; $gen < $gens; $gen++) {
my @next; my @next;
for (my $elem = 0; $elem < $gens; $elem++) { for (my $elem = 0; $elem < $width ; $elem++) {
# Boundary conditions # Boundary conditions
my $state; my $state;
if($elem == 0) { if($elem == 0 ) {
$state = "0" . $initial[$elem] . $initial[$elem+1]; $state = $initial[$width -1] . $initial[$elem] . $initial[$elem +1 ] ;
} elsif ($elem +1 == $gens) { } elsif ($elem == $width -1 ) {
$state = $initial[$elem -1] . $initial[$elem] . "0"; $state = $initial[$elem-1] . $initial[$elem] . $initial[0];
} else { } else {
$state = $initial[$elem -1] . $initial[$elem] . $initial[$elem+1]; $state = $initial[$elem - 1] . $initial[$elem] . $initial[$elem +1];
} }
$next[$elem] = get_val_at_pos oct("0b".$state); $next[$elem] = get_val_at_pos oct("0b".$state);
} }

Loading…
Cancel
Save