I’m trying to write one of Daniel Shiffman’s CA from Processing into oF, but I’m having difficulty rendering to the screen.
Here’s my Game of Life class:
#include "GOL.h"
GOL::GOL() {
init();
}
void GOL::init() {
for (int i =1;i < cols-1;i++) {
for (int j =1;j < rows-1;j++) {
board.push_back(rows * cols);
board[i * cols + j] = ofRandom(2);
}
}
}
void GOL::generate() {
vector<int> next(rows * cols);
// Loop through every spot in our 2D array and check spots neighbors
for (int x = 0; x < cols; x++) {
for (int y = 0; y < rows; y++) {
// Add up all the states in a 3x3 surrounding grid
int neighbors = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
neighbors += board[((x+i+cols)%cols) * cols + ((y+j+rows)%rows)];
}
}
// A little trick to subtract the current cell's state since
// we added it in the above loop
neighbors -= board[x * cols + y];
// Rules of Life
if ((board[x * cols + y] == 1) && (neighbors < 2)) next[x * cols + y] = 0; // Loneliness
else if ((board[x * cols + y] == 1) && (neighbors > 3)) next[x * cols + y] = 0; // Overpopulation
else if ((board[x * cols + y] == 0) && (neighbors == 3)) next[x * cols + y] = 1; // Reproduction
else next[x * cols + y] = board[x * cols + y]; // Stasis
}
}
// Next is now our board
board = next;
}
void GOL::display() {
for ( int i = 0; i < cols;i++) {
for ( int j = 0; j < rows;j++) {
if (board[i * cols + j] == 1) {
col.set(255);
ofSetColor(col);
}
else {
col.set(0, 255, 0);
ofSetColor(col);
}
ofRect(i * w, j * w, w , w);
}
}
}
When I build and run I get a green screen. I think the issue may reside in my display method? I’d be very grateful if someone could point me in the right direction.
Thanks.