I've spent an embarrassing number of hours trying to figure this one part out, but am still stuck and desperate for some help.
The professor has us starting us out with making a "U" shape w/ 1s while the grid is 0s, and updating that according to the rules.
The problem is, is that when it updates to the next generation, the 1's that are supposed to update do update, but now in this second generation there are the old 1s and the new 1s, and there is overpopulating going on, but no cells are dying (Ex. One cell has 5 1's around it, and according to the rules and my code it should be dead, but it doesn't die and remains a 1, even in the 3rd, 4th 5th generation and so on).
I have a feeling this has to do with rule 6: "All births and deaths take place at exactly the same time, so that dying cells can help to give birth to another, but cannot prevent the death of others by reducing overcrowding, nor can cells being born either preserve or kill cells in the previous generation." or this response I found online to another question. I don't quite understand the response. I do have a temporary Array that I am transferring to and from the main Array.
I hope someone can point me in the right direction. Thank you so much.
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
using namespace std;
/////////////GLOBAL VARIABLES/////////////
int row, col;
char PorQ;
const int MAX_COL = 60;
const int MAX_ROW = 30;
int currentArray[MAX_ROW][MAX_COL];
int tempArray[MAX_ROW][MAX_COL];
int Array[MAX_ROW][MAX_COL];
/////////////FUNCTION DECLARATIONS/////////////
void displayMenu(void);
void setZeroArray (int tempArray[MAX_ROW][MAX_COL], int currentArray[MAX_ROW][MAX_COL]);
void setInitialPatternArray (void);
void copyArray(int tempArray[MAX_ROW][MAX_COL], int currentArray[MAX_ROW][MAX_COL]);
void displayArray (int Array[MAX_ROW][MAX_COL]);
void setNextGenArray (int currentArray[MAX_ROW][MAX_COL], int tempArray[MAX_ROW][MAX_COL]);
/////////////MAIN FUNCTION/////////////
int main()
{
displayMenu();
setZeroArray(currentArray, tempArray);
setInitialPatternArray();
copyArray(tempArray, currentArray);
displayArray (currentArray);
cout << endl << ">> ";
cin >> PorQ;
//quitting first time
if ((PorQ == 'Q') || (PorQ == 'q'))
{
exit(0);
}
while ((PorQ != 'Q') && (PorQ != 'q') && (PorQ != 'P') && (PorQ != 'p'))
{
cout << "Invalid Entry. Press [P]lay or [Q]uit." << endl;
cin >> PorQ;
}
if ((PorQ == 'P') || (PorQ == 'p'))
{
char ans;
bool t;
do
{
while (1)
{
if (t = kbhit())//checks for user keyboard input
{
break;
}
else
{
system("cls");
setNextGenArray (currentArray, tempArray);
copyArray(tempArray, currentArray);
displayArray (currentArray);
Sleep(1000);
}
}
ans = getch();//if user keyboard input is q, quit
}while (ans != 'q');
}
return 0;
}
/////////////FUNCTION DEFINITIONS/////////////
void displayMenu(void)
{
cout << "[P]lay > Press 'P' to play." << endl;
cout << "[Q]uit > Press 'Q' to exit." << endl;
}
void setZeroArray (int array1[MAX_ROW][MAX_COL], int array2[MAX_ROW][MAX_COL])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
array1[row][col] = 0;
}
}
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
array2[row][col] = 0;
}
}
}
void setInitialPatternArray (void)
{
srand(time(NULL));
int randomrow;
for (int i = 0; i <100; i++)
{
randomrow = rand() % (MAX_ROW - 6);
}
srand(time(NULL));
int randomcol;
for (int i = 0; i <100; i++)
{
randomcol = rand() % (MAX_COL - 6);
}
for (row = 0 + randomrow; row < MAX_ROW; row++)
{
for(col = 0 + randomcol; col < MAX_COL; col++)
{
if (row <= 4 + randomrow && col == 0 + randomcol)
{
tempArray[row][col] = 1;
}
if (row == 5 + randomrow && col <= 6 + randomcol)
{
tempArray[row][col] = 1;
}
if (row <= 5 + randomrow && col == 6 + randomcol)
{
tempArray[row][col] = 1;
}
}
}
}
void copyArray(int array1[MAX_ROW][MAX_COL], int array2[MAX_ROW][MAX_COL])
{
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
array2[row][col] = array1[row][col];
}
}
}
void displayArray (int Array[MAX_ROW][MAX_COL])
{
for (row = 0; row < MAX_ROW; row++)
{
for(col = 0; col < MAX_COL; col++)
{
cout << Array[row][col] << " ";
}
}
}
void setNextGenArray (int currentArray[MAX_ROW][MAX_COL], int tempArray[MAX_ROW][MAX_COL])
{
//copies currentArray into tempArray so that GoL modifications can take place on tempArray
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
tempArray[row][col] = currentArray[row][col];
}
}
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
tempArray[row][col] = 0;
}
}
//tempArray now reflects current state of currentArray that is displayed. will modify below v
//MIDDLE CELLS - check surrounding 8
for (int row = 0; row < MAX_ROW; row++)
{
for (int col = 0; col < MAX_COL; col++)
{
if (((row > 1) && (row < MAX_ROW)) && ((col > 1) && (col < MAX_COL)))
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1) //if there is a 1 in the box
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row-1][col-1] == 1)
counter++;
if (tempArray[row][col-1] == 1)
counter++;
if (tempArray[row+1][col-1] == 1)
counter++;
if (tempArray[row+1][col] == 1)
counter++;
if (tempArray[row+1][col+1] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
if (tempArray[row-1][col+1] == 1)
counter++;
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter = 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else if (tempArray[row][col] == 0)//if cell is 0, check around to see if surrounded by 3
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row-1][col-1] == 1)
dedtoliv++;
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (tempArray[row+1][col+1] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (tempArray[row-1][col+1] == 1)
dedtoliv++;
if (dedtoliv == 3) //if the box that has 0 in it is surrounded by 3, the cell will become a 1
tempArray[row][col] = 1;
//ded cell to liv
}
}
}
}
}
/*FOCUS ON MIDDLE CELLS FIRST TO FIGURE OUT ISSUE
//CORNER CELLS - check surrounding 3
//upper left corner
for (int row = 0; row < 1; row++)
{
for (int col = 0; col < 1; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row+1][col] == 1)
counter++;
if (tempArray[row+1][col+1] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (tempArray[row+1][col+1] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (dedtoliv == 3) //if the box that has 0 in it is surrounded by 3, the cell will become a 1
tempArray[row][col] = 1;
//ded cell to liv
}
}
}
//lower left corner
for (int row = 30; row < 31; row++)
{
for (int col = 0; col < 1; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
if (tempArray[row-1][col+1] == 1)
counter++;
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (tempArray[row-1][col+1] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
//upper right corner
for (int row = 0; row < 1; row++)
{
for (int col = 60; col < 61; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row][col-1] == 1)
counter++;
if (tempArray[row+1][col-1] == 1)
counter++;
if (tempArray[row+1][col] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
}
}
}
//lower right corner
for (int row = 30; row < 31; row++)
{
for (int col = 60; col < 61; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row-1][col-1] == 1)
counter++;
if (tempArray[row][col-1] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
}
else
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row-1][col-1] == 1)
dedtoliv++;
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
//SIDES - check surrounding 5
//left side
for (int row = 1; row < 30; row++)
{
for (int col = 0; col < 1; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row+1][col] == 1)
counter++;
if (tempArray[row+1][col+1] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
if (tempArray[row-1][col+1] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (tempArray[row+1][col+1] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (tempArray[row-1][col+1] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
//bottom
for (int row = 30; row < 31; row++)
{
for (int col = 1; col < 60; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row-1][col-1] == 1)
counter++;
if (tempArray[row][col-1] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
if (tempArray[row-1][col+1] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row-1][col-1] == 1)
dedtoliv++;
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (tempArray[row-1][col+1] ==1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
//right side
for (int row = 1; row < 30; row++)
{
for (int col = 60; col < 61; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row-1][col] == 1)
counter++;
if (tempArray[row-1][col-1] == 1)
counter++;
if (tempArray[row][col-1] == 1)
counter++;
if (tempArray[row+1][col-1] == 1)
counter++;
if (tempArray[row+1][col] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row-1][col] == 1)
dedtoliv++;
if (tempArray[row-1][col-1] == 1)
dedtoliv++;
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
//top
for (int row = 0; row < 1; row++)
{
for (int col = 1; col < 60; col++)
{
int counter = 0;
int dedtoliv = 0;
if (tempArray[row][col] == 1)
{
if (tempArray[row][col-1] == 1)
counter++;
if (tempArray[row+1][col-1] == 1)
counter++;
if (tempArray[row+1][col] == 1)
counter++;
if (tempArray[row+1][col+1] == 1)
counter++;
if (tempArray[row][col+1] == 1)
counter++;
//cell ded or liv
if(counter <= 1)
tempArray[row][col] = 0;
//The cell ded.
if((counter == 3) || (counter == 2))
tempArray[row][col] = 1;
//The cell liv.
if(counter >= 4)
tempArray[row][col] = 0;
//The cell ded.
}
else
{
if (tempArray[row][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col-1] == 1)
dedtoliv++;
if (tempArray[row+1][col] == 1)
dedtoliv++;
if (tempArray[row+1][col+1] == 1)
dedtoliv++;
if (tempArray[row][col+1] == 1)
dedtoliv++;
if (dedtoliv == 3)
tempArray[row][col] = 1;
//The ded cell to liv cell
}
}
}
}
*/