细胞自动机故障(康威的生命游戏)

时间:2012-11-15 12:50:35

标签: arrays simulate

我尝试使用基本C ++模拟生命游戏。在5x5网格中,可以测试的最佳物种是振荡器。所以我前进并做到了,但我没有得到所需的输出。这是我的代码。请建议更正。

#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 个答案:

答案 0 :(得分:-1)

幽灵,

http://pastebin.com/tTnLifaR 是什么将修复你的前卫!

尝试一下然后告诉我!