# c++ Assignment: Game of Life

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 02-28-2012
rogster001
thats what i am talking about, there will be a problem when you check the neighbours as it stands.

You can step through the board in a nested loop of two yes. But then you need to check each cell to find out if it lives or dies on the next generation. It should be clear to you that each cell is always thought of as being surrounded by eight neighbours that 'touch it' so a further loop to examine the immediate neighbours is required. There are other fancy ways to optimally examine the board but in your case stick with the method discussed thus far

Now given that some cells are at the edges of the board, what do you think the implications of this are?

123
4C5
678

C = current cell,

For example imagine it is the top left cell of your board as you presently have it.
• 02-28-2012
oogabooga
As rogster has pointed out, you don't want to access outside the bounds of the 2D array. But in your case that's easy since you can simply restrict the bounds of your loops. So the double loop will be like this.
Code:

``` for (int r=1; r<ROWS-1; r++)  {   for (int c=1; c<COLS-1; c++)   {     int cnt = 0;     // if statements here that increment cnt   }  }```
Now think about how to check the square up one and left one. It will have the coordinates r-1 (up one row) and c-1 (left one column), so you can check it like this:
Code:

`if (board[r-1][c-1] == LIVE)  cnt++;`
So try to make the other seven if statements. Look at rogster's post above to see the relative locations of the cells.
• 02-28-2012
rogster001
I would recommend calling function that returns a live count for the given current cell at x,y
• 02-28-2012
Dylan Metz
Okay this is what I have so-far:
Code:

``` #include <iostream> #include <string> #include <fstream> using namespace std; //Global Variables const int ROWS = 12; const int COLS = 30; const int BOARD_ROWS(10); const int BOARD_COLS(28); const char LIVE = 'X'; //life cells const char DEAD = '.'; //dead cells //functions void MakeArray(string filename, char board[][COLS]); void GameBoard(char board[][COLS]); void NextState(char board[][COLS]); int main() {         char board [ROWS][COLS];         string filename; //Name of the file         cout<<"Enter the filename: \n";         cin>>filename;         //call functions         MakeArray(filename, board);         NextState(board);         GameBoard(board);         //stop terminal window from quitting after programs ends         char q;         cin >> q;         return 0; } void MakeArray(string filename, char board[][COLS]) {         ifstream myfile;         myfile.open (filename.c_str());         for (int r=0; r<ROWS; r++)         {                 for (int c=0; c<COLS; c++)                 {                         myfile>>board[r][c];                 }         }         myfile.close();  } void GameBoard (char board[][COLS]) {         for (int r=0; r<ROWS; r++)         {                 for (int c=0; c<COLS; c++)                 {                                         cout<<board[r][c];                 }                 cout<<endl;                } } void NextState (char board[][COLS]) {         int LiveCnt=0;         for (int r=0; r<ROWS; r++)         {                 for (int c=0; c<COLS; c++)                 {                                         if (board[r-1][c-1]=='X')                         {                         LiveCnt++;                                }                         if (board[r-1][c]=='X')                         {                         LiveCnt++;                                }                         if (board[r-1][c+1]=='X')                         {                         LiveCnt++;                                }                         if (board[r][c-1]=='X')                         {                         LiveCnt++;                                }                         if (board[r][c]=='X')                         {                         LiveCnt++;                                }                         if (board[r][c+1]=='X')                         {                         LiveCnt++;                                }                         if (board[r+1][c-1]=='X')                         {                         LiveCnt++;                                }                         if (board[r+1][c+1]=='X')                         {                         LiveCnt++;                                }                 }                 cout<<endl;                } }```
Now I need to have another if statement for the rules? How do I do that?
• 02-28-2012
oogabooga
Code:

```void NextState (char board[][COLS]) {         // You need a second board here. Call it board2.         int LiveCnt=0; // This needs to go at the top of the inner loop                       // since it needs to be reset to 0 every time.         for (int r=0; r<ROWS; r++) // Should be from 1 to ROWS-1         {                 for (int c=0; c<COLS; c++) // Should be from 1 to COLS-1                 {                                         if (board[r-1][c-1]=='X')                         {                         LiveCnt++;                                }                         if (board[r-1][c]=='X')                         {                         LiveCnt++;                                }                         if (board[r-1][c+1]=='X')                         {                         LiveCnt++;                                }                         if (board[r][c-1]=='X')                         {                         LiveCnt++;                                }                         // You don't want to count the current cell,                         // so get rid of this one.                         if (board[r][c]=='X')                         {                         LiveCnt++;                                }                         if (board[r][c+1]=='X')                         {                         LiveCnt++;                                }                         if (board[r+1][c-1]=='X')                         {                         LiveCnt++;                                }                         // You're missing the one right below the current cell here                         if (board[r+1][c+1]=='X')                         {                         LiveCnt++;                                }                 }                 cout<<endl;  // Get rid of this.         }         // Here you put an if statement that says "if the cell is live and does not         // have either 2 or 3 neighbors then set it (in board2) to dead.         // else if it is dead and has exactly 3 neighbors then set it (in board2) to alive         // else set board2[r][c] to board[r][c]. }```
• 02-28-2012
Dylan Metz
Quote:

Originally Posted by oogabooga
Code:

