1

I'm working on a cellular automaton where changes happen in every rounds. Obviously, I made a loop for it - basically it works, fortunately, but if I want to add another type of cells to the map, the whole thing doesn't work! I mean, one type of cells works, but the other doesn't do anything: The game begins and e.g. in this example, the Conway-automaton starts growing, but the red test-cells are just staying without any changes.

These are the two functions (with predefined things):

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

and this is the loop itself:

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

}

Can you tell me what is wrong with it? Or maybe the problem is not in the loop?

4

1 回答 1

0

Your problem is that the second function you call reverses all the changes made by the first function. That is why you see only one working.

The best way to get both working it to let each function operate only on one colour channel. Test uses only the red colour channel (both for reading and changing), while Conway uses only the green colour channel. Be prepared to see cells with different colours: these were affected by both functions.

于 2013-05-20T13:52:33.500 回答