1

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.

Zoltán Schmidt
  • 1,286
  • 2
  • 28
  • 48

2 Answers2

0

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!

Florian Rhiem
  • 1,758
  • 1
  • 14
  • 23
  • Oh yes, I did both of them and there is happened thatnothing happens! I mean, the cells are just staying as they were put. – Zoltán Schmidt May 20 '13 at 09:26
  • Your equality function takes one tile for each parameter, not an array. GuyRT's replay is just like first part of mine and your comment there makes me think you misunderstood the both of us. Please making the changed I suggested and see if anything changes. – Florian Rhiem May 20 '13 at 09:30
  • I got it already; equality gets only ONE structure. Sorry, I act a bit quickly because I'm struggling with this problem for some days. So the matter is that check is OK at the moment (theoretically) but nothing happens with the cells. – Zoltán Schmidt May 20 '13 at 09:37
  • See my edit, the way you did it, each cell has the neighbors (i-1,j-1), (i,j-1) and (i-1, j), so counter can be at most 3. Because of that, no new living cells will be spawned. – Florian Rhiem May 20 '13 at 09:44
  • Indeed...I've always forgot similar things. Anyway, it seems to work. Thank you very much! – Zoltán Schmidt May 20 '13 at 09:54
0

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)
GuyRT
  • 2,919
  • 13
  • 8