2
\$\begingroup\$

I tried my hands of Conway's Game of life. Well it works as I wanted it to but I want to improve my design and coding practice. I will be glad to get feedback and ideas to improve it and how to code it better.

#include<stdio.h>
#include<windows.h>
#include<conio.h>

#define ALIVE true
#define DEAD false
#define T 100

const int GRIDSIZE = 50;

using namespace std;

class Grid {
    bool grid[GRIDSIZE][GRIDSIZE], stable;
    int generation;
    public:
        Grid();
        void printFrame();
        void nextFrame();
        void play();
        bool isExtinct();
        bool isStable();
};

Grid::Grid() {
    for( int i=0; i < GRIDSIZE; i++)
        for( int j=0; j < GRIDSIZE; j++){
                grid[i][j] = DEAD;
    }    
    stable = false;
    generation = 1;

   // Manually setting the initial population

   // grid[24][20] = ALIVE;
    grid[2][2] = ALIVE;
    grid[3][3] = ALIVE;
    grid[3][6] = ALIVE;
    grid[4][4] = ALIVE;
    grid[2][6] = ALIVE;
    //grid[4][8] = ALIVE;
    //grid[4][9] = ALIVE;
    //grid[4][7] = ALIVE;
    //grid[5][6] = ALIVE;

}

void Grid::printFrame() {
    printf("\n **  GENERATION : %d  ** \n\n", this -> generation);
    for( int i = 0; i < GRIDSIZE; i++) {
        for( int j = 0; j < GRIDSIZE; j++) {
            if( this -> grid[i][j]){
                printf("*");
            }
            else {
                printf(" ");
            }
        }
        printf("\n");
    }
}


void Grid::nextFrame() {

    int numSurrounding = 0, tmpcounter=0;
    bool tempGrid [GRIDSIZE][GRIDSIZE];

    for ( int i = 0; i < GRIDSIZE ; i++) {
        for ( int j = 0; j < GRIDSIZE ; j++) {
            if ( ( i + 1) < GRIDSIZE && this -> grid[i + 1][j] == true ) {
                numSurrounding++;
            }
            if ( (i-1) >= 0 && this -> grid[i - 1][j] == true ) {
                numSurrounding++;
            }
            if ( (j+1) < GRIDSIZE && this -> grid[i][j+1] == true ) {
                numSurrounding++;
            }
            if ( (j-1) >= 0 && this -> grid[i][j-1] == true ) {
                numSurrounding++;
            }
            if ( (i+1) < GRIDSIZE && (j+1) < GRIDSIZE && this -> grid[i+1][j+1] == true ) {
                numSurrounding++;
            }
            if ( (i+1) < GRIDSIZE && (j-1) >= 0 && this -> grid[i+1][j-1] == true ) {
                numSurrounding++;
            }
                if ( (i-1) >= 0 && (j+1) < GRIDSIZE && this -> grid[i-1][j+1] == true ) {
                numSurrounding++;
            }
            if ( (i-1) >= 0 && (j-1) >= 0 && this -> grid[i-1][j-1] == true ) {
                numSurrounding++;
            }

            if (numSurrounding < 2 || numSurrounding > 3) {
                tempGrid[i][j] = false;
            }
            else if ( numSurrounding == 2) {
                tempGrid[i][j] = this -> grid[i][j];
            }
            else if ( numSurrounding == 3) {
                tempGrid[i][j] = true;
            }

            numSurrounding = 0;

        }
    }  

    for ( int i = 0 ; i < GRIDSIZE ; i++ ) {
        for ( int j = 0 ; j < GRIDSIZE ; j++ ) {
            if (this -> grid[i][j] != tempGrid[i][j]) {
                tmpcounter = 1;
            }

            this -> grid[i][j] = tempGrid[i][j];
        }
    }
    this -> generation++;
    if(tmpcounter == 0)
        this -> stable = true;
}
bool Grid::isExtinct() {
    for(int i = 0; i < GRIDSIZE; i++)
        for( int j = 0; j < GRIDSIZE; j++)
            if( this -> grid[i][j])
                return false;
    return true;
}
bool Grid::isStable() { return this->stable; }


void Grid::play(){
    int generation;
    printf( " \n\n  FIRST GENERATIOM :- (Press ENTER to let the life begin)\n\n");
    this -> printFrame();
    getch();

    while(1) {
        system("CLS");
        this -> printFrame();
        Sleep(T);
        this -> nextFrame();
        if( this -> isExtinct()) {
            system("CLS");
            printf("\n\n\n\n                           *******************\n                 ALL THINGS CREATED MUST END SOMEDAY!\n                       *******************\n\n\n\n\n\n");
            printf("\n\n\n      Total generations till extinction : %d \n\n\n\n\n\n", (this -> generation)-1);

            break;
        }
        if(this -> isStable()) {
            system("CLS");
            printf("\n\n\n\n                   *******************\n                 AND THE LIFE IS STAGNANT AFTER %d GENERATIONS!  \n                   *******************\n\n\n\n\n\n", this -> generation);
            this -> printFrame();
            getch();
            break;
        }

    }
}


int main() {
    Grid life;
    life.play();
    return 0;
}
\$\endgroup\$
2
\$\begingroup\$

Prefer declaring variables where you need them. This makes it clearer where they are expected to be used. Don't worry about optimizing stack allocations because the compiler will move the increment of the stack pointer tot he beginning of the function anyway. Only when there is a cost to initializing the object should it be pulled out of loops.

void Grid::nextFrame() {


    bool tempGrid [GRIDSIZE][GRIDSIZE];


    for ( int i = 0; i < GRIDSIZE ; i++) {
        for ( int j = 0; j < GRIDSIZE ; j++) {
            int numSurrounding = 0;

            if ( ( i + 1) < GRIDSIZE && this -> grid[i + 1][j] == true ) {
                numSurrounding++;
            }
            //...
        }
    }

    int gridChanged = 0;

    for ( int i = 0 ; i < GRIDSIZE ; i++ ) {
        for ( int j = 0 ; j < GRIDSIZE ; j++ ) {
            //...
        }
    }

}

you #define DEAD and ALIVE but then don't use them in the nextFrame function. Also enum beats macro for a group of related constants.

\$\endgroup\$
  • \$\begingroup\$ Okay thanks, but I don't understand the 'optimization of stack allocation's' point you mentioned. Can I get any reference to this where to read about it? Thanks \$\endgroup\$ – windlessStorm Feb 5 '16 at 17:32
  • \$\begingroup\$ @windlessStorm it's one of the arguments for declaring all locals at the top of a function even though it's not necessary. \$\endgroup\$ – ratchet freak Feb 5 '16 at 17:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.