c++ - Cellular automaton doesn't make new cells -
i'm trying make program able create every kind of cellular automatons, such conway's game of life , else too.
the graphic implementation works already, wouldn't waste time (especially uses allegro libraries), functions counts cells, doesn't work properly.
that's have @ moment. (the code in order, break commentary make clear you)
pre-definitions:
#define fldwidth 110 #define fldheight 140 a structure graphics:
typedef struct tiles { unsigned char red, green, blue; }tiles; two predefined structures: rgb code of alive , dead test cell.
const tiles test_alive = {200,0,0}; const tiles test_dead = {100,0,0}; a function checks color equality of structure variable , 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. gets 2 arrays of structures (first 1 current round, second 1 counting happens; in round loop, after counting, b array copied array); when started, following steps every structures: counts, how many living cells has in neighborhood (if living cell, starts -1 avoid counting neighbours, otherwise start 0 regularly), if not living test cell (but else) , has 5 neighbours, becomes living test cell; if living test cell , has 2 neighbours, becomes dead cell.
void test(tiles arra[fldwidth][fldheight], tiles arrb[fldwidth][fldheight]) { int a,b,i,j,counter; (j=1;j<fldheight-1;j++) { (i=1;i<fldwidth-1;i++) { if (equality(arra[i][j], test_alive) == true) { counter = -1; } else { counter = 0; } (b=j-1;b<j+1;b++) { (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 when counting begins, every living cell becomes dead in first round , disappear, without becoming dead cell (which darker red colour obviously), , happens every "counter == xy" check.
i've got tips, have no idea, why doesn't work. have logic failure? because can't see mistake, though there.
edit:
arra[fldwidth][fldheight] is replaced by
arra[i][j] and
arrb[i][j] = arra[i][j];
is added. stays put.
why access arra[fldwidth][fldheight] equality checks? outside of array, 1 element behind last element in array! want access arra[i][j].
and unless arrb starts copy of arra, want add arrb[i][j] = arra[i][j]; in front of 2 equality checks. way if cell doesn't meet of 2 state change rules, keep current state.
edit:
you need let loop run between i-1 , i+1, should be: for (a = i-1; <= i+1; a++), same b!
Comments
Post a Comment