CDE
This commit is contained in:
parent
518da4a923
commit
764da6cbbb
6144 changed files with 83610 additions and 11 deletions
54
Task/Forest-fire/D/forest-fire-1.d
Normal file
54
Task/Forest-fire/D/forest-fire-1.d
Normal file
|
|
@ -0,0 +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 Cell : char { empty=' ', tree='T', fire='#' }
|
||||
alias Cell[][] World;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
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 Cell.fire:
|
||||
nextWorld[r][c] = Cell.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);
|
||||
|
||||
foreach (i; 0 .. 4) {
|
||||
nextState(world, nextWorld);
|
||||
writeln(join(cast(string[])nextWorld, "\n"), "\n");
|
||||
swap(world, nextWorld);
|
||||
}
|
||||
}
|
||||
72
Task/Forest-fire/D/forest-fire-2.d
Normal file
72
Task/Forest-fire/D/forest-fire-2.d
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import std.stdio, std.random, std.string, std.algorithm, simpledisplay;
|
||||
|
||||
enum double TREE_PROB = 0.55; // original tree probability
|
||||
enum double F_PROB = 0.01; // auto combustion probability
|
||||
enum double P_PROB = 0.01; // tree creation probability
|
||||
|
||||
template TypeTuple(T...) { alias T TypeTuple; }
|
||||
alias TypeTuple!(-1, 0, 1) sp;
|
||||
|
||||
enum Cell : char { empty=' ', tree='T', burning='#' }
|
||||
alias Cell[][] World;
|
||||
|
||||
immutable white = Color(255, 255, 255),
|
||||
red = Color(255, 0, 0),
|
||||
green = Color(0, 255, 0);
|
||||
|
||||
void nextState(ref World world, ref World nextWorld,
|
||||
ref Xorshift rnd, Image img) {
|
||||
enum double div = cast(double)typeof(rnd.front()).max;
|
||||
immutable nr = world.length;
|
||||
immutable nc = world[0].length;
|
||||
foreach (r, row; world)
|
||||
foreach (c, elem; row)
|
||||
final switch (elem) {
|
||||
case Cell.empty:
|
||||
img.putPixel(c, r, white);
|
||||
nextWorld[r][c] = (rnd.front()/div)<P_PROB ? Cell.tree : Cell.empty;
|
||||
rnd.popFront();
|
||||
break;
|
||||
|
||||
case Cell.tree:
|
||||
img.putPixel(c, r, green);
|
||||
|
||||
foreach (rowShift; sp)
|
||||
foreach (colShift; sp)
|
||||
if ((r + rowShift) >= 0 && (r + rowShift) < nr &&
|
||||
(c + colShift) >= 0 && (c + colShift) < nc &&
|
||||
world[r + rowShift][c + colShift] == Cell.burning) {
|
||||
nextWorld[r][c] = Cell.burning;
|
||||
goto END;
|
||||
}
|
||||
|
||||
nextWorld[r][c]=(rnd.front()/div)<F_PROB ? Cell.burning : Cell.tree;
|
||||
rnd.popFront();
|
||||
END: break;
|
||||
|
||||
case Cell.burning:
|
||||
img.putPixel(c, r, red);
|
||||
nextWorld[r][c] = Cell.empty;
|
||||
break;
|
||||
}
|
||||
|
||||
swap(world, nextWorld);
|
||||
}
|
||||
|
||||
void main() {
|
||||
auto rnd = Xorshift(1);
|
||||
auto world = new World(600, 600); // create world
|
||||
foreach (row; world)
|
||||
foreach (ref el; row)
|
||||
el = uniform(0.0, 1.0, rnd) < TREE_PROB ? Cell.tree : Cell.empty;
|
||||
auto nextWorld = new World(world.length, world[0].length);
|
||||
|
||||
auto w= new SimpleWindow(world.length,world[0].length,"ForestFire");
|
||||
auto img = new Image(w.width, w.height);
|
||||
|
||||
w.eventLoop(1, {
|
||||
auto painter = w.draw();
|
||||
nextState(world, nextWorld, rnd, img);
|
||||
painter.drawImage(Point(0, 0), img);
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue