Forest fire: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 26:
 
=={{header|6502 Assembly}}==
<langsyntaxhighlight lang="asm"> ORG $4357
; SYS 17239 or CALL 17239
 
Line 447:
; end COMMODORE 64 specific
 
</syntaxhighlight>
</lang>
 
=={{header|8086 Assembly}}==
Line 454:
It uses a field size of 320x200 (the CGA screen) and runs at about one frame per second on a 20mhz 286.
 
<langsyntaxhighlight lang="asm"> ;;; Simulation settings (probabilities are P/65536)
probF: equ 7 ; P(spontaneous combustion) ~= 0.0001
probP: equ 655 ; P(spontaneous growth) ~= 0.01
Line 642:
rnddat: resb 4 ; RNG state
stack: resw 128 ; Stack space
.top: equ $</langsyntaxhighlight>
 
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Numerics.Float_Random; use Ada.Numerics.Float_Random;
with Ada.Text_IO; use Ada.Text_IO;
 
Line 714:
Put (Forest);
end loop;
end Forest_Fire;</langsyntaxhighlight>
Sample output:
<pre style="height:30ex;overflow:scroll">
Line 815:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny].}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of '''format'''[ted] ''transput''.}}
<langsyntaxhighlight lang="algol68">LONG REAL tree prob = 0.55, # original tree probability #
f prob = 0.01, # new combustion probability #
p prob = 0.01; # tree creation probability #
Line 873:
printf(($gl$, 2 UPB world * "-"))
OD
)</langsyntaxhighlight>
Output:
<pre>
Line 916:
This implementation uses AutoHotkey's pseudo-arrays to contain each cell.
The size of the (square) map, probabilities, and characters which correspond to burning, tree, or empty can be edited at the beginning of the script.
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>
; The array Frame1%x%_%y% holds the current frame. frame2%x%_%y%
; is then calculated from this, and printed. frame2 is then copied to frame1.
Line 1,069:
return 0
}
</syntaxhighlight>
</lang>
Sample Output using the default settings:
<pre style="height:35ex;overflow:scroll;">
Line 1,132:
==={{header|Applesoft BASIC}}===
{{trans|6502 Assembly}}
<langsyntaxhighlight lang="gwbasic"> 100 FOR I = 17239 TO 17493
110 READ B
120 NEXT
Line 1,149:
250 DATA 68,208,169,169,153,208,165,198,6,208,14,198,7,208,10,169,23,133,6,169
260 DATA 39,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</langsyntaxhighlight>
 
==={{header|BASIC256}}===
[[File:Forest fire BASIC-256.gif|right|thumb|Forest fire animation: p=0.03, p/f=1000]]
<langsyntaxhighlight lang="basic256">N = 150 : M = 150 : P = 0.03 : F = 0.00003
 
dim f(N+2,M+2) # 1 tree, 0 empty, 2 fire
Line 1,190:
next y
next x
end while</langsyntaxhighlight>
 
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> VDU 23,22,400;400;16,16,16,128
OFF
Line 1,229:
NEXT x%
old&() = new&()
UNTIL FALSE</langsyntaxhighlight>
Output:
<p>
Line 1,235:
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">'[RC] Forest Fire
'written for FreeBASIC
'Program code based on BASIC256 from Rosettacode website
Line 1,342:
Print " You entered ESC - goodbye "
Print " Press any key to exit "
Sleep</langsyntaxhighlight>
 
==={{header|GFA Basic}}===
 
<langsyntaxhighlight lang="basic">
width%=80
height%=50
Line 1,485:
CLOSEW 1
RETURN
</syntaxhighlight>
</lang>
 
==={{header|PureBasic}}===
<langsyntaxhighlight PureBasiclang="purebasic">; Some systems reports high CPU-load while running this code.
; This may likely be due to the graphic driver used in the
; 2D-function Plot().
Line 1,654:
ForEver
EndIf
EndIf</langsyntaxhighlight>
[[Image:Forest_Fire_in_PureBasic,_frame_300.png]]
 
