c++ - 调用的函数清除前一个函数的更改

标签 c++ function call allegro cellular-automata

我正在研究一个元胞自动机,其中每一轮都会发生变化。显然,我为它做了一个循环 - 幸运的是,基本上它可以工作,但是如果我想向 map 添加另一种类型的单元格,整个事情就不起作用了!我的意思是,一种类型的细胞起作用,但另一种细胞什么都不做:游戏开始了,例如在此示例中,康威自动机开始增长,但红色测试单元保持不变。

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

#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));
    }
}

}

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

最佳答案

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

最好的方法是让每个函数只在一个颜色 channel 上运行。 Test 使用红色 channel (用于读取和更改),而 Conway 使用绿色 channel 。准备好看到不同颜色的细胞:这些细胞受到两种功能的影响。

关于c++ - 调用的函数清除前一个函数的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16649185/

相关文章:

javascript - 计算服务调用收集响应所需的时间

c++ - 线程没有被分离

c++ - 使用 QFile::remove() 从 C++ 代码中删除文件是否会留​​下 .nfsxxxxx 文件?

c++ - 如何最有效地迭代私有(private)成员 std::vector?

c++ - Windows 上来自 std::error_code 的 "unknown error"

assembly - CALL 指令是否总是将 EIP 指向的地址压入堆栈?

javascript - jQuery - 在函数调用中使用当前对象

c - 了解由于 getchar 而导致的 c 循环

javascript,这个函数怎么可能返回一个空数组?

python - 从 python 调用外部程序并获取其输出