Skip to content

Commit

Permalink
Get all three rc-forest-fire variants showing the same thing
Browse files Browse the repository at this point in the history
* Remove yellow "ashes" phase in Perl 5 variant
* Switch to bright red fire in Perl 5 variant
* Switch to tree ideograms in Perl 5 to match NQP and Perl 6
* Switch to fire ideogram (instead of just red tree) in all variants
* Reset color after printing every non-empty cell in Perl 5, like NQP/Perl 6
  • Loading branch information
Geoffrey Broadwell committed Jun 10, 2012
1 parent f3a1716 commit d9b2efb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
3 changes: 2 additions & 1 deletion nqp/rc-forest-fire
@@ -1,11 +1,12 @@
# XXXX: WIP translation from Perl 6, won't compile in NQP yet!

my $RED := "\e[1;31m";
my $GREEN := "\e[0;32m";
my $CLEAR := "\e[0m";
my $Empty := 0;
my $Tree := 1;
my $Burning := 2;
my @show := (' ', '木', $RED ~ '' ~ $CLEAR);
my @show := (' ', $GREEN ~ '木' ~ $CLEAR, $RED ~ '' ~ $CLEAR);

class Forest {
has @!grid;
Expand Down
14 changes: 5 additions & 9 deletions perl5/rc-forest-fire
Expand Up @@ -4,7 +4,7 @@ my ($w, $h, $steps) = @ARGV;
my $r = "\033[H";
my $step = 0;

my ($green, $red, $yellow, $norm) = ("\033[32m", "\033[31m", "\033[33m", "\033[0m");
my ($green, $red, $norm) = ("\033[0;32m", "\033[1;31m", "\033[0m");

my $tree_prob = .01;
my $burn_prob = .001;
Expand All @@ -17,8 +17,7 @@ sub iterate {
for my $j (0 .. $w - 1) {
$new[$i][$j] = $forest[$i][$j];
if ($forest[$i][$j] == 2) {
$new[$i][$j] = 3;
next;
$new[$i][$j] = 0;
} elsif ($forest[$i][$j] == 1) {
if (rand() < $burn_prob) {
$new[$i][$j] = 2;
Expand All @@ -39,8 +38,6 @@ sub iterate {
}
} elsif (rand() < $tree_prob) {
$new[$i][$j] = 1;
} elsif ($forest[$i][$j] == 3) {
$new[$i][$j] = 0;
}
}}
@forest = @new;
Expand All @@ -51,10 +48,9 @@ sub forest {
say $step;
for (@forest) {
for (@$_) {
when(0) { print " "; }
when(1) { print "${green}*"}
when(2) { print "${red}&" }
when(3) { print "${yellow}&" }
when(0) { print " "; }
when(1) { print "${green}木${norm}"}
when(2) { print "${red}火${norm}" }
}
print "\033[E\033[1G";
}
Expand Down
5 changes: 3 additions & 2 deletions perl6/rc-forest-fire
@@ -1,8 +1,9 @@
my $RED = "\e[1;31m";
my $RED = "\e[1;31m";
my $GREEN = "\e[0;32m";
my $CLEAR = "\e[0m";

enum Cell-State <Empty Tree Burning>;
my @show = (' ', '木', $RED ~ '' ~ $CLEAR);
my @show = (' ', $GREEN ~ '木' ~ $CLEAR, $RED ~ '' ~ $CLEAR);

class Forest {
has Cell-State @!grid;
Expand Down

0 comments on commit d9b2efb

Please sign in to comment.