«game of life» by Raver_Ale

on 10 Sep'14 16:20 in generativeconwaygame of lifevisualwork in progress

a work in progress implementation of conway's game of life. some stuff still not working properly. Used the 1d cellular automaton by the berlin sc-group as reference.

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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
(
var xSize = 60;
var ySize = 60;
var scale = 10;
var waitDur = 0.3;
var win = Window.new("game of lyf", Rect(0, 0, xSize*scale, ySize*scale));
var uview;
var arr, buf, initFunc, iterFunc, startIter, finalize, task, iter;
var xPos, yPos;


initFunc = {
	arr = Array.fill2D(xSize,ySize,{ [0, 1, 1, 0, 0].choose });

	~arr = arr;
};

uview = UserView.new(win, Rect(0, 0, xSize*scale, ySize*scale));
uview.clearOnRefresh = true;
uview.drawFunc = { |uview|
	xSize.do { |x|
		ySize.do { |y|
			Pen.fillColor = if(~arr[x][y]>0, {Color.black},{Color.white});
			Pen.fillRect(Rect(x * scale,y * scale, scale, scale));
			Pen.fill;
		}
	}
};

// TODO:
// __borders not working correctly
// __rules slightly off ...
iterFunc = {
	var state, localArr, border, xP, yP;

	border = arr.size - 1;

	ySize.do{ |y|
		xSize.do{ |x|
			xP = x;
			yP = y;

			localArr = Array.fill2D(3, 3, { |y, x|
				var xPos, yPos;
				xPos = x + xP - 1;
				yPos = y + yP - 1;

				if( yPos == -1, { 0 }, {
					if( yPos >= border, { 0 }, {
						if( xPos == -1, { 0 }, {
							if ( xPos >= border, { 0 }, {
								~arr[xPos][yPos]
							})
						})
					})
				});

			});

			state = localArr.sum.sum;
			//localArr.postln;
			//state.postln;

			if(~arr[xP][yP] == 1, {
				state = state - 1;
				if(state < 2, { ~arr[xP][yP] = 0 });
				if(state > 3, { ~arr[xP][yP] = 0 });
			});
			if(~arr[xP][yP] == 0, {
				if(state == 3, { ~arr[xP][yP] = 1 });
			});
		}
	};
};

startIter = {
	initFunc.value;
	task = iter.fork(AppClock);
};

iter = {
	inf.do{
		//"---------------------------------------------------".postln;
		uview.refresh;
		waitDur.wait;
		iterFunc.value;

	}
};

finalize = {
	task.stop;
};

win.front;
win.onClose = finalize.value;
startIter.value;
)
raw 1836 chars (focus & ctrl+a+c to copy)
reception
comments
Fredrik Olofsson user 12 Sep'14 09:58

line 25 (Pen.fill) is unneeded. Pen.fillRect is enough to draw. that'll save you a little bit of cpu.

and actually, because you clear the window on each refresh, it's enough to only draw the black dots. like this...

//replace lines 18-28 with this: uview = UserView.new(win, Rect(0, 0, xSize*scale, ySize*scale)); uview.clearOnRefresh = true; uview.background= Color.white; uview.drawFunc = { |uview| Pen.fillColor= Color.black; xSize.do { |x| ySize.do { |y| if(~arr[x][y]&gt;0, { Pen.addRect(Rect(x * scale,y * scale, scale, scale)); }); } }; Pen.fill; };

also see this game of life + ca implementation http://www.fredrikolofsson.com/f0blog/?q=node/43

raffaelseyfried user 15 Sep'14 08:13

Hey, thanks for the tips!

I just don't know what the if(~arr[x][y]&gt) does it throws an arrow for me because i didn't declare it. Is it a shorthand for something?

grirgz user 15 Sep'14 13:48

@raffaelseyfried: this is an html glitch, you must read ~arr[x][y] > 0 (gt stand for greater than)