==={{header|REALbasic}}===
This example puts all of the forestry logic into a Thread class. This allows the UI to remain responsive while the Thread does all the work in the background. We create a Thread by subclassing the Thread object in the IDE, in this case creating ''forestfire'' as a subclass of the Thread object and put the following code in its ''Run()'' event:
<langsyntaxhighlight lang="realbasic">
Sub Run()
//Handy named constants
Line 1,725:
Wend
End Sub
</syntaxhighlight>
</lang>
As you can see, this Thread is expecting a Window object called Window1 with several other objects within it. The IDE will automatically create a Window object called Window1 when a new GUI application is created. Our Window1 has 5 objects (widgets) in it: a Canvas (for displaying graphics), three sliders, and a pushbutton.
<langsyntaxhighlight lang="realbasic">
Sub Open()
//First method to run on the creation of a new Window. We instantiate an instance of our forestFire thread and run it.
Line 1,739:
stop = True
End Sub
</syntaxhighlight>
</lang>
[[Image:ForestFireRB.PNG]]
 
==={{header|Run BASIC}}===
<langsyntaxhighlight lang="runbasic">graphic #g, 200,200
dim preGen(200,200)
dim newGen(200,200)
Line 1,767:
next x
next gen
render #g</langsyntaxhighlight>
[[File:ForestFire.png]]
 
Line 1,782:
 
(2) this program differs from most BASIC examples on Rosetta Code, but resembles most real BASIC programs of more than about 20 lines, in that the line numbers do not always go up smoothly in multiples of ten.
<langsyntaxhighlight lang="basic"> 10 DIM F$(20,30)
20 DIM N$(20,30)
30 LET INIT=.5
Line 1,853:
1030 LET I$=I$+CHR$ (128+CODE S$(K))
1040 NEXT K
1050 RETURN</langsyntaxhighlight>
{{out}}
Screenshot [http://www.edmundgriffiths.com/zx81forest.jpg here].
Line 1,861:
This program sits behind a Windows form with fixed borders, the only component of which is a timer (named Timer1, set to something like 50 or 100ms depending on the speed the user wants to see it). Other constant values (the probabilities and the window dimensions) can be set at the top of the code.
 
<langsyntaxhighlight lang="vbnet">Public Class ForestFire
Private _forest(,) As ForestState
Private _isBuilding As Boolean
Line 1,964:
Me.Text = "Gen " & _gen.ToString() & " @ " & (_gen / (_sw.ElapsedMilliseconds / 1000)).ToString("F02") & " FPS: Forest Fire"
End Sub
End Class</langsyntaxhighlight>
 
==={{header|ZX Spectrum Basic}}===
Line 1,970:
 
A screenshot of the program running can be found [http://www.edmundgriffiths.com/spectrumforestfire.jpg here].
<langsyntaxhighlight lang="zxbasic"> 10 PAPER 6: CLS
20 DIM n$(20,30)
30 LET init=.5
Line 2,025:
540 NEXT j
550 NEXT i
560 GO TO 260</langsyntaxhighlight>
 
=={{header|Batch File}}==
Line 2,036:
</pre>
Default is <code> 10 50 5 5 </code>
<langsyntaxhighlight lang="dos">
@echo off
setlocal enabledelayedexpansion
Line 2,153:
)
exit /b
</syntaxhighlight>
</lang>
{{out}}
'''Sample Default Output'''
Line 2,242:
{{libheader|SDL}}
 
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
Line 2,489:
free(field[0]); free(field[1]);
exit(EXIT_SUCCESS);
}</langsyntaxhighlight>
===Console version===
C99. Uncomment srand() for variaty, usleep() for slower speed.
<langsyntaxhighlight Clang="c">#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Line 2,557:
 
evolve(w, h);
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
 
