Few questions about a game of life
in
Programming Questions
•
1 year ago
Hi everybody,
First of all, I have to say that I'm pretty new to Processing & Java programming (and not very good in English neither).
I'm trying to improve the Game of life example coming with Processing and I ran into several bugs :
The idea is, instead of calculating if a cell will live or die and draw it, I try to distinguish the two steps and it will cycle on 25 frames :
1st I do the calculation on the frame 0 (call getnextgen() ) then depending on the result, raise or lower the alpha channel of the cell during the 24 first frame (0-23) and during the last frame update its status to a living or dead cell. (I hope it makes sense). Here's my code :
- int sx, sy, time, squaresize;
- int[][][] world;
- void setup()
- {
- squaresize = 4;
- size(640, 480);
- frameRate(25);
- sx = width/squaresize;
- sy = height/squaresize;
- time = 0;
- world = new int[sx][sy][2];
- PFont police;
- police = loadFont("Georgia-Italic-25.vlw");
- textFont(police,25);
- //starting point : a vertical line at the middle of the screen
- for (int i = 1; i < sy; i++) {
- world[(int)sx/2][(int)i][1] = 1;
- }
- }
- // Birth and death cycle
- void getnextgen() {
- for (int x = 1; x < sx-1; x++) {
- for (int y = 1; y < sy-1; y++) {
- int count = neighbors(x, y);
- if (count == 3 && world[x][y][0] == 0) //live
- {
- world[x][y][1] = 1;
- }
- if ((count < 2 || count > 3) && world[x][y][0] == 1) //die
- {
- world[x][y][1] = -1;
- }
- }
- }
- }
- int alpha() {
- return (int)(gettime()*10.2);
- }
- int gettime() {
- return time % 25;
- }
- void draw()
- {
- background(0);
- // time++;//comment to iterate with space bar
- if (gettime() == 0) {
- getnextgen();
- }
- for (int x = 1; x < sx-1; x++) {
- for (int y = 1; y < sy-1; y++) {
- if (gettime() == 24) { //update the cell's status
- if (world[x][y][0] == 1 && world[x][y][1] == -1) { //dies
- world[x][y][0] = 0;
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(0);
- }
- if (world[x][y][0] == 0 && world[x][y][1] == 1) { //lives
- world[x][y][0] = 1;
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(0);
- }
- if (world[x][y][0] == 1 && (world[x][y][1] == 1 || world[x][y][1] == 0)) {//survives
- world[x][y][0] = 1;
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(255, 255, 255);
- }
- if (world[x][y][0] == 0 && (world[x][y][1] == -1 || world[x][y][1] == 0)) {//was dead
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(0);
- world[x][y][0] = 0;
- }
- else {///////////////////////////////////BUG
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(255, 0, 0);
- }
- }
- else { //transition during 24 frames without updating the cell's status
- if (world[x][y][1] == 1 && world[x][y][0] == 1) { //stays alive
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(255, 255, 255);
- }
- if ( world[x][y][1] == -1 && world[x][y][0] == 0) { //stays dead
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(0);
- }
- if (world[x][y][1] == 1 && world[x][y][0] == 0) { //live
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(255, 255, 255, alpha());
- }
- if (world[x][y][1] == -1 && world[x][y][0] == 1) { //die
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(255, 255, 255, 255 - alpha() );
- }
- else{
- noStroke();
- rect(x*squaresize, y*squaresize, squaresize, squaresize);
- fill(0);
- }
- }
- }
- }
- fill(255, 255, 255);
- text(gettime(), 20, 35);
- }
- 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];
- }
- void keyPressed() {
- int keyIndex = -1;
- if (keyIndex == -1) {
- time++;
- }
- }
You can press the space bar to increment the frames.
The bugs :
- When I start I got a white cell at something like (2,2) I don't understand why...
- I put in red some cells (only at the 24th frame) I don't even understand how they can exist...
Thanks by advance if someone could push me in the right direction!
Matthieu
1