Conway's Game of Life Origin Function

Hey everybody,

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.

Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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;
    }
    else if (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;
                }
                else if (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

        }

        else if (typeOfEntity[countX] == 2) {
        // code to assign a Left Glider to the entity

        }

        else {
        // code to assign a Right Glider to the entity

        }
        }
 }
}
Topic archived. No new replies allowed.