<langsyntaxhighlight lang="csharp">using System;
using System.Drawing;
using System.Drawing.Drawing2D;
Line 2,721:
}
}
}</langsyntaxhighlight>
 
=={{header|C++}}==
[[File:ForestFireCpp.png|300px]]
<langsyntaxhighlight lang="cpp">
#include <windows.h>
#include <string>
Line 3,006:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
</lang>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">import ceylon.random { DefaultRandom }
 
abstract class Cell() of tree | dirt | burning {}
Line 3,132:
forest.evolve();
}
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">
<lang Clojure>
(def burn-prob 0.1)
(def new-tree-prob 0.5)
Line 3,214:
(forest-fire)
 
</syntaxhighlight>
</lang>
 
example output
Line 3,233:
=={{header|COBOL}}==
{{works with|OpenCOBOL}}
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. forest-fire.
 
Line 3,400:
COMPUTE rand-num =
FUNCTION MOD(FUNCTION RANDOM * 100000, 10000)
.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defvar *dims* '(10 10))
(defvar *prob-t* 0.5)
(defvar *prob-f* 0.1)
Line 3,476:
(progn (format t "~%------ Generation ~d ------~%" (1+ i))
(print-forest forest)))))
</syntaxhighlight>
</lang>
Example results:
<langsyntaxhighlight lang="lisp">CL-USER>(defparameter *forest* (make-new-forest))
CL-USER>(simulate *forest* 5)
------ Initial forest ------
Line 3,552:
TTT TTT T
NIL
</syntaxhighlight>
</lang>
 
=={{header|D}}==
===Textual Version===
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.string, std.algorithm;
 
enum treeProb = 0.55; // Original tree probability.
Line 3,609:
world.swap(nextWorld);
}
}</langsyntaxhighlight>
{{out}}
<pre> T T T#TT T TT TT TTTT TT TTT T TT T# T T TT TT TTTTT
Line 3,650:
===Graphical Version===
{{libheader|simpledisplay}}
<langsyntaxhighlight lang="d">import std.stdio, std.random, std.algorithm, std.typetuple,
simpledisplay;
 
Line 3,717:
painter.drawImage(Point(0, 0), img);
});
}</langsyntaxhighlight>
 
=={{header|Déjà Vu}}==
<langsyntaxhighlight lang="dejavu">#chance of empty->tree
set :p 0.004
#chance of spontaneous tree combustion
Line 3,796:
step
 
