Allow for arbitrary number of rules
This commit is contained in:
parent
a547340b4e
commit
22b72824b7
1 changed files with 14 additions and 4 deletions
18
cellaut.pl
18
cellaut.pl
|
@ -1,12 +1,22 @@
|
||||||
#!/usr/bin/env perl
|
#!/usr/bin/env perl
|
||||||
use strict;
|
use strict;
|
||||||
use warnings;
|
use warnings;
|
||||||
|
use POSIX;
|
||||||
|
|
||||||
# Get arguments
|
# Get arguments
|
||||||
my $gens = $ARGV[0];
|
my $gens = $ARGV[0];
|
||||||
my $width = $ARGV[1];
|
my $width = $ARGV[1];
|
||||||
my $rule = $ARGV[2];
|
my $rule = $ARGV[2];
|
||||||
|
|
||||||
|
# Set num_rules to 255 and bits to 8 by default
|
||||||
|
my $num_rules = $ARGV[3];
|
||||||
|
my $bits = 8;
|
||||||
|
if($num_rules) {
|
||||||
|
$bits = floor(log($num_rules)/log(2)) + 1;
|
||||||
|
} else {
|
||||||
|
$num_rules = 255;
|
||||||
|
}
|
||||||
|
|
||||||
# Set up all possible rules 0 - 255
|
# Set up all possible rules 0 - 255
|
||||||
# 0 = 00000000
|
# 0 = 00000000
|
||||||
# 1 = 00000001
|
# 1 = 00000001
|
||||||
|
@ -14,8 +24,8 @@ my $rule = $ARGV[2];
|
||||||
# 254 = 11111110
|
# 254 = 11111110
|
||||||
# 255 = 11111111
|
# 255 = 11111111
|
||||||
my @rules;
|
my @rules;
|
||||||
for (my $index = 255; $index >=0; $index--) {
|
for (my $index = $num_rules; $index >=0; $index--) {
|
||||||
push @rules, [get_bin($index, 8)];
|
push @rules, [get_bin($index, $bits)];
|
||||||
}
|
}
|
||||||
|
|
||||||
# extract the correct binary number for this position and this rule
|
# extract the correct binary number for this position and this rule
|
||||||
|
@ -53,11 +63,11 @@ sub set_up_init {
|
||||||
|
|
||||||
# Print usage
|
# Print usage
|
||||||
sub usage {
|
sub usage {
|
||||||
print "$0 <number of generations> <width> <rule number (0-255)>\n";
|
print "$0 <number of generations> <width> <rule number (0-255)> [num rules]\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
# Some error handling
|
# Some error handling
|
||||||
if (scalar @ARGV < 3 or $rule > 255) {
|
if (scalar @ARGV < 3 or $rule > $num_rules) {
|
||||||
usage;
|
usage;
|
||||||
exit 1;
|
exit 1;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue