c++ - strange problem with polymorphism - cellular automaton
- by Green
Hello, I am making "game of life" implementation that, when cell has:
two live neighbours I make object of class CCellB
two live neighbours I make object of class CCellA
when has 3 or <2 I make object of class CCellX (dead)
Class CCell is base of CCellA, CCellB, CCellX
My problem is:
That works fine, until I change new CCell(); to new CCellB();:
CCell ***temp = allocateArray();
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
{
c = arr[i][j]->countAliveNeighbor();
if(c == 3)
{
temp[i][j] = new CCellA();
temp[i][j]->alive = 1;
}else if(c == 2)
{
temp[i][j] = new CCell(); // HERE, WHEN I CHANGE IT TO CCELLB IT DON'T WORKS
temp[i][j]->alive = tab[i][j]->alive;
}else if((c >= 4)||(c < 2))
{
temp[i][j] = new CCellX();
}
}
}
Then it seems don't work properly... Seems like neighbours are not count properly, Cell with 4,5 neighbours still alive
alive var is member of base class - CCell, all is public, please help!