首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调用的函数清除对前一个函数的更改

调用的函数清除对前一个函数的更改
EN

Stack Overflow用户
提问于 2013-05-20 20:08:26
回答 1查看 87关注 0票数 1

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

以下是两个函数(具有预定义的内容):

代码语言:javascript
复制
#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;
            }
        }
    }
}

这就是循环本身:

代码语言:javascript
复制
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));
    }
}

}

你能告诉我它出了什么问题吗?或者也许问题不在循环中?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-05-20 21:52:34

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

让两者工作的最好方法是让每个功能只在一个颜色通道上运行。Test仅使用红色通道(用于读取和更改),而Conway仅使用绿色通道。准备好看到不同颜色的单元格:这两种功能都会影响这些单元格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16649185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档