run</langsyntaxhighlight>
{{out}}
<pre>T.T.T...T..T..TT.T.T.
Line 3,838:
[https://easylang.online/apps/forest-fire.html Run it]
 
<syntaxhighlight lang="text">p_fire = 0.00002
p_tree = 0.002
#
Line 3,906:
call update
timer 0.2
.</langsyntaxhighlight>
 
=={{header|Emacs Lisp}}==
<langsyntaxhighlight lang="lisp">#!/usr/bin/env emacs -script
;; -*- lexical-binding: t -*-
;; run: ./forest-fire forest-fire.config
Line 4,006:
 
(simulate-forest (elt command-line-args-left 0))
</syntaxhighlight>
</lang>
 
The configuration file controls the simulation.
<langsyntaxhighlight lang="lisp">((rows . 10)
(cols . 45)
(time . 100)
Line 4,015:
(f . 0.001) ;; probability tree ignites
(p . 0.01) ;; probability empty space fills with a tree
(tree . 0.5)) ;; initial probability of tree in a new world</langsyntaxhighlight>
 
{{out}}
Line 4,134:
Not even text graphics. Notice the use of random:seed/1 when creating a tree. Without it all calls to random:uniform/1 gave the same result for each tree.
 
<syntaxhighlight lang="erlang">
<lang Erlang>
-module( forest_fire ).
 
Line 4,207:
tree_init( Tree_probalility, Random ) when Tree_probalility > Random -> tree;
tree_init( _Tree_probalility, _Random ) -> empty.
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,293:
=={{header|F_Sharp|F#}}==
This implementation can be compiled or run in the interactive F# shell.
<langsyntaxhighlight lang="fsharp">open System
open System.Diagnostics
open System.Drawing
Line 4,382:
[<EntryPoint>]
let main args = ForestFire.main args
#endif</langsyntaxhighlight>
[[File:ForestFire-FSharp.png]]
 
=={{header|Factor}}==
{{works with|Factor|0.99 Development version 2019-07-10}}
<langsyntaxhighlight lang="factor">USING: combinators grouping kernel literals math math.matrices
math.vectors prettyprint random raylib.ffi sequences ;
IN: rosetta-code.forest-fire
Line 4,471:
end-drawing
next-forest
] until drop close-window</langsyntaxhighlight>
{{out}}
[https://gfycat.com/boilingenviousboto]
Line 4,477:
=={{header|Forth}}==
{{works with|Gforth|0.7.3}}
<langsyntaxhighlight lang="forth">30 CONSTANT WIDTH
30 CONSTANT HEIGHT
WIDTH HEIGHT * CONSTANT SIZE
Line 4,540:
DUP .FOREST \ print the current state
SWAP ; \ prepare for next iteration
: GO A B BEGIN (GO) AGAIN ;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|95 and later}}
 
<langsyntaxhighlight lang="fortran">module ForestFireModel
implicit none
 
Line 4,727:
end subroutine forestfire_print
 
end module ForestFireModel</langsyntaxhighlight>
 
<langsyntaxhighlight lang="fortran">program ForestFireTest
use ForestFireModel
implicit none
Line 4,746:
call forestfire_destroy(f)
 
end program ForestFireTest</langsyntaxhighlight>
 
=={{header|Go}}==
Text. The program prints the configuration, waits for the Enter key, and prints the next. It makes a pretty good animation to just hold down the Enter key.
<langsyntaxhighlight lang="go">package main
 
import (
Line 4,833:
}
}
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Control.Monad (replicateM, unless)
import Data.List (tails, transpose)
import System.Random (randomRIO)
Line 4,894:
main :: IO ()
main = evolveForest 6 50 3</langsyntaxhighlight>
{{Out}} Sample:
<pre>>>>>>> 1:
Line 4,921:
=={{header|Icon}} and {{header|Unicon}}==
[[File:Forestfire-Unicon.png|400px|thumb|right|Forest fire 400 x 400 rounds=500 p.initial=0.100000 p/f=0.010000/0.000200 fps=1.495256]]
<langsyntaxhighlight Iconlang="icon">link graphics,printf
 
$define EDGE 0
Line 4,993:
procedure probability(P) #: succeed with probability P
if ?0 <= P then return
end</langsyntaxhighlight>
 
{{libheader|Icon Programming Library}}
Line 5,000:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">NB. states: 0 empty, 1 tree, _1 fire
dims =:10 10
 
Line 5,020:
smoutput ' #o' {~ forest=. step forest
end.
)</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight lang="j"> run 2
##### #
Line 5,043:
## # #
o #
o# # </langsyntaxhighlight>
 
Note that I have used an artificially small grid here, and that I ran this several times until I could find one that had a fire from the start. Also, the current revision of this code does not show the starting state, though that would be easily changed.
Line 5,052:
 
=={{header|JAMES II/Rule-based Cellular Automata}}==
<langsyntaxhighlight lang="j2carules">@caversion 1;
 
dimensions 2;
Line 5,068:
 
// a tree is hit by lightning with a change of f = 0.006 %
rule{TREE} [0.00006] : -> BURNING;</langsyntaxhighlight>
The starting configuration cannot be given in the modeling language since the concepts of the ''model'' and its ''parameters'' (which includes the starting configuration) are separate in JAMES II.
 
