forked from dcheesman/LEDStripController
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a "Conway's Game of Life" effect to the project.
- Loading branch information
Chris Parkins
committed
Feb 13, 2014
1 parent
0f2038d
commit f379452
Showing
2 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* An effect class to represent Conway's game of life: http://en.wikipedia.org/wiki/Conway's_Game_of_Life | ||
* With some randomness added in for aestetic appeal. | ||
*/ | ||
class GameOfLife extends Effect { | ||
color baseColor; | ||
float weight = 1; | ||
int[][] grid, newGrid; | ||
|
||
GameOfLife(int _millis, color _c) { | ||
super(_millis); | ||
baseColor = _c; | ||
grid = new int[cols][rows]; | ||
newGrid = new int[cols][rows]; | ||
|
||
//Populate a "random" seed | ||
for (int x = 0; x < cols; x++) { | ||
for (int y = 0; y < rows; y++) { | ||
grid[x][y] = (int) random(1024) % 2; | ||
} | ||
} | ||
|
||
imageBuffer.beginDraw(); | ||
imageBuffer.noFill(); | ||
imageBuffer.stroke(baseColor); | ||
imageBuffer.strokeWeight(weight); | ||
imageBuffer.endDraw(); | ||
} | ||
|
||
void update() { | ||
processTurn(); | ||
imageBuffer.beginDraw(); | ||
imageBuffer.background(0, 1); | ||
for (int x = 0; x < cols; x++) { | ||
for (int y = 0; y < rows; y++) { | ||
if (newGrid[x][y] == 1) { | ||
imageBuffer.point(x, y); | ||
} | ||
} | ||
} | ||
|
||
//shift the color randomly | ||
float red = ((baseColor >> 16 & 0xFF) * random(16)) % 255; | ||
float green = ((baseColor >> 8 & 0xFF) * random(16)) % 255; | ||
float blue = ((baseColor & 0XFF) * random(16)) % 255; | ||
float alpha = random(128, 255); | ||
imageBuffer.stroke(color(red, green, blue, alpha)); | ||
imageBuffer.endDraw(); | ||
|
||
grid = newGrid; | ||
} | ||
|
||
void processTurn() { | ||
for (int x = 0; x < cols; x++) { | ||
for (int y = 0; y < rows; y++) { | ||
//Calculate number of neighbors | ||
int count = 0; | ||
if (x > 0 && y > 0) { | ||
count += grid[x - 1][y - 1]; //Nortwest | ||
} | ||
|
||
if (x > 0) { | ||
count += grid[x - 1][y]; //West | ||
} | ||
|
||
if (x > 0 && y < rows - 1) { | ||
count += grid[x - 1][y + 1]; //Southwest | ||
} | ||
|
||
if (y > 0) { | ||
count += grid[x][y - 1]; //North | ||
} | ||
|
||
if (x < cols - 1 && y < rows - 1) { | ||
count += grid[x + 1][y + 1]; //Southeast | ||
} | ||
|
||
if (x < cols - 1 && y > 0) { | ||
count += grid[x + 1][y - 1]; //Northeast | ||
} | ||
|
||
if (x < cols - 1) { | ||
count += grid[x + 1][y]; //East | ||
} | ||
|
||
if (y < rows - 1) { | ||
count += grid[x][y + 1]; //South | ||
} | ||
|
||
if (grid[x][y] == 1 && count < 2) { | ||
newGrid[x][y] = 0; //Isolation Death | ||
} else if (grid[x][y] == 1 && count > 3) { | ||
newGrid[x][y] = 0; //Overpopulation Death | ||
} else if (grid[x][y] == 0 && count == 3) { | ||
newGrid[x][y] = 1; //Birth | ||
} else { | ||
newGrid[x][y] = grid[x][y]; //Continue | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters