Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream.h>
- #include<conio.h>
- #include<stdio.h>
- #define DEAD 0
- #define ALIVE 1
- void matdis(int a1[5][5]) // Outputs the array to screen!
- {
- for(int u = 0; u<5; u++)
- {
- for(int g = 0; g<5; g++)
- {
- cout<<a1[u][g];
- }
- cout<<endl;
- }
- }
- int nsum(int a1[5][5], int i, int j) // Calculates sum of diagonal elements
- {
- 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];
- }
- void rules(int a1[5][5], int a2[5][5]) // Run 1 round of Conway's rules!
- {
- for(int i = 0; i<5; i++)
- {
- for(int j = 0; j<5; j++)
- {
- if(a1[i][j]== ALIVE)
- {
- if(nsum(a1, i, j)==1)
- {
- a2[i][j] = 0;
- }
- else if (nsum(a1,i,j)==2 || nsum(a1,i,j)==3)
- {
- a2[i][j] = 1;
- }
- else if(nsum(a1,i,j)>3)
- {
- a2[i][j] = 0;
- }
- }
- else if(a1[i][j]==DEAD)
- {
- if(nsum(a1,i,j)==3)
- {
- a2[i][j]==1;
- }
- }
- }
- }
- }
- int main() // All the rest
- {
- int t = 0; int a1[5][5]; int a2[5][5];
- cout<<" \t\t Welcome to the Game of Life "<<endl;
- cout<<" \t Provide the automaton with initial conditions,(1 or 0) and see it proceed"<<endl;
- for(int a = 0; a<5; a++)
- {
- for(int b = 0; b<5; b++)
- {
- a2[a][b]=DEAD;
- }
- }
- for(int i = 0; i<5; i++)
- {
- for(int j = 0; j<5; j++)
- {
- cin>>a1[i][j];
- }
- }
- clrscr();
- cout << "Here's what you entered!\n";
- matdis(a1);
- cout<<endl;
- getch();
- clrscr();
- int stopcon = 1;
- while(stopcon)
- {
- clrscr();
- for(int y = 0; y<5; y++)
- {
- for(int z = 0; z<5; z++)
- {
- a3[y][z] = '\0';
- }
- }
- if(t%2==0)
- {
- rules(a1,a2);
- matdis(a2);
- }
- else
- {
- rules(a2,a1);
- matdis(a1);
- }
- cout<<" Go for another generation? Y : 1 . N : 0 "<<endl;
- t++;
- cin>>stopcon;
- }
- getch(); return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement