| TusharRakheja (1) | |
|
I came up with this code to simulate Conway's Game of Life. But somehow the output is unexpected. Please suggest corrections. #include<iostream.h> #include<conio.h> #include<stdio.h> void matdis(char a1[5][5]) // It displays char array { for(int u = 0; u<5; u++) { for(int g = 0; g<5; g++) { cout<<a1[u][g]<<" "; } cout<<endl; } } void transcriptor(int a1[5][5], char a2[5][5]) // It translates int into char for display { for(int i = 0; i<5; i++) { for(int j = 0; j<5; j++) { if(a1[i][j]==1) { a2[i][j] = 'x'; } else if(a1[i][j]==0) { a2[i][j] = '\0'; } } } } int nsum(int a1[5][5], int i, int j) // Calculates sum of diagonal elements { int sum = 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]; return sum; } void rules(int a1[5][5], int a2[5][5]) // Array1 to Array2 Generation simulator { for(int i = 0; i<5; i++) { for(int j = 0; j<5; j++) { if(a1[i][j]==1) { 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]==0) { 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]; char a3[5][5]; cout<<" \t\t Welcome to the Game of Life "<<endl; cout<<" \t Provide the automaton with initial conditions, and see it proceed"<<endl; for(int a = 0; a<5; a++) { for(int b = 0; b<5; b++) { a2[a][b]='\0'; } } for(int i = 0; i<5; i++) { for(int j = 0; j<5; j++) { cin>>a1[i][j]; } } int stopcon = 1; while(stopcon) { 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); transcriptor(a2,a3); matdis(a3); } else { rules(a2,a1); transcriptor(a1,a3); matdis(a3); } cout<<" Go for another generation? Y : 1 . N : 0 "<<endl; cin>>stopcon; t++; } getch(); return 0; } | |
|
Last edited on
|
|
| MikeyBoy (175) | |
| Please use the code tags for the code parts of your post. It will make it much easier to read. | |
|
|
|
| jimobama (36) | |
|
try and comment your codes for eay understand | |
|
|
|
| Kart (45) | |||
|
Try running this and let me know if this is what you expect. I changed the getch to cin.ignore(); and removed the conio file.
| |||
|
|
|||