Conways Game of life game in C
-
============================================================================ Name : new.c Author : Abhinav Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include #include void compute(int N[6][6],int Z[6][6]) { //compute neighbours int i,j; for (i = 1; i <=6; i++) { for (j = 1; j <= 6; j++) { N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1] + Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1] +Z[i][j+1]+Z[i+1][j+1]; } } } // This method is going to print the Matrix that we have defined in the main method void show(int Z[6][6]) { printf("****************************************************************\n"); int i,j; // Incrementing the value of integer i so that it can be used as an index value for matrix 'Z'. for (i = 0; i < 6; i++) { // Incrementing the value of integer j to be used as the second index value for matrix 'Z'. for (j = 0; j < 6; j++) { // This will print the elements present in th array line wise. printf("%d ", Z[i][j]); } printf("\n"); } printf("****************************************************************\n"); } /* This method is going to take the value of elements from the array and compute the output according to some conditions. Like for the rule of UnderPopulation we have to check that if the element at some index is surrounded by less than two live elements than the element is going to die. */ void iteration(int Z[6][6]){ int N[6][6]= {{0}}; int i,j; compute(N,Z); for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((Z[i][j] == 1)&&(N[i][j] < 2 || N[i][j] > 3)) { Z[i][j] = 0; } else if((Z[i][j] == 0)&&(N[i][j] == 3)) { Z[i][j] = 1; } } } /* for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((N[i][j] < 2 )) // Rule number 1 - Under population { if (Z[i][j]==1&&((N[i][j] ==2 )||(N[i][j] == 3 ))) // Rule 4 - Next generation { Z[i][j] = 1; } else { Z[i][j] = 0;
-
============================================================================ Name : new.c Author : Abhinav Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include #include void compute(int N[6][6],int Z[6][6]) { //compute neighbours int i,j; for (i = 1; i <=6; i++) { for (j = 1; j <= 6; j++) { N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1] + Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1] +Z[i][j+1]+Z[i+1][j+1]; } } } // This method is going to print the Matrix that we have defined in the main method void show(int Z[6][6]) { printf("****************************************************************\n"); int i,j; // Incrementing the value of integer i so that it can be used as an index value for matrix 'Z'. for (i = 0; i < 6; i++) { // Incrementing the value of integer j to be used as the second index value for matrix 'Z'. for (j = 0; j < 6; j++) { // This will print the elements present in th array line wise. printf("%d ", Z[i][j]); } printf("\n"); } printf("****************************************************************\n"); } /* This method is going to take the value of elements from the array and compute the output according to some conditions. Like for the rule of UnderPopulation we have to check that if the element at some index is surrounded by less than two live elements than the element is going to die. */ void iteration(int Z[6][6]){ int N[6][6]= {{0}}; int i,j; compute(N,Z); for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((Z[i][j] == 1)&&(N[i][j] < 2 || N[i][j] > 3)) { Z[i][j] = 0; } else if((Z[i][j] == 0)&&(N[i][j] == 3)) { Z[i][j] = 1; } } } /* for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((N[i][j] < 2 )) // Rule number 1 - Under population { if (Z[i][j]==1&&((N[i][j] ==2 )||(N[i][j] == 3 ))) // Rule 4 - Next generation { Z[i][j] = 1; } else { Z[i][j] = 0;
-
============================================================================ Name : new.c Author : Abhinav Version : Copyright : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */ #include #include void compute(int N[6][6],int Z[6][6]) { //compute neighbours int i,j; for (i = 1; i <=6; i++) { for (j = 1; j <= 6; j++) { N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1] + Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1] +Z[i][j+1]+Z[i+1][j+1]; } } } // This method is going to print the Matrix that we have defined in the main method void show(int Z[6][6]) { printf("****************************************************************\n"); int i,j; // Incrementing the value of integer i so that it can be used as an index value for matrix 'Z'. for (i = 0; i < 6; i++) { // Incrementing the value of integer j to be used as the second index value for matrix 'Z'. for (j = 0; j < 6; j++) { // This will print the elements present in th array line wise. printf("%d ", Z[i][j]); } printf("\n"); } printf("****************************************************************\n"); } /* This method is going to take the value of elements from the array and compute the output according to some conditions. Like for the rule of UnderPopulation we have to check that if the element at some index is surrounded by less than two live elements than the element is going to die. */ void iteration(int Z[6][6]){ int N[6][6]= {{0}}; int i,j; compute(N,Z); for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((Z[i][j] == 1)&&(N[i][j] < 2 || N[i][j] > 3)) { Z[i][j] = 0; } else if((Z[i][j] == 0)&&(N[i][j] == 3)) { Z[i][j] = 1; } } } /* for (i = 0; i < 6; i++) { for (j = 0; j < 6; j++) { if((N[i][j] < 2 )) // Rule number 1 - Under population { if (Z[i][j]==1&&((N[i][j] ==2 )||(N[i][j] == 3 ))) // Rule 4 - Next generation { Z[i][j] = 1; } else { Z[i][j] = 0;
When calculating the N array, the element of the boundary is overstepped by the Z array access as calculated by your algorithm in your compute function.
for (i = 1; i <=6; i++)
{
for (j = 1; j <= 6; j++)
{
N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1]
+ Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1]
+Z[i][j+1]+Z[i+1][j+1];
}
}You can use the recursive algorithm of the stack or compute the boundary element separately.
xgzsChris
-
When calculating the N array, the element of the boundary is overstepped by the Z array access as calculated by your algorithm in your compute function.
for (i = 1; i <=6; i++)
{
for (j = 1; j <= 6; j++)
{
N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1]
+ Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1]
+Z[i][j+1]+Z[i+1][j+1];
}
}You can use the recursive algorithm of the stack or compute the boundary element separately.
xgzsChris
元昊 潘 wrote:
When calculating the N array, the element of the boundary is overstepped by the Z array access as calculated by your algorithm in your compute function.
for (i = 1; i <=6; i++)
{
for (j = 1; j <= 6; j++)
{
N[i][j]=Z[i-1][j-1]+Z[i][j-1]+Z[i+1][j-1]
+ Z[i-1][j] +Z[i+1][j]+ Z[i-1][j+1]
+Z[i][j+1]+Z[i+1][j+1];
}
}Yes. And the same is for N array itself! The OP tries to set the element with the index 6 while the "allowed" indexes are 0 to 5!