1

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
 ██   █
██  ███
    ██
█ █*█ █
   █
 ██ █ █
█   █ █
j.dow
  • 81
  • 4
  • Have you tried to [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) your program? For example using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through your code line by line while monitoring variables and threir values, to see what really happens? – Some programmer dude May 28 '23 at 08:22
  • 3
    The code you show is not complete enough to see what the problem is. – Pepijn Kramer May 28 '23 at 08:23
  • Things to look out for when debugging: Is `barrier` and `filled` set up correctly? Is `neighbors` set up correctly? – Some programmer dude May 28 '23 at 08:24
  • provided snippet is the most innocent and straightforward part of implementation. Assuming undocumented wrong behaviour instead of inderifying wrong behaviour rarely works in debugging. Your input of `cell::fill()` must be broke, either state of `cell` instances or of `neighbors`. We can't even trust the output fully without seeing how it is implemented. – Swift - Friday Pie May 28 '23 at 08:33
  • 2
    Thanks, @Someprogrammerdude. It was a problem of me adding pointers to copies of the neighboring cells rather than the cells themselves. Just had to change a function to pass by reference instead of pass by value. New to coding and it was my first time using the debugger in visual studio. – j.dow May 28 '23 at 09:27
  • 1
    @j.dow That's great! Just a heads-up for the next time you ask a question. Please include a [mre] and the expected output of your program. I'm sure someone would have spotted that mistake in a minute. – Ted Lyngmo May 28 '23 at 09:44
  • Using pointers among elements of a **regular** grid is drastically inefficient, although that might not matter for your problem size. – Davis Herring May 28 '23 at 12:29

0 Answers0