Help game of life

my professor gave us most of this code and the main thing he wanted us to do was to set the rules for the game of life which was blank. My question is after setting the rules in applyrules function trying to run i don't really see how i am suppose to make it work without making alot of changes to the code it self. I am thinking that maybe the logic in my rules is off and i am not seeing it.

the comments were done by the professor

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
111
112
113
114
115
116
117
118
119
120
121
  const char LIFE  = 'L';
const char BLANK = '.';

const int N = 20;
const int M = 20;
const int nLife = M>N ? M/2 : N/2;
char world[N*M];    // will simulate 2-dimenisonal array.


// OUR FUNCTIONS (ie, TOOLS) that work on our DATA
void fillWorldwith(char w[], const int N, const int M, const char stuff){
 for(int i=0; i<N; i++)
    for(int j=0; j<M; j++)
     w[i*M+j*sizeof(char)]=stuff;
}

void fillWorldwithLife(char w[], const int N, const int M, const int Life){
 for(int k=0; k<Life; k++)
    {
        int i = rand()%N;
        int j = rand()%M;
       // cout << "i="<< i<< ", j="<< j ;
       // cout << " loc= "<<i+j*sizeof(char) << endl;
        w[i*M+j*sizeof(char)]=LIFE;
    }
}

void displayWorld(char *w, const int N, const int M){
 for(int i=0; i<N; i++)
 {
   for(int j=0; j<M; j++)
     cout << setw(2) << w[i*M+j*sizeof(char)];
   cout << endl;
 }
}

// Used in applyRules as a buffer to update world.
void copyWorld(char dest[], char src[], const int N, const int M){

  for(int i=0; i<N; i++)
    for(int j=0; j<M; j++)
      dest[i*M+j*sizeof(char)]= src[i*M+j*sizeof(char)];
}

// Rules
void applyRules(char  w[], const int N, const int M)
{
    //char *t = (char *) malloc( sizeof(char)*N*M);
    char *t = new char[N*M];  // create a temporary world
	int connect;
    copyWorld(t, w, N, M); // copy world w to temporary world t
    for(int i=0; i<N; i++)
     for(int j=0; j<M; j++)
     {
		connect = 0;
     if (w[(i-1)*M+j*sizeof(char)] == LIFE) 
		 connect +=1;							// rules look at w, to change t
	 if (w[(i-1)*M+(j-1)*sizeof(char)] == LIFE)
		 connect +=1;
	 if (w[(i-1)*M+(j+1)*sizeof(char)] == LIFE)
		 connect +=1;
	 if (w[(i+1)*M+(j-1)*sizeof(char)] == LIFE)
		 connect +=1;
	 if (w[(i+1)*M+(j+1)*sizeof(char)] == LIFE)
		 connect +=1;
	 if (w[(i+1)*M+j*sizeof(char)] == LIFE)
		 connect +=1;
	 if (w[i*M+(j-1)*sizeof(char)] == LIFE)
		 connect +=1;
     if (w[i*M+(j+1)*sizeof(char)] == LIFE)
		 connect +=1;
	 //cout << connect;
    
	 if (w[i*M+j*sizeof(char)] == LIFE && connect > 3)
		 t[i*M+j*sizeof(char)] = BLANK;
	 else if (w[i*M+j*sizeof(char)] == LIFE && connect < 2)
		 t[i*M+j*sizeof(char)] = BLANK;
	 else if (w[i*M+j*sizeof(char)] == LIFE && (connect == 2 || connect == 3))
		 t[i*M+j*sizeof(char)] = LIFE;
	 else if (w[i*M+j*sizeof(char)] == BLANK && connect == 3)
		 t[i*M+j*sizeof(char)] = LIFE;
     }

    copyWorld(w, t, N, M);  // copy temp world to w world: w <- temp
	//system("pause");
}


int main()
{
    SMALL_RECT windowSize= {0, 0, 200, 200};                              // windows
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);  // windows

