# SOLVED: Noob Cellular Automata question:

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.

test.h

``````

#pragma once

#include "ofMain.h"

class testApp : public ofBaseApp{

public:
void setup();
void update();
void draw();

void keyPressed  (int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
int neighbors(int x, int y);
int sx, sy;
float density ;
int world;
};

``````

test.cpp

``````

#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){

ofSetFrameRate(12);
sx = ofGetWidth();
sy = ofGetHeight();
density = 0.5;

// Set random cells to 'on'
for (int i = 0; i < sx * sy * density; i++) {
int x = int(ofRandom(sx));
int y = int(ofRandom(sy));
world[x][y] = 1;
}

// Count the number of adjacent cells 'on'
}
//--------------------------------------------------------------

int testApp::neighbors(int x, int y) {
return world[(x + 1) % sx][y] +
world[x][(y + 1) % sy] +
world[(x + sx - 1) % sx][y] +
world[x][(y + sy - 1) % sy] +
world[(x + 1) % sx][(y + 1) % sy] +
world[(x + sx - 1) % sx][(y + 1) % sy] +
world[(x + sx - 1) % sx][(y + sy - 1) % sy] +
world[(x + 1) % sx][(y + sy - 1) % sy];
}

//--------------------------------------------------------------

//--------------------------------------------------------------
void testApp::draw(){

ofBackground(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) {

world[x][y] = 1;
ofSetColor(255);
/*
//Black and White is boring uncomment for more fun!

ofColor c;
int hue = ofMap(y, 0, sy, 20, 200);
c.setHsb(hue,255, 255);
ofSetColor(c);

*/
ofRect(x , y, 1 , 1);
}
if (world[x][y] == -1) {
world[x][y] = 0;
}
world[x][y] = 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) {
world[x][y] = 1;
}
if ((count < 2 || count > 3) && world[x][y] == 1) {
world[x][y] = -1;
}
}
}

}

``````

Thanks for this. I have one issue. I see you use a three dimensional array for the CA space. My CA space must be a one dimensional vector as I intend to pass the vector into a Pure Data array via ofxPd.

Fixed: the init() method should look like this:

``````
void GOL::init() {

for (int i =0; i < cols; i ++) {
for (int j =0; j < rows; j ++) {
board.push_back(rows * cols);
board[i * cols + j] = ofRandom(2);
}
}
}

``````