Jump to content

Forest fire: Difference between revisions

Updated to compile with Nim 1.4. Some formatting and case changes.
(Updated to compile with Nim 1.4. Some formatting and case changes.)
Line 5,597:
=={{header|Nim}}==
{{trans|C}}
<lang nim>import mathrandom, os, sequtils, strutils
 
randomize()
 
type State {.pure.} = enum Empty, Tree, Fire
 
const
dispDisp: array[State, string] = [" ", "\e[32m/\\\e[m", "\e[07;31m/\\\e[m"]
treeProbTreeProb = 0.01
burnProbBurnProb = 0.001
 
proc chance(prob: float): bool {.inline.} = randomrand(1.0) < prob
 
# Set the size
var w, h: int
if paramCount() >= 2:
w = parseInt paramStr (1).parseInt
h = parseInt paramStr (2).parseInt
if w <= 0: w = 30
if h <= 0: h = 30
 
iterator fields(a = (0, 0), b = (h-1, w-1)): tuple[y, x: int] =
## Iterate over fields in the universe
iterator fields(a = (0,0), b = (h-1,w-1)) =
for y in max(a[0], 0) .. min(b[0], h-1):
for x in max(a[1], 0) .. min(b[1], w-1):
yield (y, x)
 
# Create a sequence with an initializer
proc newSeqWith[T](len: int, init: T): seq[T] =
result = newSeq[T] len
for i in 0 .. <len:
result[i] = init
 
# Initialize
var univ, univNew = newSeqWith(h, newSeq[State] (w))
 
while true:
 
# Show.
stdout.write "\e[H"
for y, x in fields():
stdout.write dispDisp[univ[y][x]]
if x == 0: stdout.write "\e[E"
stdout.flushFile
 
# Evolve.
for y, x in fields():
case univ[y][x]
of Fire:
univNew[y][x] = Empty
of Empty:
if chance treeProb(TreeProb): univNew[y][x] = Tree
of Tree:
for y1, x1 in fields((y-1, x-1), (y+1, x+1)):
if univ[y1][x1] == Fire: univNew[y][x] = Fire
if chance burnProb: univNew[y][x] = Fire
break
if chance(BurnProb): univNew[y][x] = Fire
univ = univNew
sleep 200</lang>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.