//    windowSize ;
    SetConsoleWindowInfo(hConsole, TRUE, &windowSize); // windows
    SetConsoleTitle(TEXT("Game of Life"));                   // windows

    system("cls");               // clear the console
    stringstream ss;  // trick to combine string and numbers
    ss << "color " << Black << Yellow;

    // objects are fun to use.
    system( ss.str().c_str() );   // change console colour

    fillWorldwith(world, N, M, BLANK);     // fill whole world with BLANKS
    displayWorld(world, N, M);
    // cout << endl << "now add stuff" << endl;

    for(int time=0; time<100; time++){  // each loop, considered time.
     system("cls");
     fillWorldwithLife(world, N, M, nLife);  // new random life
	 applyRules(world,N,M);
     displayWorld(world, N, M);              // display world
     Sleep(500);                             // delay so we can see world
	 
   fillWorldwith(world, N, M, BLANK);      // fill whole world with BLANKS
    }

    return 0;
}
Last edited on
You need to insert more M/2 or N/2 new random lives for the rules to not kill all of them in the first iteration. The boundary conditions need to be fixed as well.
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
    for(int i=0; i<N; i++)
     for(int j=0; j<M; j++) {
        // rules look at w, to change t
        connect = 0;

//--------------------------------------
// sizeof(char) is not needed 
// because sizeof(char) = 1, so j*1 = j
//
// What happens when i = 0, then 
// i = 0 and j = 0 then (i-1)*M+j = -20
// -20 as an index may cause a segmentaion 
// fault
        if (w[(i-1)*M+j] == LIFE) 
           connect +=1;							
//--------------------------------------
// What happens when i = 0, then 
// i = 0 and j = 0 then 
// (i-1)*M+(j-1) = -21
        if (w[(i-1)*M+(j-1)] == LIFE)
           connect +=1;
        if (w[(i-1)*M+(j+1)] == LIFE)
           connect +=1;
        if (w[(i+1)*M+(j-1)] == LIFE)
           connect +=1;
//--------------------------------------
// What happens when i = 0, then 
// i = N-1 and j = M-1 then 
// (i+1)*M+(j+1) = 20*20+20 = 420
// but your array is on 20*20 in size
        if (w[(i+1)*M+(j+1)] == LIFE)
           connect +=1;
        if (w[(i+1)*M+j] == LIFE)
           connect +=1;
        if (w[i*M+(j-1)] == LIFE)
           connect +=1;
        if (w[i*M+(j+1)] == LIFE)
           connect +=1;
        


int main() {
//--------------------------------------
// If you want to have a different game
// board each time uncomment the line 
// below.  srand will seed the random
// algorithm 
// srand(time(0));
    fillWorldwith(world, N, M, BLANK); 
//--------------------------------------
// You need to insert more M/2 or N/2 
// new random lives for the rules to not
// kill all of them in the first iteration 
    fillWorldwithLife(world, N, M, 75);  // <--- 75 new random lives 
    displayWorld(world, N, M);

   // Each loop, considered time
   for(int time(0); time<100; time++){  
        applyRules(world,N,M);
        displayWorld(world, N, M);    
        sleep(1);                    
//----------------------------------------
// Don't fill the world again with BLANKS
//fillWorldwith(world, N, M, BLANK);    
    }
    return 0;
}
Start
 . L . . . . . . L . . L L . L . . . . .
 . . . . . . L . L . L . . . . . . L . .
 . . . . . . . . . . . . . . . . . . L .
 . L . . . . . . . L L . . . . . . . L .
 . . . . L L . . . . . L . . . . . L . .
 . . . . . L . . . . . L L . . L . . . L
 . L . L . . . . . . L . . . . . . . . .
 . . . . . . . . . . . . . L L . . . . .
 L . . . . . . . . . . L L L . . . . . .
 L . . . L . . . . . . . L . . . . L . .
 L . . . . . . . . . . . L . . L . L L .
 L . . . . . . . L . . . . L L . . . . .
 . . . . . . . . . . . . . . . . . L . .
 . . L . L . . L . . . . . . . . . . . .
 . . . . . . . . . . . . . . . . . . . .
 . L . . . . . . . . . . . . . . . . L .
 L L . . . . . . . L . . L L . . . . . .
 . . . . L . . . . . L L . . . . L . . .
 . . L . L L L L . . . . . . . L . . . .
 . L . L . . . . . . . . . . . . . L . .
----------------------------------------
Iteration #1
 . . . . . . . L . L . L . . . . . . . .
 . . . . . . . L . L . L . . . . . . . .
 . . . . . . . . . . L . . . . . . L L .
 . . . . . . . . . . L . . . . . . L L .
 . . . . L L . . . . . L L . . . . . L .
 . . . . . L . . . . L L L . . . . . . .
 . . . . . . . . . . . L L L L . . . . .
 L . . . . . . . . . . L . L L . . . . .
 . . . . . . . . . . . L . . L . . . . L
 L L . . . . . . . . . . . . . . L L L .
 L L . . . . . . . . . . L . L . L L L L
 . . . . . . . . . . . . . L L . L L L .
 . . . . . . . . . . . . . . . . . . . .
 . . . . . . . . . . . . . . . . . . . .
 . . . . . . . . . . . . . . . . . . . .
 L L . . . . . . . . . . . . . . . . . .
 L L . . . . . . . . L L L . . . . . . .
 . L . L L . L . . . L L L . . . . . . .
 . . L . L L L . . . . . . . . . L . . .
 . . L L L L L . . . . . . . . . . . . .
----------------------------------------
Iteration #2
 . . . . . . . . . . . . . . . . . . . .
 . . . . . . . . . L . L . . . . . . . .
 . . . . . . . . . L L L . . . . . L L .
 . . . . . . . . . . L . . . . . . . . L
 . . . . L L . . . . . . L . . . . L L .
 . . . . L L . . . . L . . . . . . . . .
 . . . . . . . . . . . . . . L . . . . .
 . . . . . . . . . . L L . . . L . . . L
 . L . . . . . . . . . . L L L L . L L L
 . L . . . . . . . . . . . L . . L . . .
 . L . . . . . . . . . . . . L . . . . .
 L . . . . . . . . . . . . L L . L . . L
 . . . . . . . . . . . . . . . . . L . .
 . . . . . . . . . . . . . . . . . . . .
 . . . . . . . . . . . . . . . . . . . .
 L L . . . . . . . . . L . . . . . . . .
 . . . . . . . . . . L . L . . . . . . .
 L L . L L . L . . . L . L . . . . . . .
 . L . . . . . L . . . L . . . . . . . .
 . . L . . . L . . . . . . . . . . . . .
Last edited on
Topic archived. No new replies allowed.