Forest fire: Difference between revisions

m
fixed "copy pasta" link in Commodore BASIC
m (fixed "copy pasta" link in Commodore BASIC)
 
(8 intermediate revisions by 6 users not shown)
Line 1,134:
<syntaxhighlight lang="gwbasic"> 100 FOR I = 17239 TO 17493
110 READ B
120 NEXT POKE I,B
130 CALL 17239NEXT
140 ENDCALL 17239
150 END
150 DATA 162,23,138,32,71,248,165,38,157,60,3,165,39,157,84,3,202,16,239,162,96
160 DATA 134DATA162,24923,134138,132,16071,0248,132165,038,152157,14560,03,200165,20839,251157,23284,1343,1202,22416,128239,208162,96
170 DATA 244DATA134,44249,86134,1921,44160,820,192132,440,84152,192145,440,80200,192208,32251,50232,248134,1621,0224,134,0128,169208
180 DATA 41DATA244,13344,286,133192,25444,16982,83192,13344,484,165192,24944,13380,1192,13332,350,133248,5162,730,16134,1330,255169
190 DATA DATA41,133,2492,138133,134254,45169,7483,168133,1694,15165,144249,2133,105,2241,133,463,185133,605,373,16,133,38255
200 DATA 185DATA133,84249,3138,133134,3945,16074,1168,132169,4415,177144,2,145105,254224,240133,7946,16185,9360,1693,0,164133,4438
210 DATA 145DATA185,25484,1363,81133,3839,37160,461,81132,3844,145177,382,164145,44254,200240,19279,4116,20893,224169,1650,2164,44
220 DATA 133DATA145,0254,165136,381,13338,137,16546,481,13338,2145,13338,254164,2444,105200,42192,13341,4208,224,165,5,73,162
230 DATA 133DATA133,2550,73165,163,133,31,105165,04,133,52,166133,45254,23224,224105,4842,208133,1594,44165,05,19273,16
240 DATA 48DATA133,3255,7673,14416,67133,443,16105,1920,44133,815,192166,9645,198232,8224,48,208,190159,16944,1010,133,8,169192
250 DATA 68DATA48,2083,16976,169144,15367,20844,16516,198192,644,20881,192,1496,198,78,208,10190,169,23101,133,68,169
260 DATA 39,133,7DATA68,208,234169,177169,0153,17208,4165,136198,176,0208,1714,2198,177,4208,20010,200169,1723,0,17,2133,176,4169
270 DATA39,133,7,208,234,177,0,17,4,136,17,0,17,2,17,4,200,200,17,0,17,2,17,4
270 DATA 48,213,16,137,41</syntaxhighlight>
280 DATA48,213,16,137,41</syntaxhighlight>
 
==={{header|BASIC256}}===
Line 1,233 ⟶ 1,234:
<p>
[[File:Forestbbc.gif|200px]]
 
