Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Conways Game of life game in C

Conways Game of life game in C

Scheduled Pinned Locked Moved C / C++ / MFC
cssdatabasegame-devdata-structuresannouncement
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Abhinav9993
    wrote on last edited by
    #1

    ============================================================================ 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;

    L 元 2 Replies Last reply
    0
    • A Abhinav9993

      ============================================================================ 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;

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      What is the question?

      1 Reply Last reply
      0
      • A Abhinav9993

        ============================================================================ 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;

        元 Offline
        元 Offline
        元昊 潘
        wrote on last edited by
        #3

        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

        V 1 Reply Last reply
        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

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          元昊 潘 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!

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups