Skip to main content
Replaced bullets with code formatting since they were kind-of distracting
Source Link

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1
1 1 1 1 1 1 
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from wouldn't have been enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from wouldn't have been enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

Imagine the following setup:

1 1 1 1 1 1 
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 0 0 0 0 1
1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from wouldn't have been enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

minor grammar fix to make things clearer
Source Link
TokPhobia
  • 441
  • 4
  • 4

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from isn'twouldn't have been enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from isn't enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from wouldn't have been enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.

Source Link
TokPhobia
  • 441
  • 4
  • 4

Imagine the following setup:

  • 1 1 1 1 1 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 0 0 0 0 1
  • 1 1 1 1 1 1

As a side note, I refer to squares in the matrix like this: (row, column). I've represented mines with "1" and empty spaces with "0". Assume the user clicks on the empty space at (2, 2) (the corner at the top-left is (0, 0)).

This is what would happen:

  • the square at (2, 3) is empty and has no adjacent mines. Let's call our function for those coordinates
  • in the new function call we notice that (3, 3) is empty and has no adjacent mines, let's call the function for that space as well
  • (3, 2) is a valid place too, let's call again
  • in the fourth call we'll notice that (2, 2) is empty and has no nearby mines, so we're back at square one, thus creating an endless loop

Any adjacent squares that each do not have adjacent mines will create an endless loop once your recursive function is called for either of them. Simply putting in a condition to not call the function again for the square you just came from isn't enough, as can be seen in the example above.

One solution is to create another matrix that can show what squares have already been visited. If the current square has already been visited, return from the function immediately. Otherwise, mark is as a visited square and carry on with your usual code.