==={{header|Commodore BASIC}}===
{{trans|6502 Assembly}}
With the keyword CALL changed to SYS, the [[Forest_fire#Applesoft_BASIC|Applesoft BASIC]] code works in Commodore BASIC.
<syntaxhighlight lang="gwbasic"> 100 FOR I = 17239 TO 17493
110 READ B
120 POKE I,B
130 NEXT
140 SYS 17239
150 END
160 DATA162,23,138,32,71,248,165,38,157,60,3,165,39,157,84,3,202,16,239,162,96
170 DATA134,249,134,1,160,0,132,0,152,145,0,200,208,251,232,134,1,224,128,208
180 DATA244,44,86,192,44,82,192,44,84,192,44,80,192,32,50,248,162,0,134,0,169
190 DATA41,133,2,133,254,169,83,133,4,165,249,133,1,133,3,133,5,73,16,133,255
200 DATA133,249,138,134,45,74,168,169,15,144,2,105,224,133,46,185,60,3,133,38
210 DATA185,84,3,133,39,160,1,132,44,177,2,145,254,240,79,16,93,169,0,164,44
220 DATA145,254,136,81,38,37,46,81,38,145,38,164,44,200,192,41,208,224,165,2
230 DATA133,0,165,3,133,1,165,4,133,2,133,254,24,105,42,133,4,165,5,73,16
240 DATA133,255,73,16,133,3,105,0,133,5,166,45,232,224,48,208,159,44,0,192
250 DATA48,3,76,144,67,44,16,192,44,81,192,96,198,8,208,190,169,101,133,8,169
260 DATA68,208,169,169,153,208,165,198,6,208,14,198,7,208,10,169,23,133,6,169
270 DATA39,133,7,208,234,177,0,17,4,136,17,0,17,2,17,4,200,200,17,0,17,2,17,4
280 DATA48,213,16,137,41</syntaxhighlight>
 
==={{header|FreeBASIC}}===
Line 3,836 ⟶ 3,860:
=={{header|EasyLang}}==
 
[https://easylang.onlinedev/apps/forest-fire.html Run it]
 
<syntaxhighlight lang="text">
Line 3,847 ⟶ 3,871:
clear
for r = 0 to 99
for c = 0 to 99
i = r * 102 + c + 104
if randomf < 0.5
f[i] = 1
.
.
.
timer 0
#
subr show
for r = 0 to 99
for c = 0 to 99
i = r * 102 + c + 104
h = f[i]
if h <> p[i]
move c + 0.5 r + 0.5
if h = 0
color 100
circle 0.6
elif h = 1
color 151
circle 0.5
else
color 9 * 100 + (18 - 2 * h) * 10
circle 0.5
.
.
.
.
.
.
subr update
swap f[] p[]
for r = 0 to 99
for c = 0 to 99
i = r * 102 + c + 104
if p[i] = 0
f[i] = 0
if randomf < p_tree
f[i] = 1
.
elif p[i] = 1
f[i] = 1
s = p[i - 103] + p[i - 102] + p[i - 101]
s += p[i - 1] + p[i + 1]
s += p[i + 101] + p[i + 102] + p[i + 103]
if s >= 9 or randomf < p_fire
f[i] = 9
.
elif p[i] = 4
f[i] = 0
else
f[i] = p[i] - 1
.
.
.
.
.
on timer
call show
call update
timer 0.2
.
</syntaxhighlight>
Line 4,292 ⟶ 4,316:
{burning,{5,5}}]
</pre>
 
 
=={{header|Evaldraw}}==
 
[[File:Evaldrawforest.gif|right|thumb|Forest fire animation]]
 
Creates a 256x256 pixel forest, try adjusting the probability for ignite, spread, sprout and max neighbors.
 
<syntaxhighlight lang="c">
enum{XSIZ=255, YSIZ=XSIZ} // size of forest
enum{EMPTY=0, TREE=1, BURN=2} // possible states of a cell
static prob_ignite = .000001; // very rare, but remember we have many trees.
static prob_spread = .25; // Fire spread speed/probability if neighbor on fire
static prob_sprout = 0.25; // probability of new tree to sprout
static MAX_NEIGHBORS = 6; // tree refuses to sprout if overcrowded
static forest[2][YSIZ][XSIZ]; // state of pixel
static fuel[2][YSIZ][XSIZ]; // stores fuel (wood) 0-255
static heat[2][YSIZ][XSIZ]; // tree refuses to spout if heat!=0, also, sets draw color.
static arr_numburn[YSIZ][XSIZ]; // number of burning trees for this cell
static arr_numtree[YSIZ][XSIZ]; // number of neighbor trees for this cell
static xoff[8] = {-1,+0,+1,-1,/*NA*/1,-1,+0,+1}; // offsets to find 8-connected neighbors
static yoff[8] = {-1,-1,-1,+0,/*NA*/0,+1,+1,+1};
() { // Main in evaldraw scripts is a unnamed function.
static otim;
tim = klock(); // Time since program start in seconds.
dt=tim-otim; // Deltatime. 1/dt is FPS. 0 in first frame.
otim=tim; // store old time for next dt.
simulate(); // simulate and draw are coupled, since draw also ping-pongs state.
draw();
setcol(0); fillrect(0,YSIZ,XSIZ,15);
setcol(0xffffff); moveto(0,YSIZ); printf("%4.0ffps generation %5.0f", 1 /dt, numframes);
if (bstatus>0) setFire(mousx,mousy);
}// end main
 
draw() {
for(y=0; y<YSIZ; y++)
for(x=0; x<XSIZ; x++)
{
cell = forest[1][y][x];
if (cell == EMPTY) setcol(0);
else if(cell==BURN) setcol(511-.25*fuel[0][y][x],255-3*heat[0][y][x],33);
else if(cell==TREE) setcol(0,64+fuel[0][y][x],0);
setpix(x,y);
// Transfer next simulation state into current ready for next frame
forest[0][y][x] = forest[1][y][x];
heat[0][y][x] = heat[1][y][x];
fuel[0][y][x] = fuel[1][y][x];
// Count neighbors burning and not
numburn = 0; numtree = 0;
for(n=0; n<8; n++) {
ypos=y+yoff[n];
xpos=x+xoff[n];
if (xpos<0 || xpos > XSIZ-1)continue;
if (ypos<0 || ypos > YSIZ-1)continue;
cell = forest[1][ypos][xpos];
if (cell==BURN) numburn++;
else if (cell==TREE) numtree++;
}
arr_numburn[y][x] = numburn;
arr_numtree[y][x] = numtree;
}
}
 
fillrect(x0,y0,w,h) {
x0=int(x0); y0=int(y0); w=int(w) + 1; h=int(h);
for(y=y0;y<=y0+h;y++) { moveto(x0,y); lineto(x0+w,y); }
}
 
simulate() {
for(y=0; y<YSIZ; y++)
for(x=0; x<XSIZ; x++) {
cell = forest[0][y][x];
cellfuel = fuel[0][y][x];
celltemp = heat[0][y][x];
rand=rnd;
numburn = arr_numburn[y][x];
numtree = arr_numtree[y][x];
if (cell == BURN) {
if (cellfuel <= 0) {
forest[1][y][x] = EMPTY;
}
else {
fuel[1][y][x] = cellfuel - 1;
heat[1][y][x] = celltemp + 1;
}
}
else if (cell == TREE) {
if (numburn == 0 && rand < prob_ignite) setFire(x,y);
else if (numburn > 0 && rand < prob_spread) setFire(x,y);
else if(cellfuel < 255) fuel[1][y][x] = cellfuel + 1;
}
else if (cell == EMPTY) {
if ( celltemp > 0 ) heat[1][y][x] = celltemp - 1;
else if (numburn==0 && rand < prob_sprout && numtree <= MAX_NEIGHBORS) setTree(x,y);
}
}
} // end sim
 
setFire(x,y) {
forest[1][y][x] = BURN;
}
 
setTree(x,y) {
forest[1][y][x] = TREE;
}
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 5,912 ⟶ 6,045:
 
===Alternate Perl Solution===
<syntaxhighlight lang="perl">#!/usr/bin/perluse strict;
 
use strict; # http://www.rosettacode.org/wiki/Forest_fire
use warnings;
use feature 'bitwise';
 
my $p = 0.01; # probability of empty -> tree
Line 5,928 ⟶ 6,060:
print "\e[H", $forest =~ tr/0123/ ^#/r, "\n"; # ^=tree #=burning tree
my $n = $forest =~ tr/123/004/r; # 4=a neighbor is burning
$forest |.= 0 x $_ . $n |. substr $n, $_ for 1, $wide - 1 .. $wide + 1;
$forest &.= $mask; # clear borders and trim
$forest =~ tr/1-7/et10e31/; # step to next generation
$forest =~ s/t/ rand() < $f ? 3 : 2 /ge; # rule 3) tree cell to burning
Line 6,492 ⟶ 6,624:
{{works with|rakudo|2015-10-04}}
This version saves a lot of looking around by using four states instead of three; the <tt>Heating</tt> state does a lookahead to track trees that are being heated up by burning trees, so we only ever have to traverse the neighbors of burning trees, not all trees. Also, by only checking the list of burning trees, we can avoid copying the entire forest each iteration, since real forests are mutable.
<syntaxhighlight lang="raku" line>myconstant $RED = "\e[1;31m";
myconstant $YELLOW = "\e[1;33m";
myconstant $GREEN = "\e[1;32m";
myconstant $CLEAR = "\e[0m";
# make sure we clear colors at the end
END print $CLEAR;
enum Cell-State <Empty Tree Heating Burning>;
Line 8,094 ⟶ 8,228:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "io" for Stdin
 
413

edits