Source

cowbel / test / life.cow

Full commit
/* cowbel test suite
 *
 * Written in 2012 by David Given.
 *
 * To the extent possible under law, the author has dedicated all copyright
 * and related and neighboring rights to this software to the public domain
 * worldwide. This software is distributed without any warranty.
 *
 * Please see the file COPYING.CC0 in the distribution package for more
 * information.
 */

#include "SimpleIO.ch"
#include "Array.ch"

var width = 10;
var height = 10;

var buffera = Array.New2D<int>(width, height, 0);
var bufferb = Array.New2D<int>(width, height, 0);
var front = buffera;
var back = bufferb;

function wipe_buffers()
{
	for y = 0, height
		for x = 0, width
		{
			buffera.set(x, y, 0);
			bufferb.set(x, y, 0);
		}
}

function getxy(x: int, y: int): int
{
	return front.get(x, y);
}

function setxy(x: int, y: int, value: int)
{
	front.set(x, y, value);
}
	
function populate_front()
{
	setxy(2, 4, 1);
	setxy(3, 4, 1);
	setxy(4, 4, 1);
	setxy(2, 3, 1);
	setxy(3, 2, 1);
}

function draw_front()
{
	for y = 0, height
	{
		var s = "";

		for x = 0, width		
		{
			var cell = front.get(x, y);
			if (cell == 0)
				s = s + ".";
			else
				s = s + "*";
		}
		println(s);
	}
}

function do_cell(x: int, y: int)
{
	var s = 
		getxy(x-1, y-1) +
		getxy(x-1, y  ) +
		getxy(x-1, y+1) +
		getxy(x  , y-1) +
		getxy(x  , y+1) +
		getxy(x+1, y-1) +
		getxy(x+1, y  ) +
		getxy(x+1, y+1);
		
	var old = getxy(x, y);
	
	if (s == 2)
	{
		var v = getxy(x, y);
		back.set(x, y, v);
	}
	else if (s == 3)
		back.set(x, y, 1);
	else
		back.set(x, y, 0);
}

function process()
{
	var maxw = width - 1;
	var maxh = height - 1;

	for y = 1, maxh	
	{
		for x = 1, maxw
			do_cell(x, y);
	}
}
	 
function run()
{
	println("Initialising. (Be patient...)");
	wipe_buffers();
	populate_front();

	var b = true;
	for generations = 0, 10
	{
		println("");
		println("Frame " + generations.toString());
		process();
		
		if (b)
		{
			front = bufferb;
			back = buffera;
		}
		else
		{
			front = buffera;
			back = bufferb;
		}
		b = !b;
		
		draw_front();
	}
}

run();