Line 5,074:
{{works with|Java|1.5+}}
===Text===
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
Line 5,210:
processNPrint(land, 10);
}
}</langsyntaxhighlight>
===Graphics===
See: [[Forest fire/Java/Graphics]]
Line 5,220:
Functional approach using [https://lodash.com/ lodash]
 
<langsyntaxhighlight lang="javascript">"use strict"
 
const _ = require('lodash');
Line 5,288:
}, 20);
 
</syntaxhighlight>
</lang>
 
 
===JavaScript===
<langsyntaxhighlight lang="javascript">var forest = {
X: 50,
Y: 50,
Line 5,355:
afterLoad(forest);
}, 100);
</syntaxhighlight>
</lang>
 
To actually see it work we need a small demo page with HTML5 compliant code:
 
<langsyntaxhighlight lang="html5"><!DOCTYPE html>
<html>
<head>
Line 5,373:
</body>
</html>
</syntaxhighlight>
</lang>
 
The output is a (mostly fluent) animation of the area.
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf
 
@enum State empty tree fire
Line 5,438:
end
 
evolution()</langsyntaxhighlight>
 
{{out}}
Line 5,477:
=={{header|Lua}}==
This program uses the Lua Curses library for graphics, although changing the code to avoid such dependency is easy.
<syntaxhighlight lang="lua">
<lang Lua>
-- ForestFire automaton implementation
-- Rules: at each step:
Line 5,575:
socket.sleep(naptime)
until false
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica is good at working with cellular automata -- especially 2-color 1-dimensional cellular automata. The automaton function is awkward yet very powerful. This code implements a 3-color 2-dimensional cellular automaton with 9-cell neighbourhoods using a custom cell evolution function. There is probably a rule number specification that can replace the custom evolution function and make this simpler and faster. But this works well enough. The last line of code plots the state of the forest after the 300th step.
 
<langsyntaxhighlight Mathematicalang="mathematica">evolve[nbhd_List, k_] := 0 /; nbhd[[2, 2]] == 2 (*burning->empty*)
evolve[nbhd_List, k_] := 2 /; nbhd[[2, 2]] == 1 && Max@nbhd == 2 (*near_burning&nonempty->burning*)
evolve[nbhd_List, k_] := RandomChoice[{f, 1 - f} -> {2, nbhd[[2, 2]]}] /; nbhd[[2, 2]] == 1 && Max@nbhd < 2 (*spontaneously combusting tree*)
Line 5,587:
r = 100; c = 100; p = 10^-2; f = 10^-4;
init = RandomInteger[BernoulliDistribution[0.05], {r, c}];
MatrixPlot[CellularAutomaton[{evolve, {}, {1, 1}}, {init, 0}, {{{300}}}], ColorRules -> {0 -> White, 1 -> Green, 2 -> Red}, Frame -> False]</langsyntaxhighlight>
[[File:ForestFire-Mathematica.png]]
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function forest_fire(f,p,N,M)
% Forest fire
if nargin<4;
Line 5,619:
G = G + (F==3); % empty after burn
F = G;
end; </langsyntaxhighlight>
 
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang="nim">import random, os, sequtils, strutils
 
randomize()
Line 5,676:
if chance(BurnProb): univNew[y][x] = Fire
univ = univNew
sleep 200</langsyntaxhighlight>
 
=={{header|OCaml}}==
Line 5,683:
This example uses a curses display (with the [http://www.nongnu.org/ocaml-tmk/ ocaml-curses] bindings).
 
<langsyntaxhighlight lang="ocaml">open Curses
 
let ignite_prob = 0.02
Line 5,755:
Unix.sleep 1;
done;
endwin()</langsyntaxhighlight>
 
You can execute this script with:
Line 5,761:
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(import (lib gl))
(import (otus random!))
Line 5,814:
(glEnd)
(gl:set-userdata step))))
</syntaxhighlight>
</lang>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">step(M,p,f)={
my(m=matsize(M)[1],n=matsize(M)[2]);
matrix(m,n,i,j,
Line 5,841:
while(1,print(M=step(M,p,f)))
};
burn(5,.1,.03)</langsyntaxhighlight>
 
