Game Of Life

hi I am trying to implement conway game of life.
I am having errors when calculating value of neighbors
The following is what i have implemented.

Any suggestions would be really helpful.
Thanks in advance.

#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <string.h>

void playss(int **, int, int);
void print(int**,int);
int adjacent_to (int **, int, int );

int sizeX,sizeY;
int num_of_iterations;
int** temp;
int** current;
int** next;


int main(){

/* reading the size of the grid */
printf("enter size for mxm matrix");
scanf("%d",&sizeX);
sizeY=sizeX;
printf("\n%d,%d\n",sizeX,sizeY);

//initializing an double pointer array to hold the grid cell
current = new int*[sizeX];
for(int i=0; i < sizeX; ++i)
{ //int value;
current[i] = new int[sizeY];

}
// Reading the state of the grid from user input
printf("\n enter the state of the matrix\n");
for(int i=0; i<sizeX ; i++)
{ for(int j=0; j<sizeY; j++)
{
printf("enter state for a[%d][%d]",i,j);
scanf("%d",&current[i][j]);
}
}

// get the value for number of generations user want to generate

printf("number of iterations");
scanf("%d",&num_of_iterations);

#pragma
{
// starting game
for(int play=0;play<=num_of_iterations;play++)
{
// calculating the neighbors values
for (int i=0; i<sizeX; i++)
{
for (int j=0; j<sizeX; j++)
{
int n=0;
if ((i+1)<sizeX && current[i+1][j]==1)
n=n+1;
if ((i-1)>=0 && current[i-1][j]==1)
n=n+1;
if ((j+1)<sizeX && current[i][j+1]==1)
n=n+1;
if ((j-1)>=0 && current[i][j-1]==1)
n=n+1;

if ((i+1)<sizeX && (j+1)<sizeX && current[i+1][j+1]==1)
n=n+1;
if ((i+1)<sizeX && (j-1)>=0 && current[i+1][j-1]==1)
n=n+1;
if ((i-1)>=0 && (j+1)<sizeX && current[i-1][j+1]==1)
n=n+1;
if ((i-1)>=0 && (j-1)>=0 && current[i-1][j-1]==1)
n=n+1;

// decide the cell postion in next generation
if (n==3)
next[i][j]=1; // if neighbors == 3 make it alive

if (current[i][j]==1 && n!=2 && n!=3)// make it dead
next[i][j]=0;
}
}

//printing newly generated grid
printf("---------------------------------\n");
for(int i=0; i<sizeX; i++)
{
for(int j=0; j<sizeY; j++)
{
printf("%d",next[i][j]);
}
printf("\n");
}

printf("---------------------------------\n");
// swapping current grid and newly generated grid(next);
temp=current;
current=next;
next=temp;


}





}

return 0;
}



The program in running correctly but I think I am not check all boundary conditions correctly.

Last edited on
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
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <string.h>

void playss(int **, int, int);
void print(int**,int);
int adjacent_to (int **, int, int );

int sizeX,sizeY;
int num_of_iterations;
//int** b,
int** temp;
int** current;
int** next;


int main(){

printf("enter size for mxm matrix");
scanf("%d",&sizeX);
//scanf("%d",&sizeY);
sizeY=sizeX;
printf("\n%d,%d\n",sizeX,sizeY);

current = new int*[sizeX];
for(int i=0; i < sizeX; ++i)
{ //int value;
current[i] = new int[sizeY];

}
printf("\n enter the state of the matrix\n");
for(int i=0; i<sizeX ; i++)
{	for(int j=0; j<sizeY; j++)
{
printf("enter state for a[%d][%d]",i,j);
scanf("%d",&current[i][j]);
}
}



printf("number of iterations");
scanf("%d",&num_of_iterations);
printf("\nenter num of threads:");
scanf("%d",&nthreads);
next= current;


#pragma 
{

for(int play=0;play<=num_of_iterations;play++)
{

for (int i=0; i<sizeX; i++)
{
for (int j=0; j<sizeX; j++) 
{
int n=0;
if ((i+1)<sizeX && current[i+1][j]==1)
n=n+1;
if ((i-1)>=0 && current[i-1][j]==1)
n=n+1;
if ((j+1)<sizeX && current[i][j+1]==1)
n=n+1; 
if ((j-1)>=0 && current[i][j-1]==1)
n=n+1; 

if ((i+1)<sizeX && (j+1)<sizeX && current[i+1][j+1]==1)
n=n+1;
if ((i+1)<sizeX && (j-1)>=0 && current[i+1][j-1]==1)
n=n+1; 
if ((i-1)>=0 && (j+1)<sizeX && current[i-1][j+1]==1)
n=n+1; 
if ((i-1)>=0 && (j-1)>=0 && current[i-1][j-1]==1)
n=n+1; 


if (n==3)
next[i][j]=1; 

if (current[i][j]==1 && n!=2 && n!=3)
next[i][j]=0; 
}
}

printf("---------------------------------\n");
for(int i=0; i<sizeX; i++)
{
for(int j=0; j<sizeY; j++)
{
printf("%d",next[i][j]);
}
printf("\n");
}

printf("---------------------------------\n");

temp=current;
current=next;
next=temp;


}





}

return 0;
}


always wrap your code with code tags. It makes it alot easier for us to read.

Edit: Where are the errors? And also what line are you calculating the value of the players?
Last edited on
Thanks for reply .
formatted my code
Line 61 - Line 77 I was trying to calculate the number of neighbors.
Topic archived. No new replies allowed.