Skip to content

Commit c071136

Browse files
authored
Create program-for-conways-game-of-life
Initially, there is a grid with some cells which may be alive or dead. Our task is to generate the next generation of cells based on the following rules: Any live cell with fewer than two live neighbors dies as if caused by underpopulation. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
1 parent d221bd2 commit c071136

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)