=={{header|Perl}}==
Requires terminal that understands ANSI escape sequences:<langsyntaxhighlight Perllang="perl">
use 5.10.0;
 
Line 5,907:
}
 
forest while (1);</langsyntaxhighlight>
 
===Alternate Perl Solution===
<langsyntaxhighlight Perllang="perl">#!/usr/bin/perl
 
use strict; # http://www.rosettacode.org/wiki/Forest_fire
Line 5,932:
$forest =~ s/e/ rand() < $p ? 2 : 1 /ge; # rule 4) empty cell to tree
select undef, undef, undef, 0.1; # comment out for full speed
}</langsyntaxhighlight>
 
=={{header|Phix}}==
{{libheader|Phix/pGUI}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
-- demo\rosetta\Forest_fire.exw
Line 6,045:
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php"><?php
 
define('WIDTH', 10);
Line 6,160:
return rand(0, 100) < $prob;
}
</syntaxhighlight>
</lang>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l")
 
(scl 3)
Line 6,205:
(put This @ T)
(=: burn)
(=: tree) ) ) ) ) ) )</langsyntaxhighlight>
Use:
<pre>(forestFire 26 0.5 0.01 0.001)</pre>
 
=={{header|PostScript}}==
<langsyntaxhighlight PostScriptlang="postscript">%!PS-Adobe-3.0
%%BoundingBox: 0 0 400 400
 
Line 6,273:
 
1000 { drawforest showpage iter } repeat
%%EOF</langsyntaxhighlight>
 
=={{header|Python}}==
Just hit return to advance the simulation, or enter an integer to advance that integer amount of 'frames'.
Entering 'p' will print the grid, and 'q' will quit. A summary of the grids status is printed before each prompt for input.
<langsyntaxhighlight lang="python">'''
Forest-Fire Cellular automation
See: http://en.wikipedia.org/wiki/Forest-fire_model
Line 6,360:
break
grid = gnew(grid)
iter +=1</langsyntaxhighlight>
 
'''Sample output'''
Line 6,420:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require 2htdp/universe)
(require 2htdp/image)
Line 6,479:
[to-draw render-forest]))
 
(forest-fire 0 1/8 1/1024 50)</langsyntaxhighlight>
 
I'll tweak with the parameters for a bit, and when I have some nice
Line 6,490:
{{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" perl6line>my $RED = "\e[1;31m";
my $YELLOW = "\e[1;33m";
my $GREEN = "\e[1;32m";
Line 6,560:
$forest.step;
}
}</langsyntaxhighlight>
 
===SDL2 Animation===
An alternate version implemented in SDL2.
 
<syntaxhighlight lang="raku" perl6line>use NativeCall;
use SDL2::Raw;
 
Line 6,691:
}
$fps
}</langsyntaxhighlight>
 
=={{header|REXX}}==
Line 6,710:
└───────────────────────────────────────────────────────────────────┘
</pre>
<langsyntaxhighlight lang="rexx">/*REXX program grows and displays a forest (with growth and fires caused by lightning).*/
parse value scrSize() with sd sw . /*the size of the terminal display. */
parse arg generations birth lightning rSeed . /*obtain the optional arguments from CL*/
Line 6,759:
return random(1, field) <= lightning /*lightning ignition*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
p: return word(arg(1), 1) /*pick─a─word: first or second word.*/</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''scrSize''' &nbsp; REXX program (or BIF) &nbsp; which is used to determine the screen size of the terminal (console).
<br>The &nbsp; '''SCRSIZE.REX''' &nbsp; REXX program is included here &nbsp; ──► &nbsp; &nbsp;[[SCRSIZE.REX]]. <br><br>
Line 6,822:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Forest fire
 
