Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,135 @@
import ceylon.random {
DefaultRandom
}
class Cell of tree | empty | burning {
shared new tree {}
shared new empty {}
shared new burning {}
}
class Forest(Integer width, Integer height, Float f, Float p) {
value random = DefaultRandom();
function chance(Float probability) => random.nextFloat() < probability;
object doubleBufferedGrid satisfies Iterable<Array<Cell>, Null> {
function makeGrid(Cell|Cell() initialValue) => [
for(j in 0:height)
Array {
for(i in 0:width)
switch(initialValue)
case(is Cell) initialValue
case(is Cell()) initialValue()
}
];
value grids = [
makeGrid(() =>
if(chance(0.5))
then Cell.tree
else Cell.empty),
makeGrid(Cell.empty)
];
variable value firstIsFront = true;
value front => firstIsFront then grids.first else grids.last;
value back => firstIsFront then grids.last else grids.first;
iterator() => front.iterator();
shared Cell? get(Integer x, Integer y) => front[y]?.get(x);
shared void set(Integer x, Integer y, Cell cell) => back[y]?.set(x, cell);
shared void flip() => firstIsFront = !firstIsFront;
}
shared void evolve() {
function fireNearby(Integer x, Integer y) {
for(i in -1..1) {
for(j in -1..1) {
if(i == 0 && j == 0) {
continue;
}
if(exists cell = doubleBufferedGrid.get(x + i, y + j),
cell == Cell.burning) {
return true;
}
}
}
return false;
}
for(j->row in doubleBufferedGrid.indexed) {
for(i->cell in row.indexed) {
switch(cell)
case(Cell.burning) {
doubleBufferedGrid.set(i, j, Cell.empty);
}
case(Cell.empty) {
value nextCell = chance(p) then Cell.tree else Cell.empty;
doubleBufferedGrid.set(i, j, nextCell);
}
case(Cell.tree) {
value nextCell =
fireNearby(i, j) || chance(f)
then Cell.burning
else Cell.tree;
doubleBufferedGrid.set(i, j, nextCell);
}
}
}
doubleBufferedGrid.flip();
}
shared void display() {
void drawLine() => print("-".repeat(width + 2));
drawLine();
for(row in doubleBufferedGrid) {
process.write("|");
for(cell in row) {
switch(cell)
case(Cell.empty) {
process.write(" ");
}
case(Cell.tree) {
process.write("A");
}
case(Cell.burning) {
process.write("#");
}
}
print("|");
}
drawLine();
}
}
shared void run() {
value forest = Forest(78, 38, 0.02, 0.03);
variable value generation = 1;
while(true) {
forest.display();
forest.evolve();
print("Generation ``generation++``
Press enter for next generation or q and then enter to quit");
value input = process.readLine();
if(exists input) {
switch(input.trimmed)
case("q" | "Q") {
return;
}
else {}
}
}
}

View file

@ -0,0 +1,56 @@
import math, os, strutils
randomize()
type State = enum Empty, Tree, Fire
const
disp: array[State, string] = [" ", "\e[32m/\\\e[m", "\e[07;31m/\\\e[m"]
treeProb = 0.01
burnProb = 0.001
proc chance(prob: float): bool = random(1.0) < prob
# Set the size
var w, h: int
if paramCount() >= 2:
w = parseInt paramStr 1
h = parseInt paramStr 2
if w <= 0: w = 30
if h <= 0: h = 30
# 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 disp[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: 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
univ = univNew
sleep 200

View file

@ -0,0 +1,61 @@
define w = `tput cols`.to_i-1
define h = `tput lines`.to_i-1
define r = "\033[H"
define red = "\033[31m"
define green = "\033[32m"
define yellow = "\033[33m"
define chars = [' ', green+'*', yellow+'&', red+'&']
define tree_prob = 0.05
define burn_prob = 0.0002
enum |Empty, Tree, Heating, Burning|
define dirs = [
%n(-1 -1), %n(-1 0), %n(-1 1), %n(0 -1),
%n(0 1), %n(1 -1), %n(1 0), %n(1 1),
]
var forest = h.of { w.of { 1.rand < tree_prob ? Tree : Empty } }
var range_h = h.range
var range_w = w.range
func iterate {
var new = h.of{ w.of(0) }
for i in range_h {
for j in range_w {
given (new[i][j] = forest[i][j]) {
when (Tree) {
1.rand < burn_prob && (new[i][j] = Heating; next)
dirs.each { |pair|
var y = pair[0]+i
range_h.contains(y) || next
var x = pair[1]+j
range_w.contains(x) || next
forest[y][x] == Heating && (new[i][j] = Heating; break)
}
}
when (Heating) { new[i][j] = Burning }
when (Burning) { new[i][j] = Empty }
case (1.rand < tree_prob) { new[i][j] = Tree }
}
}
}
forest = new
}
STDOUT.autoflush(true)
func init_forest {
print r
forest.each { |row|
print chars[row]
print "\033[E\033[1G"
}
iterate()
}
loop { init_forest() }

View file

@ -0,0 +1,72 @@
define RED = "\e[1;31m"
define YELLOW = "\e[1;33m"
define GREEN = "\e[1;32m"
 
define DIRS = [
[-1, -1], [0, -1], [1, -1],
[-1, 0], [1, 0],
[-1, 1], [0, 1], [1, 1],
]
 
enum (Empty, Tree, Heating, Burning)
define pix = [' ', GREEN + "*", YELLOW + "*", RED + "*"]
 
class Forest(p=0.01, f=0.001, height, width) {
 
has coords = []
has spot = []
has neighbors = []
 
method init {
coords = (0..height ~X 0..width)
spot = height.of { width.of { [true, false].pick ? Tree : Empty } }
self.init_neighbors
}
 
method init_neighbors {
for i,j in coords {
neighbors[i][j] = gather {
for dir in DIRS {
take(\(spot[i + dir[0]][j + dir[1]] \\ next))
}
}
}
}
 
method step {
var heat = []
 
for i,j in coords {
given (spot[i][j]) {
when Empty { spot[i][j] = Tree if (1.rand < p) }
when Tree { spot[i][j] = Heating if (1.rand < f) }
when Heating { spot[i][j] = Burning; heat << [i, j] }
when Burning { spot[i][j] = Empty }
}
}
 
for i,j in heat {
neighbors[i][j].each { |ref|
*ref = Heating if (*ref == Tree)
}
}
}
 
method show {
for i in ^height {
say pix[spot[i]]
}
}
}
STDOUT.autoflush(true)
var(height, width) = `stty size`.nums.map{.dec}...
 
var forest = Forest(height: height, width: width)
print "\e[2J"
loop {
print "\e[H"
forest.show
forest.step
}