I'm trying to create a program that flood-fills a 2d array of cells that can either be barriers or open.
I have a class called "cell" and each cell has a vector of pointers to its neighbors. When the member function "fill()" is called it fills the cell as long as it's not already filled and not a barrier and then calls fill for all its neighbors. the code looks like this:
void cell::fill() {
if (barrier == false && filled == false) {
filled = true;
cout << "filling" << endl;
for (int i = 0; i < neighbors.size(); i++) {
neighbors[i] -> fill();
}
}
}
When I call fill for a certain cell it fills that cell but doesn't fill any of its neighbors. It does however output "filling" for each of its open neighbors but doesn't seem to actually set filled to true for each of those neighbors. This is my first time using pointers and I don't have much experience with classes so am I missing something obvious that's causing this behavior? My only thought is that this sort of recursive calling of member functions doesn't work the way I expect it to.
EDIT: This is what the output looks like. black rectangles being barriers, spaces being empty cells and asterisks being filled cells.
filling
filling
██ █
██ ███
██
█ █*█ █
█
██ █ █
█ █ █