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?

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48
  • Please clarify what '*the whole thing doesn't work*' means. – Marc Claesen May 20 '13 at 12:11
  • **How** doesn't it work? Does it crash? Compilation errors? Have you tried stepping through the code in a debugger? – Some programmer dude May 20 '13 at 12:12
  • There is no debugging error in the program. 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. – Zoltán Schmidt May 20 '13 at 12:14
  • 1
    It seems to me that both function, Test and Conway, do the same thing, so, only the last stay visible. I suggest you to invert the calls and, instead of calling `Test(fielda,fieldb); Conway(fielda,fieldb);`, call `Conway(fielda,fieldb); Test(fielda,fieldb);` and see what happens – Amadeus May 20 '13 at 12:28
  • In that case, the red one works. Of course both do ALMOST (just almost) the same, because both of them are cellular automatons. What if I want them to work at the same time? – Zoltán Schmidt May 20 '13 at 12:33

1 Answers1

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.

Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
  • But later, when I want to use more colours (such as orange, purple or anything for cells, I won't be able to it, without reverse. Or am I wrong? – Zoltán Schmidt May 20 '13 at 14:05
  • No, but consider this: How would you represent a cell that is alive according to `Test`, but dead according to `Conway`? This information is now directly encoded in the colour channels, of which there are only three. If you want to use additional or non-primary colours, you have to decouple the alive/dead information from the colours used to represent them. – Bart van Ingen Schenau May 21 '13 at 12:47