new files
This commit is contained in:
parent
3af7344581
commit
86c034bb8b
1364 changed files with 21352 additions and 0 deletions
63
Task/Forest-fire/Perl/forest-fire.pl
Normal file
63
Task/Forest-fire/Perl/forest-fire.pl
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use 5.10.0;
|
||||
|
||||
my $w = `tput cols` - 1;
|
||||
my $h = `tput lines` - 1;
|
||||
my $r = "\033[H";
|
||||
|
||||
my ($green, $red, $yellow, $norm) = ("\033[32m", "\033[31m", "\033[33m", "\033[m");
|
||||
|
||||
my $tree_prob = .05;
|
||||
my $burn_prob = .0002;
|
||||
|
||||
my @forest = map([ map((rand(1) < $tree_prob) ? 1 : 0, 1 .. $w) ], 1 .. $h);
|
||||
|
||||
sub iterate {
|
||||
my @new = map([ map(0, 1 .. $w) ], 1 .. $h);
|
||||
for my $i (0 .. $h - 1) {
|
||||
for my $j (0 .. $w - 1) {
|
||||
$new[$i][$j] = $forest[$i][$j];
|
||||
if ($forest[$i][$j] == 2) {
|
||||
$new[$i][$j] = 3;
|
||||
next;
|
||||
} elsif ($forest[$i][$j] == 1) {
|
||||
if (rand() < $burn_prob) {
|
||||
$new[$i][$j] = 2;
|
||||
next;
|
||||
}
|
||||
for ( [-1, -1], [-1, 0], [-1, 1],
|
||||
[ 0, -1], [ 0, 1],
|
||||
[ 1, -1], [ 1, 0], [ 1, 1] )
|
||||
{
|
||||
my $y = $_->[0] + $i;
|
||||
next if $y < 0 || $y >= $h;
|
||||
my $x = $_->[1] + $j;
|
||||
next if $x < 0 || $x >= $w;
|
||||
if ($forest[$y][$x] == 2) {
|
||||
$new[$i][$j] = 2;
|
||||
last;
|
||||
}
|
||||
}
|
||||
} elsif (rand() < $tree_prob) {
|
||||
$new[$i][$j] = 1;
|
||||
} elsif ($forest[$i][$j] == 3) {
|
||||
$new[$i][$j] = 0;
|
||||
}
|
||||
}}
|
||||
@forest = @new;
|
||||
}
|
||||
|
||||
sub forest {
|
||||
print $r;
|
||||
for (@forest) {
|
||||
for (@$_) {
|
||||
when(0) { print " "; }
|
||||
when(1) { print "${green}*"}
|
||||
when(2) { print "${red}&" }
|
||||
when(3) { print "${yellow}&" }
|
||||
}
|
||||
print "\033[E\033[1G";
|
||||
}
|
||||
iterate;
|
||||
}
|
||||
|
||||
forest while (1);
|
||||
Loading…
Add table
Add a link
Reference in a new issue