Hey all, I’ve been recently experimenting with programming games/visualizations for the vex v5 system (with varying success) and ran into a sort of roadblock with my attempt to program Conway’s game of life.
/*
Conway's game of life, implemented in VEXcode,
stack overflows currently occurring in multiple methods.
*/
#include "vex.h"
using namespace vex;
class world {
public:
int screen[480][240];
int buffer[480][240];
int count;
void display() { //code to display the screen, with each pixel being one cell in the game of life
for (int i = 1; i < 240; i++) { // loop that iterates across the entire world.
for (int x = 1; x < 480; x++) {
buffer[x][i] = screen[x][i]; //this causes a stack overflow
if (screen[x][i] == 0) {
Brain.Screen.drawPixel(x,i); //this causes a stack overflow
}
}
}
}
void iterate() { // performs the main logic of life
for (int i = 1; i < 240; i++) {
for (int x = 1; x < 480; x++) { // loop that iterates across the entire world.
count = 0;
//buffer[x][i] = screen[x][i]; //this should be in a separate part of the code, commented out in case it might end up being better placed here
for (int z = -1; z < 1; z++) {
for (int a = -1; a < 1; a++) {
if (buffer[x + z][i + a] == 1) {
if (z != 0 & a != 0) {
count++;
}
}
}
}
if (count == 3) {
screen[x][i] = 1;
}
if (count < 2 or count > 3) {
screen[x][i] = 0;
}
}
}
}
void random() {
for (int i = 1; i < 240; i++) {
for (int x = 1; x < 480; x++) {
screen[x][i] = (rand() % 2);
//buffer[x][i] = screen[x][i]; //i think this caused a stack overflow as well
}
}
}
};
int main() {
Brain.Screen.setPenColor(white);
// Initializing Robot Configuration. DO NOT REMOVE!
world s1; //creates the world
s1.random(); //calls the random method
while (1) { //main loop
Brain.Screen.clearScreen();
s1.display();
s1.iterate();
task::sleep(1600);
} //this loop clears the screen, displays the world, iterates that world, then stops for 1.6 seconds
vexcodeInit();
}
The program compiles without errors, bur for some reason the Brain.Screen.drawPixel(x,i);
line causes a stack overflow, along with the buffer[x][i] = screen[x][i];
line. and possibly some other methods.
is there bad practice in my programming which is causing this? this is the second program I’ve written with object oriented programming in mind, so any advice on best practices for that, or pointing out mistakes i am making is welcome