```void NextState (char board[][COLS]) {     // You need a second board here. Call it board2.     int LiveCnt=0; // This needs to go at the top of the inner loop                   // since it needs to be reset to 0 every time.     for (int r=0; r<ROWS; r++) // Should be from 1 to ROWS-1     {         for (int c=0; c<COLS; c++) // Should be from 1 to COLS-1         {                     if (board[r-1][c-1]=='X')             {             LiveCnt++;                }             if (board[r-1][c]=='X')             {             LiveCnt++;                }             if (board[r-1][c+1]=='X')             {             LiveCnt++;                }             if (board[r][c-1]=='X')             {             LiveCnt++;                }             // You don't want to count the current cell,             // so get rid of this one.             if (board[r][c]=='X')             {             LiveCnt++;                }             if (board[r][c+1]=='X')             {             LiveCnt++;                }             if (board[r+1][c-1]=='X')             {             LiveCnt++;                }             // You're missing the one right below the current cell here             if (board[r+1][c+1]=='X')             {             LiveCnt++;                }         }         cout<<endl;  // Get rid of this.     }     // Here you put an if statement that says "if the cell is live and does not     // have either 2 or 3 neighbors then set it (in board2) to dead.     // else if it is dead and has exactly 3 neighbors then set it (in board2) to alive     // else set board2[r][c] to board[r][c]. }```

Okay here how's this:
Code:

```void NextState (char board[][COLS]){         char board2[ROWS][COLS];         //int LiveCnt=0;         for (int r=1; r<ROWS-1; r++)         {                 int LiveCnt=0;                 for (int c=1; c<COLS-1; c++)                 {                                         if (board[r-1][c-1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r-1][c]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r-1][c+1]==LIVE)                         {                                 LiveCnt++;                                }                         /*/if (board[r][c-1]==LIVE)                         {                                 LiveCnt++;                                }/*/                         if (board[r][c]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r][c+1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r+1][c-1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r+1][c]==LIVE)                         {                                 LiveCnt++;                         }                         if (board[r+1][c+1]==LIVE)                         {                                 LiveCnt++;                                }                 }         }         if () }```
Okay how do I do the IF statements that checks to see if the LIVE cell has 2 o3 neighbors? What do I put inside : if (?????)....

Thanks for the help.
• 02-28-2012
oogabooga
In the previous post, I put the blue text in the wrong place. It needs to be inside the inner loop, at the bottom. I've moved it there.

I've moved the initialization of LiveCnt to the proper place.
You removed (commented out) the wrong if. I fixed that.

You need to do the rest yourself! :)
Code:

```void NextState (char board[][COLS]){         char board2[ROWS][COLS];         for (int r=1; r<ROWS-1; r++)         {                 for (int c=1; c<COLS-1; c++)                 {                         int LiveCnt=0;                                         if (board[r-1][c-1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r-1][c]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r-1][c+1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r][c-1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r][c+1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r+1][c-1]==LIVE)                         {                                 LiveCnt++;                                }                         if (board[r+1][c]==LIVE)                         {                                 LiveCnt++;                         }                         if (board[r+1][c+1]==LIVE)                         {                                 LiveCnt++;                                }                         // Here you put an if statement that says "if the cell is live and does not                         // have either 2 or 3 neighbors then set it (in board2) to dead.                         // else if it is dead and has exactly 3 neighbors then set it (in board2) to alive                         // else set board2[r][c] to board[r][c].                 }         }         // Here you simply copy board2 back to board         // (but only from 1 to ROWS-1 and 1 to COLS-1) }```
• 02-29-2012
rogster001
Quote:

Okay how do I do the IF statements that checks to see if the LIVE cell has 2 o3 neighbors? What do I put inside : if (?????)....
This is getting to the stage where you are practically having it written for you,
For example I wanted you to realise that out of bounds would occur by coding it yourself and getting all the program crashes, best way to learn.

IMO there are still things that are being ignored that wil distort the evaluation at each generation

You are doing the minimum each time you get some new advice, it seems you are just inserting the advice code and then saying 'ok how do i do the next bit?' and then adding that advice, etc.
You honestly mean to say you cannot write a series of if else statements to apply the rules? Even given the almost working version you have now? Itt seems you have become divorced from whatis actually happeing in the program or something and cant see it any more.

You use your live count in the if statements, obviously, that is why you go to the trouble of getting the live count in the first place, so you write conditions like,

if live count is such and such, and cell at r,c is such and such then...

Also where are you at with your compiling and testing? I am not trying to be critical just saying that you need to be incrementally building it at this stage, checking changes are compiling ok and outputting some data at least to show you that things are proceeding as expected.

The beauty of coding game of life is that there are known outcomes that can be recognised, so when you finally build the rules in and it does not work and stuff is running all over the screen you will tweak it, until finally a eureka moment that makes it worthwhile, you will see an oscillator or similar pattern that says 'done coded the game of life'

Well in your case this pattern that emerges depends on library input.. ie from your file, the tutor may have set up something nice to be shown when you crack it.

Finally dont be discouragedby blank 'worlds'; extinction occurs...

Finally Finally! I looked at the files you were supplied with, one is a bit of a red herring, whcih may confuse you but i can see why it was included
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12