Question

I'm trying to make a program that is able to create every kind of cellular automatons, such as Conway's game of life and everything else too.

The graphic implementation works perfectly already, so I wouldn't waste your time with that (especially that it uses Allegro libraries), but the functions that counts the cells, doesn't work properly.

That's what I have at the moment. (the code is in order, I just break it with commentary to make everything clear for you)

Pre-definitions:

#define fldwidth 110 
#define fldheight 140

A structure for graphics:

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

Two predefined structures: the RGB code of an alive and a dead test cell.

const tiles TEST_ALIVE = {200,0,0};
const tiles TEST_DEAD = {100,0,0};

A function that checks the color equality of a structure variable and a constant structure.

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

The main function. It gets two arrays of structures (first one is the current round, second one is where counting happens; in round loop, after the counting, b array will be copied into a array); when started, it does the following steps for every structures: counts, how many living cells it has in its neighborhood (if its a living cell, it starts from -1 to avoid counting itself as neighbours, otherwise it start from 0 regularly), then if itself is NOT a living test cell (but anything else) and has 5 neighbours, it becomes a living test cell; if itself is a living test cell and has 2 neighbours, it becomes a dead cell.

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];
            if (equality(arra[i][j], TEST_ALIVE) == false && counter == 5)
            {
                arrb[i][j] = TEST_ALIVE;
            }

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

The problem is that when the counting begins, every living cell becomes dead immediately in the first round and sometimes they just disappear, even without becoming dead cell (which is a darker red colour obviously), and it happens for almost every "counter == XY" check.

I've already got some tips, but I have no idea, why it doesn't work. Does it have logic failure? Because I can't see the mistake, even though it is there.

EDIT:

arra[fldwidth][fldheight]

is replaced by

arra[i][j]

and

arrb[i][j] = arra[i][j];

is added. Now everything stays as they were put.

Was it helpful?

Solution

Why do you access arra[fldwidth][fldheight] for the equality checks? This is outside of the array, one element behind the last element in the array! What you want to access is arra[i][j].

And unless arrb starts as a copy of arra, you probably want to add arrb[i][j] = arra[i][j]; in front of the two equality checks. That way if a cell doesn't meet any of the two state change rules, it will keep its current state.

Edit:

You also need to let the loop run between i-1 and i+1, so it should be: for (a = i-1; a <= i+1; a++), same for b!

OTHER TIPS

I think your bug is in the line:

if (equality(arra[fldwidth][fldheight], TEST_ALIVE) == false && counter == 5)

This should be:

if (equality(arra[i][j], TEST_ALIVE) == false && counter == 5)

and similarly for the line:

if (equality(arra[fldwidth][fldheight], TEST_ALIVE) == true && counter == 2)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top