aboutsummaryrefslogtreecommitdiff
path: root/examples/cgol.sloth
blob: eea14c30d2fb6493ba0fce62797432da4672fd1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
foreign fn print(str: String);
foreign fn randGen(min: Int, max: Int) Int;
foreign fn wait(time: Float);
foreign fn termpos(x: Int, y: Int);


fn populate() [Int] {
	var life: [Int] = [0];
	vpopi(life);
	var i: Int = 0;
	while i < 100 {
		n: Int = randGen(0,1);
		vpushi(life, n);
	}
	return life;
}

fn coord(x: Int, y: Int) Int {
	if x >= 0 && y >= 0 {
		return y*10 + x;
	}
	return -1;

}

fn cval(x: Int, y: Int, life: [Int]) Int {
	c: Int = coord(x, y);
	if c < 0 {
		return 0;	
	}
	return vgeti(life, c);
}

fn update(life: [Int]) [Int] {
	x: Int = 0;
	while x < 10 Int  {
		y: Int = 0;
		while y < 10 {
			total: Int = cval(x-1, y-1) + cval(x-1, y) + cval(x-1, y+1) + cval(x, y-1) + cval(x, y+1) + cval(x+1, y-1) + cval(x+1, y) + cval(x+1, y+1);
			if cval(x, y) == 1 && total < 2 || total > 3{
				vseti(life, 0);
			} else if total == 3 {
				vseti(life, 1);
			}
		}
	}
	return life;
}

fn display(life: [Int]) {
	x: Int = 0;
	while x < 10 {
		y: Int = 0;
		while y < 10 {
			alive: Bool = cval(x, y) == 1;
			if alive {
				termpos(x, y);
				print("#");
			}


		}
	}
}

fn main() Int {
	var life: [Int] = populate();
	while true {
		life = update(life);
		display(life);
		wait(0.5);
	}
}