2012-11-15 4 views
1

저는 C++를 사용하여 Game of Life를 시뮬레이션하려고했습니다. 5x5 그리드에서 테스트 할 수있는 가장 좋은 종은 오실레이터입니다. 그래서 저는 앞으로 나아갔습니다. 그러나 나는 필요한 결과물을 얻지 못했습니다. 여기 내 코드가있다. 수정을 제안하십시오.셀룰러 오토 마톤 기능 장애 (Conway 's Life of Game)

#include<iostream.h> 
#include<conio.h> 
#include<stdio.h> 

void matdis(char a1[5][5])  // It displays char 
{ 
    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 converts 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]; 
    } 
} 
cout<<"This is what you entered"<<endl; 
transcriptor(a1,a3); matdis(a3); 
cout<<endl; 
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; 
    t++; 
    cin>>stopcon; 

} 
getch(); return 0; 
} 

답변

-1

레이스,

http://pastebin.com/tTnLifaR 은 음식물가 해결됩니다 것입니다!

시험해보고 알려주세요.

+0

여전히 작동하지 않습니다. –

관련 문제