-1

I am working on a Minesweeper for a school project. I am currently working on opening up adjacent squares if the number of mines in adjacent squares is 0. I have written a function to do so.

{
    void Square::openAdjacent(int a, int b)
    {
        for (int i=(a-1); i<=(a+1); i++)
             {
               for (int j= (b-1); j<=(b+1); j++)
                 {
                    if ((i==a && j==b) || (i<=0) || (j<=0) || (i>=board_->size()) || (j>=board_->size())){}
                    else if (not board_->at(j).at(i).opened_ && count_==0 && (opened_) && (not board_->at(j).at(i).hasMine_)){
                             board_->at(j).at(i).opened_=true;
                             if (board_->at(j).at(i).count_==0){
                                 openAdjacent(i, j);
                             }
                          }
                 }
             }
    }
}

As you can see I have called the function openAdjacent within the same function. I have read around and learnt that this isn't possible in C++. I would like to know if it's possible to work around the problem and how I should do so.

Ray
  • 3,864
  • 7
  • 24
  • 36
  • 9
    A function calling itself is called *recursion* and it is allowed in `C++`. What's wrong here? – Fureeish Sep 16 '18 at 17:31
  • 2
    ***I have read around and learnt that this isn't possible in C++*** Whatever you read is wrong. With this said, you have to make sure that your recursion is not infinite. – drescherjm Sep 16 '18 at 17:35
  • [OT]: `if (cond1){/*Empty*/} else if (cond2) { /*..*/}` can be replaced by `if ((not cond1) && cond2) { /*..*/}`. – Jarod42 Sep 16 '18 at 17:44
  • 2
    The only restriction is that you can't call `main` inside `main`. – Thomas Matthews Sep 16 '18 at 18:30

1 Answers1

0

I have read around and learnt that this isn't possible in C++. I would like to know if it's possible to work around the problem and how I should do so.

No, it's not possible to work around the problem because the problem doesn't exist. You can call a function within the same function. Or even indirectly from a function called from it. There is really no restriction here. You just have to take care to end the infinite recursion.

bolov
  • 72,283
  • 15
  • 145
  • 224