Game Of Life

I came up with this code to simulate Conway's Game of Life. But somehow the output is unexpected. Please suggest corrections.

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void matdis(char a1[5][5]) // It displays char array
{
for(int u = 0; u<5; u++)
{
for(int g = 0; g<5; g++)
{
cout<<a1[u][g]<<" ";
}
cout<<endl;
}
}

void transcriptor(int a1[5][5], char a2[5][5]) // It translates int into char for display
{
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
if(a1[i][j]==1)
{
a2[i][j] = 'x';
}
else if(a1[i][j]==0)
{
a2[i][j] = '\0';
}

}
}
}

int nsum(int a1[5][5], int i, int j) // Calculates sum of diagonal elements
{
int sum = a1[i-1][j-1] + a1[i][j-1] + a1[i+1][j-1] + a1[i+1][j] + a1[i+1][j+1] + a1[i][j+1] + a1[i-1][j+1] + a1[i-1][j];
return sum;
}

void rules(int a1[5][5], int a2[5][5]) // Array1 to Array2 Generation simulator
{
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
if(a1[i][j]==1)
{
if(nsum(a1, i, j)==1)
{
a2[i][j] = 0;
}
else if (nsum(a1,i,j)==2 || nsum(a1,i,j)==3)
{
a2[i][j] = 1;
}
else if(nsum(a1,i,j)>3)
{
a2[i][j] = 0;
}
}
else if(a1[i][j]==0)
{
if(nsum(a1,i,j)==3)
{
a2[i][j]==1;
}
}
}
}
}

int main() // All the rest
{
int t = 0; int a1[5][5]; int a2[5][5];
char a3[5][5];
cout<<" \t\t Welcome to the Game of Life "<<endl;
cout<<" \t Provide the automaton with initial conditions, and see it proceed"<<endl;
for(int a = 0; a<5; a++)
{
for(int b = 0; b<5; b++)
{
a2[a][b]='\0';
}
}
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
cin>>a1[i][j];
}
}
int stopcon = 1;
while(stopcon)
{
for(int y = 0; y<5; y++)
{
for(int z = 0; z<5; z++)
{
a3[y][z] = '\0';
}
}
if(t%2==0)
{
rules(a1,a2);
transcriptor(a2,a3);
matdis(a3);
}
else
{
rules(a2,a1);
transcriptor(a1,a3);
matdis(a3);
}
cout<<" Go for another generation? Y : 1 . N : 0 "<<endl;
cin>>stopcon;
t++;
}
getch(); return 0;
}
Last edited on
Please use the code tags for the code parts of your post. It will make it much easier to read.
try and comment your codes for eay understand
Try running this and let me know if this is what you expect.
I changed the getch to cin.ignore();
and removed the conio file.

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
122
123
124
125
126
127
#include<iostream.h>
#include<stdio.h>

void matdis(char a1[5][5]) // It displays char array
{
for(int u = 0; u<5; u++)
{
for(int g = 0; g<5; g++)
{
cout<<a1[u][g]<<" ";
}
cout<<endl;
}
}

void transcriptor(int a1[5][5], char a2[5][5]) // It translates int into char for display
{
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
if(a1[i][j]==1)
{
a2[i][j] = 'x';
}
else if(a1[i][j]==0)
{
a2[i][j] = '\0';
}

}
}
} 

int nsum(int a1[5][5], int i, int j) // Calculates sum of diagonal elements
{
int sum = a1[i-1][j-1] + a1[i][j-1] + a1[i+1][j-1] + a1[i+1][j] + a1[i+1][j+1] + a1[i][j+1] + a1[i-1][j+1] + a1[i-1][j];
return sum;
}

void rules(int a1[5][5], int a2[5][5]) // Array1 to Array2 Generation simulator
{
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
if(a1[i][j]==1)
{
if(nsum(a1, i, j)==1)
{
a2[i][j] = 0;
}
else if (nsum(a1,i,j)==2 || nsum(a1,i,j)==3)
{
a2[i][j] = 1;
}
else if(nsum(a1,i,j)>3)
{
a2[i][j] = 0;
}
}
else if(a1[i][j]==0)
{
if(nsum(a1,i,j)==3)
{
a2[i][j]==1;
}
}
}
}
}

int main() // All the rest
{
int t = 0; int a1[5][5]; int a2[5][5];
char a3[5][5];
cout<<" \t\t Welcome to the Game of Life "<<endl;
cout<<" \t Provide the automaton with initial conditions, and see it proceed"<<endl;
for(int a = 0; a<5; a++)
{
for(int b = 0; b<5; b++)
{
a2[a][b]='\0';
}
}
for(int i = 0; i<5; i++)
{
for(int j = 0; j<5; j++)
{
cin>>a1[i][j];
}
}
int stopcon = 1;
while(stopcon)
{ 
for(int y = 0; y<5; y++)
{
for(int z = 0; z<5; z++)
{
a3[y][z] = '\0';
}
}
if(t%2==0)
{
rules(a1,a2);
transcriptor(a2,a3);
matdis(a3);
}
else
{
rules(a2,a1);
transcriptor(a1,a3);
matdis(a3);
}
cout<<" Go for another generation? Y : 1 . N : 0 "<<endl;
cin>>stopcon;
t++;
}
cin.ignore(); 

return 0;
} 
 



Topic archived. No new replies allowed.