Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -1,54 +1,54 @@
|
|||
import std.stdio, std.random, std.string, std.algorithm;
|
||||
|
||||
enum TREE_PROB = 0.55; // original tree probability
|
||||
enum F_PROB = 0.01; // auto combustion probability
|
||||
enum P_PROB = 0.01; // tree creation probability
|
||||
enum treeProb = 0.55; // Original tree probability.
|
||||
enum fProb = 0.01; // Auto combustion probability.
|
||||
enum cProb = 0.01; // Tree creation probability.
|
||||
|
||||
enum Cell : char { empty=' ', tree='T', fire='#' }
|
||||
alias Cell[][] World;
|
||||
alias World = Cell[][];
|
||||
|
||||
bool hasBurningNeighbours(in World world, in int r, in int c)
|
||||
pure nothrow {
|
||||
foreach (rowShift; -1 .. 2)
|
||||
foreach (colShift; -1 .. 2)
|
||||
if ((r + rowShift) >= 0 && (r + rowShift) < world.length &&
|
||||
(c + colShift) >= 0 && (c + colShift) < world[0].length &&
|
||||
world[r + rowShift][c + colShift] == Cell.fire)
|
||||
return true;
|
||||
return false;
|
||||
pure nothrow @safe @nogc {
|
||||
foreach (immutable rowShift; -1 .. 2)
|
||||
foreach (immutable colShift; -1 .. 2)
|
||||
if ((r + rowShift) >= 0 && (r + rowShift) < world.length &&
|
||||
(c + colShift) >= 0 && (c + colShift) < world[0].length &&
|
||||
world[r + rowShift][c + colShift] == Cell.fire)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
void nextState(in World world, World nextWorld) {
|
||||
foreach (r, row; world)
|
||||
foreach (c, elem; row)
|
||||
final switch (elem) {
|
||||
case Cell.empty:
|
||||
nextWorld[r][c]= uniform(0.,1.)<P_PROB?Cell.tree:Cell.empty;
|
||||
break;
|
||||
void nextState(in World world, World nextWorld) /*nothrow*/ @safe /*@nogc*/ {
|
||||
foreach (immutable r, const row; world)
|
||||
foreach (immutable c, immutable elem; row)
|
||||
final switch (elem) with (Cell) {
|
||||
case empty:
|
||||
nextWorld[r][c]= (uniform01 < cProb) ? tree : empty;
|
||||
break;
|
||||
|
||||
case Cell.tree:
|
||||
if (world.hasBurningNeighbours(r, c))
|
||||
nextWorld[r][c] = Cell.fire;
|
||||
else
|
||||
nextWorld[r][c]=uniform(0.,1.)<F_PROB?Cell.fire:Cell.tree;
|
||||
break;
|
||||
case tree:
|
||||
if (world.hasBurningNeighbours(r, c))
|
||||
nextWorld[r][c] = fire;
|
||||
else
|
||||
nextWorld[r][c] = (uniform01 < fProb) ? fire : tree;
|
||||
break;
|
||||
|
||||
case Cell.fire:
|
||||
nextWorld[r][c] = Cell.empty;
|
||||
break;
|
||||
}
|
||||
case fire:
|
||||
nextWorld[r][c] = empty;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto world = new World(8, 65);
|
||||
foreach (row; world)
|
||||
foreach (ref el; row)
|
||||
el = uniform(0.0, 1.0) < TREE_PROB ? Cell.tree : Cell.empty;
|
||||
auto nextWorld = new World(world.length, world[0].length);
|
||||
void main() @safe {
|
||||
auto world = new World(8, 65);
|
||||
foreach (row; world)
|
||||
foreach (ref el; row)
|
||||
el = (uniform01 < treeProb) ? Cell.tree : Cell.empty;
|
||||
auto nextWorld = new World(world.length, world[0].length);
|
||||
|
||||
foreach (i; 0 .. 4) {
|
||||
nextState(world, nextWorld);
|
||||
writeln(join(cast(string[])nextWorld, "\n"), "\n");
|
||||
swap(world, nextWorld);
|
||||
}
|
||||
foreach (immutable i; 0 .. 4) {
|
||||
nextState(world, nextWorld);
|
||||
writefln("%(%(%c%)\n%)\n", nextWorld);
|
||||
world.swap(nextWorld);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,6 @@ immutable white = Color(255, 255, 255),
|
|||
|
||||
void nextState(ref World world, ref World nextWorld,
|
||||
ref Xorshift rnd, Image img) {
|
||||
enum double div = typeof(rnd.front).max;
|
||||
immutable nr = world.length;
|
||||
immutable nc = world[0].length;
|
||||
foreach (immutable r, const row; world)
|
||||
|
|
@ -23,8 +22,7 @@ void nextState(ref World world, ref World nextWorld,
|
|||
START: final switch (elem) with (Cell) {
|
||||
case empty:
|
||||
img.putPixel(c, r, white);
|
||||
nextWorld[r][c] = (rnd.front / div) < P_PROB ? tree : empty;
|
||||
rnd.popFront;
|
||||
nextWorld[r][c] = rnd.uniform01 < P_PROB ? tree : empty;
|
||||
break;
|
||||
|
||||
case tree:
|
||||
|
|
@ -39,8 +37,7 @@ void nextState(ref World world, ref World nextWorld,
|
|||
break START;
|
||||
}
|
||||
|
||||
nextWorld[r][c]= (rnd.front / div) < F_PROB ? burning : tree;
|
||||
rnd.popFront;
|
||||
nextWorld[r][c]= rnd.uniform01 < F_PROB ? burning : tree;
|
||||
break;
|
||||
|
||||
case burning:
|
||||
|
|
@ -57,7 +54,7 @@ void main() {
|
|||
auto world = new World(worldSide);
|
||||
foreach (ref row; world)
|
||||
foreach (ref el; row)
|
||||
el = uniform(0.0, 1.0, rnd) < TREE_PROB ? Cell.tree : Cell.empty;
|
||||
el = rnd.uniform01 < TREE_PROB ? Cell.tree : Cell.empty;
|
||||
auto nextWorld = new World(world[0].length);
|
||||
|
||||
auto w= new SimpleWindow(world.length,world[0].length,"ForestFire");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue