Advertisement
Guest User

conway_fix

a guest
Nov 15th, 2012
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.14 KB | None | 0 0
  1. #include<iostream.h>
  2. #include<conio.h>
  3. #include<stdio.h>
  4.  
  5. #define DEAD 0
  6. #define ALIVE 1
  7.  
  8. void matdis(int a1[5][5])      // Outputs the array to screen!
  9. {
  10.     for(int u = 0; u<5; u++)
  11. {
  12.     for(int g = 0; g<5; g++)
  13.     {
  14.         cout<<a1[u][g];
  15.     }
  16.     cout<<endl;
  17. }
  18. }
  19.  
  20.  
  21. int nsum(int a1[5][5], int i, int j)    // Calculates sum of diagonal elements
  22. {
  23.     return a1[i-1][j-1] + a1[i][j-1] + a1[i+1][j-1] + a1[i+1][j] + a1[i+1][j+1] +     a1[i][j+1] + a1[i-1][j+1] + a1[i-1][j];
  24. }
  25.  
  26. void rules(int a1[5][5], int a2[5][5])  // Run 1 round of Conway's rules!
  27. {
  28. for(int i = 0; i<5; i++)
  29. {
  30.     for(int j = 0; j<5; j++)
  31.     {
  32.         if(a1[i][j]== ALIVE)
  33.         {
  34.                 if(nsum(a1, i, j)==1)
  35.                 {
  36.                     a2[i][j] = 0;
  37.                 }
  38.                 else if (nsum(a1,i,j)==2 || nsum(a1,i,j)==3)
  39.                 {
  40.                     a2[i][j] = 1;
  41.                 }
  42.                 else if(nsum(a1,i,j)>3)
  43.                 {
  44.                     a2[i][j] = 0;
  45.                 }
  46.         }
  47.         else if(a1[i][j]==DEAD)
  48.         {
  49.             if(nsum(a1,i,j)==3)
  50.             {
  51.                 a2[i][j]==1;
  52.             }
  53.         }
  54.     }
  55. }
  56. }
  57.  
  58. int main() // All the rest
  59. {
  60. int t = 0; int a1[5][5]; int a2[5][5];
  61.  
  62. cout<<" \t\t Welcome to the Game of Life "<<endl;
  63. cout<<" \t Provide the automaton with initial conditions,(1 or 0) and see it proceed"<<endl;
  64.  
  65. for(int a = 0; a<5; a++)
  66. {
  67.     for(int b = 0; b<5; b++)
  68.     {
  69.         a2[a][b]=DEAD;
  70.     }
  71. }
  72. for(int i = 0; i<5; i++)
  73. {
  74.     for(int j = 0; j<5; j++)
  75.     {
  76.         cin>>a1[i][j];
  77.     }
  78. }
  79. clrscr();
  80. cout << "Here's what you entered!\n";
  81. matdis(a1);
  82. cout<<endl;
  83. getch();
  84. clrscr();
  85.  
  86. int stopcon = 1;
  87. while(stopcon)
  88. {  
  89.     clrscr();
  90.     for(int y = 0; y<5; y++)
  91.     {
  92.         for(int z = 0; z<5; z++)
  93.         {
  94.             a3[y][z] = '\0';
  95.         }
  96.     }
  97.     if(t%2==0)
  98.     {
  99.         rules(a1,a2);
  100.         matdis(a2);
  101.     }
  102.     else
  103.     {
  104.         rules(a2,a1);
  105.         matdis(a1);
  106.     }
  107.     cout<<" Go for another generation? Y : 1 . N : 0 "<<endl;
  108.     t++;
  109.     cin>>stopcon;
  110.  
  111. }
  112. getch(); return 0;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement