I've been working on a function to initiate a Game of Life with random blinkers and gliders. I'm coming across two problems with the code. First, say I enter in 6 when the program asks "How many life forms?" the result is usually 2 of the same type of life forms, instead of 6.
The second problem is about half the time, no matter what number is entered for number of life forms, I get a Bus Error: 10. I tried to avoid this by checking to make sure the life forms were in the bounds of the array (univ[WIDTH][HEIGHT] with WIDTH = 90 and HEIGHT = 70) by lines 18-23 and 27-32.
I've only entered the code from the function as the whole program is pretty lengthy, but let me know if you would like to try running it and I'll enter in the whole program.
Also, if it makes any difference, I'm using a secure shell app for Google Chrome to run the program.
void genesis(bool univ[][WIDTH]){
// How many entities we are starting with
cout << "How many life forms would you like to start with?" << endl;
int NumOfEntities;
cin >> NumOfEntities;
int locationX[NumOfEntities];
int locationY[NumOfEntities];
int typeOfEntity[NumOfEntities];
int LifeForms = 3;
srand((unsigned)time(0));
// Have a starting location for each entity
for (int countX = 0; countX < NumOfEntities; countX++) {
locationX[countX] = rand()% WIDTH;
// keeps entity within the bounds of the WIDTH and HEIGHT arrays
if (locationX[countX] < 3) {
locationX[countX] = locationX[countX] + 5;
}
elseif (locationX[countX] > (WIDTH - 3)) {
locationX[countX] = locationX[countX] - 5;
}
for (int countY = 0; countY < NumOfEntities; countY++){
srand((unsigned)time(0));
locationY[countY] = rand()% HEIGHT;
if (locationY[countY] < 3) {
locationY[countY] = locationY[countY] + 5;
}
elseif (locationY[countY] > (HEIGHT - 3)) {
locationY[countY] = locationY[countY] - 5;
}
}
}
// Assign a random type of life to each entity
for (int countX = 0; countX < NumOfEntities; countX++){
srand((unsigned)time(0));
typeOfEntity[countX] = rand() % LifeForms + 1;
for (int count = 0; count < NumOfEntities; count++) {
if (typeOfEntity[countX] == 1) {
//code to assign a blinker to the entity
}
elseif (typeOfEntity[countX] == 2) {
// code to assign a Left Glider to the entity
}
else {
// code to assign a Right Glider to the entity
}
}
}
}