game of life

so i need to write a game of life code i have most of it done but it needs to start with a random pattern, i also need to calculate fill percentage and set it up so that i can see on which cycle is on. i dont know how i could do that ive tried many things but they either give me the wrong calculations and outputs or the program crashes.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
#define ALIVE true
#define DEAD false
using namespace std;
int cycles;

const int ROWS = 24;
const int COLS = 79;



void Display(bool grid[ROWS][COLS])
{


    for(int a = 1; a < ROWS; a++){
        for(int b = 1; b < COLS; b++){
            if(grid[a][b] == true){
               cout << "Q";
            }
            else{
                cout << " ";
            }
 //           if(b == COLS){
 //               std::cout << std::endl;
 //           }

        }
        cout << endl;
    }

}

//This copy's the grid for comparision purposes.
void CopyGrid (bool grid[ROWS][COLS],bool grid2[ROWS][COLS]){
    for(int a =0; a < ROWS; a++){
        for(int b = 0; b < COLS; b++){grid2[a][b] = grid[a][b];}

    }

}
//Calculates Life or Death
void liveOrDie(bool grid[ROWS][COLS])
{

    bool grid2[ROWS][COLS] = {};
    CopyGrid(grid, grid2);
    for(int a = 1; a < ROWS+1; a++)
        {
        for(int b = 1; b < COLS+1; b++)
        {
            int neighbors = 0;
            for(int c = -1; c < 2; c++)
            {
                for(int d = -1; d < 2; d++)
                {
                    if(!(c == 0 && d == 0))
                    {
                        if(grid2[a+c][b+d]) {++neighbors;}
                    }
                }
            }

            if(neighbors < 2) {grid[a][b] = DEAD;}
            else if(neighbors == 3) {grid[a][b] = ALIVE;}
            else if(neighbors > 3) {grid[a][b] = DEAD;}

        }

    }
}

int main()
{
    srand(time(0));



    bool grid[ROWS][COLS] = {};

    grid[ROWS/2][COLS/2] = true;
    grid[ROWS/2-1][COLS/2] = true;
    grid[ROWS/2][COLS/2+1] = true;
    grid[ROWS/2][COLS/2-1] = true;
    grid[ROWS/2+1][COLS/2+1] = true;
               for (int i = 1; i = cycles ; i++)
                {

}

    while (true){


        Display(grid);     //This is our display.
        liveOrDie(grid); //calculate if it lives or dies.
        Sleep(500);
// std::cin.get();


    }
return 0;

}
Last edited on
What exactly is your problem ?
What exactly is your problem ?

i have no idea how to start the code for the problems listed. or rather the ideas i did have were failures. basically i dont know where i should start the random pattern code or how i would write it i have tried doing using srand((time(0)) but im probably missing something and also i cant make the code output which cycle is on if im making sense
Last edited on
Topic archived. No new replies allowed.