Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit da39e0c

Browse files
Merge pull request #122 from AnishKr91620/patch-1
Create program-for-conways-game-of-life
2 parents ade2ca7 + c071136 commit da39e0c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

‎program-for-conways-game-of-life

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
//change row and column value to set the canvas size
5+
int row = 5;
6+
int col = 4;
7+
8+
//creates row boundary
9+
int row_line(){
10+
printf("\n");
11+
for(int i=0; i<col; i++){printf(" -----");}
12+
printf("\n");
13+
}
14+
15+
//returns the count of alive neighbours
16+
int count_live_neighbour_cell(int a[row][col], int r, int c){
17+
int i, j, count=0;
18+
for(i=r-1; i<=r+1; i++){
19+
for(j=c-1;j<=c+1;j++){
20+
if((i==r && j==c) || (i<0 || j<0) || (i>=row || j>=col)){
21+
continue;
22+
}
23+
if(a[i][j]==1){
24+
count++;
25+
}
26+
}
27+
}
28+
return count;
29+
}
30+
31+
int main(){
32+
int a[row][col], b[row][col];
33+
int i,j;
34+
int neighbour_live_cell;
35+
36+
//generate matrix canvas with random values (live and dead cells)
37+
for(i=0; i<row; i++){
38+
for(j=0;j<col;j++){
39+
a[i][j] = rand() % 2;
40+
}
41+
}
42+
43+
//print array matrix
44+
printf("Initial Stage:");
45+
row_line();
46+
for(i=0; i<row; i++){
47+
printf(":");
48+
for(j=0;j<col;j++){
49+
printf(" %d :",a[i][j]);
50+
}
51+
row_line();
52+
}
53+
54+
//next canvas values based on live neighbour count
55+
for(i=0; i<row; i++){
56+
for(j=0;j<col;j++){
57+
neighbour_live_cell = count_live_neighbour_cell(a,i,j);
58+
if(a[i][j]==1 && (neighbour_live_cell==2 || neighbour_live_cell==3)){
59+
b[i][j]=1;
60+
}
61+
62+
else if(a[i][j]==0 && neighbour_live_cell==3){
63+
b[i][j]=1;
64+
}
65+
66+
else{
67+
b[i][j]=0;
68+
}
69+
}
70+
}
71+
72+
//print next generation
73+
printf("\nNext Generation:");
74+
row_line(row);
75+
for(i=0; i<row; i++){
76+
printf(":");
77+
for(j=0;j<col;j++){
78+
printf(" %d :",b[i][j]);
79+
}
80+
row_line(row);
81+
}
82+
83+
return 0;
84+
}
85+
86+
/*
87+
###################################### OUTPUT ####################################
88+
Initial Stage:
89+
----- ----- ----- -----
90+
: 1 : 1 : 0 : 0 :
91+
----- ----- ----- -----
92+
: 1 : 0 : 0 : 0 :
93+
----- ----- ----- -----
94+
: 0 : 0 : 1 : 1 :
95+
----- ----- ----- -----
96+
: 1 : 1 : 1 : 1 :
97+
----- ----- ----- -----
98+
: 1 : 0 : 1 : 0 :
99+
----- ----- ----- -----
100+
101+
Next Generation:
102+
----- ----- ----- -----
103+
: 1 : 1 : 0 : 0 :
104+
----- ----- ----- -----
105+
: 1 : 0 : 1 : 0 :
106+
----- ----- ----- -----
107+
: 1 : 0 : 0 : 1 :
108+
----- ----- ----- -----
109+
: 1 : 0 : 0 : 0 :
110+
----- ----- ----- -----
111+
: 1 : 0 : 1 : 1 :
112+
----- ----- ----- -----
113+
114+
*/
115+
//This code is contributed by adisg25 - Aditya Singh

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /