topical media & game development
#javascript-processing-example-topic-automata-conway.htm / htm
<!DOCTYPE html>
<html><head>
<script src="javascript-processing-example-processing.js"></script>
<script src="javascript-processing-example-init.js"></script>
<link rel="stylesheet" href="javascript-processing-example-style.css">
</head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
<h2>Conway</h2>
<p>by Mike Davis.
This program is a simple version of Conway's
game of Life. A lit point turns off if there
are fewer than two or more than three surrounding
lit points. An unlit point turns on if there
are exactly three lit neighbors. The 'density'
parameter determines how much of the board will
start out lit.</p>
<p><a href="http://processing.org/learning/topics/conway.html"><b>Original Processing.org Example:</b> Conway</a><br>
<script type="application/processing">
int sx, sy;
float density = 0.5;
int[][][] world;
void setup()
{
size(100, 100);
frameRate(12);
sx = width;
sy = height;
world = new int[sx][sy][2];
stroke(255);
// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
world[(int)random(sx)][(int)random(sy)][1] = 1;
}
}
void draw()
{
background(0);
// Drawing and update cycle
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
//if (world[x][y][1] == 1)
// Change recommended by The.Lucky.Mutt
if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
{
world[x][y][0] = 1;
point(x, y);
}
if (world[x][y][1] == -1)
{
world[x][y][0] = 0;
}
world[x][y][1] = 0;
}
}
// Birth and death cycle
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
int count = neighbors(x, y);
if (count == 3 && world[x][y][0] == 0)
{
world[x][y][1] = 1;
}
if ((count < 2 || count > 3) && world[x][y][0] == 1)
{
world[x][y][1] = -1;
}
}
}
}
// Count the number of adjacent cells 'on'
int neighbors(int x, int y)
{
return world[(x + 1) % sx][y][0] +
world[x][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][y][0] +
world[x][(y + sy - 1) % sy][0] +
world[(x + 1) % sx][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
world[(x + 1) % sx][(y + sy - 1) % sy][0];
}
</script><canvas width="100" height="100"></canvas></p>
<div style="overflow: hidden; height: 0px; width: 0px;"></div>
<pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
// unless otherwise stated.</b>
int sx, sy;
float density = 0.5;
int[][][] world;
void setup()
{
size(100, 100);
frameRate(12);
sx = width;
sy = height;
world = new int[sx][sy][2];
stroke(255);
// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
world[(int)random(sx)][(int)random(sy)][1] = 1;
}
}
void draw()
{
background(0);
// Drawing and update cycle
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
//if (world[x][y][1] == 1)
// Change recommended by The.Lucky.Mutt
if ((world[x][y][1] == 1) || (world[x][y][1] == 0 && world[x][y][0] == 1))
{
world[x][y][0] = 1;
point(x, y);
}
if (world[x][y][1] == -1)
{
world[x][y][0] = 0;
}
world[x][y][1] = 0;
}
}
// Birth and death cycle
for (int x = 0; x < sx; x=x+1) {
for (int y = 0; y < sy; y=y+1) {
int count = neighbors(x, y);
if (count == 3 && world[x][y][0] == 0)
{
world[x][y][1] = 1;
}
if ((count < 2 || count > 3) && world[x][y][0] == 1)
{
world[x][y][1] = -1;
}
}
}
}
// Count the number of adjacent cells 'on'
int neighbors(int x, int y)
{
return world[(x + 1) % sx][y][0] +
world[x][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][y][0] +
world[x][(y + sy - 1) % sy][0] +
world[(x + 1) % sx][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][(y + 1) % sy][0] +
world[(x + sx - 1) % sx][(y + sy - 1) % sy][0] +
world[(x + 1) % sx][(y + sy - 1) % sy][0];
}</pre>
</body></html>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.