被调用的函数清除了前一个的变化

时间:2013-05-20 12:08:26

标签: c++ function call allegro cellular-automata

我正在研究细胞自动机,每一轮都会发生变化。显然,我为它做了一个循环 - 幸运的是,基本上它是有效的,但是如果我想在地图中添加另一种类型的单元格,那么整个过程都不​​起作用!我的意思是,一种类型的细胞起作用,但另一种不起作用:游戏开始,例如在这个例子中,Conway-automaton开始增长,但红色测试单元只是保持不变。

这两个函数(带有预定义的东西):

#define fldwidth 110 
#define fldheight 140

//struktúra, aztán a sejtek definíciója

typedef struct tiles
{
    unsigned char red, green, blue;
}tiles;

const tiles TEST_ALIVE = {255,0,0};
const tiles TEST_DEAD = {50,0,0};
const tiles CONWAY_ALIVE = {0,255,0};
const tiles CONWAY_DEAD = {0,50,0};

//Maes módszere a struktúrák egyenlőségének vizsgálatára
bool equality(tiles* a, const tiles* b) 
{
    if (a->red == b->red && a->green == b->green && a->blue == b->blue)
    {
        return true;
    } else {
        return false;
    }
}



//sejttípus 1.: tesztsejt: minden magányos vagy túlbuzgó sejt meghal
void Test(tiles arra[fldwidth][fldheight], tiles arrb[fldwidth][fldheight])
{
    int a,b,i,j,counter;

    for (j=1;j<fldheight-1;j++)
    {
        for (i=1;i<fldwidth-1;i++)
        {
            if (equality(&arra[i][j], &TEST_ALIVE) == true)
            {
            counter = -1;
            } else {
                counter = 0;
            }
            for (b=j-1;b<=j+1;b++)
            {
                for (a=i-1;a<=i+1;a++)
                {
                    if (equality(&arra[a][b], &TEST_ALIVE) == true)
                    {
                        counter+=1;
                    }
                }
            }
            arrb[i][j] = arra[i][j];
            //itt a sejtek szabályai jönnek; mindig a születést tesszük előre, utána a halált!
            if (equality(&arra[i][j], &TEST_ALIVE) == false && counter >= 2)
            {
                arrb[i][j] = TEST_ALIVE;
            }

            if (equality(&arra[i][j], &TEST_ALIVE) == true && (counter == 0 || counter > 6))
            {
                arrb[i][j] = TEST_DEAD;
            }
        }
    }

}

//sejttípus 2.: Conway életjátéka
void Conway(tiles arra[fldwidth][fldheight], tiles arrb[fldwidth][fldheight])
{
    int a,b,i,j,counter;

    for (j=1;j<fldheight-1;j++)
    {
        for (i=1;i<fldwidth-1;i++)
        {
            if (equality(&arra[i][j], &CONWAY_ALIVE) == true)
            {
            counter = -1;
            } else {
                counter = 0;
            }
            for (b=j-1;b<=j+1;b++)
            {
                for (a=i-1;a<=i+1;a++)
                {
                    if (equality(&arra[a][b], &CONWAY_ALIVE) == true)
                    {
                        counter+=1;
                    }
                }
            }
            arrb[i][j] = arra[i][j];
            //itt a sejtek szabályai jönnek; mindig a születést tesszük előre, utána a halált!
            if (equality(&arra[i][j], &CONWAY_ALIVE) == false && counter == 3)
            {
                arrb[i][j] = CONWAY_ALIVE;
            }

            if (equality(&arra[i][j], &CONWAY_ALIVE) == true && (counter != 2 && counter != 3))
            {
                arrb[i][j] = CONWAY_DEAD;
            }
        }
    }
}

这就是循环本身:

while(!end)
{
al_wait_for_event_timed(event_queue,&asd,0.001); //várakozás

if(asd.type == ALLEGRO_EVENT_KEY_DOWN)
{
    if(asd.keyboard.keycode == ALLEGRO_KEY_ENTER)
    {
    Test(fielda,fieldb);
    Conway(fielda,fieldb);
    end = false;
    round++;
    for (j = 0; j < fldheight; j++)
        {
            for (i = 0; i < fldwidth; i++)
            {
                fielda[i][j] = fieldb[i][j];
            }
        }
    }
}

for (j = 0; j < fldheight; j++)
{
    for (i = 0; i < fldwidth; i++)
    {
        al_draw_filled_rectangle(20 + (4*i), 20 + (4*j), 24 + (4*i), 24 + (4*j), al_map_rgb(fielda[i][j].red, fielda[i][j].green, fielda[i][j].blue));
    }
}

}
你能告诉我它有什么问题吗?或者问题可能不在循环中?

1 个答案:

答案 0 :(得分:0)

您的问题是您调用的第二个函数会反转第一个函数所做的所有更改。这就是为什么你只看到一个工作。

让两者兼顾的最佳方法是让每个功能仅在一个颜色通道上运行。 Test仅使用 红色通道(用于阅读和更改),而Conway仅使用 绿色通道。准备好看到不同颜色的细胞:这些都受到两种功能的影响。