Line 6,907:
label1 { setpicture(p1) show() }
return
</syntaxhighlight>
</lang>
Output:
 
Line 6,913:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Forest_Fire
Neighborhood = [-1,0,1].product([-1,0,1]) - [0,0]
States = {empty:" ", tree:"T", fire:"#"}
Line 6,956:
forest.evolve
forest.display
end</langsyntaxhighlight>
Sample Output:
<pre style="height:64ex;overflow:scroll">
Line 7,075:
{{libheader|rand}}
{{libheader|ansi_term}}
<langsyntaxhighlight lang="rust">extern crate rand;
extern crate ansi_term;
 
Line 7,233:
write!(writer, "{}", String::from_utf8_lossy(&output.stdout)).unwrap();
}
</syntaxhighlight>
</lang>
 
=={{header|Sather}}==
<langsyntaxhighlight lang="sather">class FORESTFIRE is
private attr fields:ARRAY{ARRAY{INT}};
private attr swapu:INT;
Line 7,431:
end;
 
end;</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.util.Random
 
class Forest(matrix:Array[Array[Char]]){
Line 7,461:
val EMPTY='.'
def apply(x:Int=30, y:Int=15)=new Forest(Array.tabulate(y, x)((y,x)=> if (Random.nextDouble<0.5) TREE else EMPTY))
}</langsyntaxhighlight>
 
<langsyntaxhighlight lang="scala">object ForestFire{
def main(args: Array[String]): Unit = {
var l=Forest()
Line 7,471:
}
}
}</langsyntaxhighlight>
Sample output:
<pre>.T..TTT.TT .T..TTT.TT TT..TTT.TT TT..TTTTTT TT..TTTTTT
Line 7,486:
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">define w = `tput cols`.to_i-1
define h = `tput lines`.to_i-1
define r = "\033[H"
Line 7,546:
}
 
loop { init_forest() }</langsyntaxhighlight>
 
{{trans|Raku}}
OO approach:
<langsyntaxhighlight lang="ruby">define RED = "\e[1;31m"
define YELLOW = "\e[1;33m"
define GREEN = "\e[1;32m"
Line 7,621:
forest.show
forest.step
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
 
# Build a grid
Line 7,701:
printGrid
if {[gets stdin line] < 0} break
}</langsyntaxhighlight>
Sample output:
<pre>
Line 7,752:
=={{header|uBasic/4tH}}==
It's a small forest, since it's a small interpreter.
<syntaxhighlight lang="text">B = 1 ' A burning tree
E = 16 ' An empty space
T = 256 ' A living tree
Line 7,858:
Next ' Try next row
 
Return (T) ' No burning trees found</langsyntaxhighlight>
{{out}}
<pre> @ @ @ @
Line 7,895:
 
Note: In order to display the graphics characters correctly, use DOS (OEM) font such as "Terminal".
<langsyntaxhighlight lang="vedit">#1 = 25 // height of the grid
#2 = 60 // width of the grid
#3 = 2 // probability of random fire, per 1000
Line 7,969:
#93 = 0x7fffffff % 48271
#90 = (48271 * (#90 % #92) - #93 * (#90 / #92)) & 0x7fffffff
return ((#90 & 0xffff) * #91 / 0x10000)</langsyntaxhighlight>
 
Sample output, 10th generation:
Line 8,005:
Text. The program prints the configuration, waits for enter key, and prints the next. It makes a pretty good animation to just hold down the enter key.
{{trans|go}}
<langsyntaxhighlight lang="vlang">import rand
import strings
import os
Line 8,088:
}
}
}</langsyntaxhighlight>
 
=={{header|Wren}}==
{{trans|Go}}
<langsyntaxhighlight lang="ecmascript">import "random" for Random
import "io" for Stdin
 
Line 8,154:
step.call(odd, even)
Stdin.readLine()
}</langsyntaxhighlight>
10,333

edits