Check your clear method for HashTable.
You are calling data[wrappedI].clear()
I think this should be:
1 2 3 4 5 6 7 8 9
|
template<class DataType>
void HashTable<DataType>::clear(){
for(int i=0;i<HASHTABLE_CAPACITY;i++){
data[i].clear();
}
current = -1;
size = 0;
wrappedI = 0;
}
|
I think this was causing most of your issues.
Also review your HashTable<DataType>::setWrappedIndex method.
You are checking if the index dI is strictly greater than the [HASHTABLE_CAPACITY] - this then causes no action to be taken when dI is equal to [HASHTABLE_CAPACITY] which causes an indexing error when referencing the internal array of lists. Arrays are zero-based referenced, thus index equal to length of array will be one unit out of bounds.
You could try changing method to:
1 2 3 4 5 6 7 8 9
|
template<class DataType>
void HashTable<DataType>::setWrappedIndex(int dI){
while (dI < 0)
{
dI += HASHTABLE_CAPACITY;
}
wrappedI = dI % HASHTABLE_CAPACITY;
}
|
You should also review your logic for neighborCount when the cell you are counting neighbours for lies on a boundary (ie, MINCOL, MAXCOL, MINROW, MAXROW). You are correct to start scanning for neigbours from (row -1, col - 1), however, when row - 1 or col - 1, (or actually your iterating indexes) falls out of boundary range, then your model isn't catering for logically wrapping your neighbours on your screen dimensions.
It can be appreaciated that your model caters for "an infinite screen" by allowing for negative row and col indexing, but unless you make your view model a pan-viewer there isn't much point in proceeding in such a fashion. (And besides, Game of Life is emulated on life existing within finite space as is natural even if we consider expanding to the rest of the universe :))
With this said, you may want to consider changing your (MINCOL, MAXCOL, MINROW, MAXROW) concept to a ROWCNT & COLCNT concept or will need to consider such concept as ROWCNT = MAXROW-MINROW and likewise for COLCNT to impose row, col wrapping w.r.t. screen dims when counting neighbours.
Maybe try changing it to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
const int ROWCNT = MAXROW - MINROW + 1;
const int COLCNT = MAXCOL - MINCOL + 1;
int neighborCount(int row, int col)
{
cell temp;
int count = 0;
for (int r=row-1; r<=row+1; r++)
for (int c=col-1; c<=col+1; c++)
{
temp.row = r;
temp.col = c;
while (temp.row < 0) temp.row += ROWCNT;
while (temp.col < 0) temp.col += COLCNT;
temp.row %= ROWCNT;
temp.col %= COLCNT;
if (temp.row != row || temp.col != col)
if (grid.retrieve(temp))
++count;
}
return count;
}
|
Consequently you should also range check values when intializing or force them into screen dimension range.
You may even want to consider having an init_load function that loads live cell points from a file - this will eliminate the need to enter these in by hand each time you run your program.