Classic Minesweeper puzzle. Clear the board without clicking on hidden mines. Multiple difficulties and mobile friendly controls.
When you click on a cell in Minesweeper that has no adjacent mines (a "zero" cell), the game magically opens up a massive cavern of empty space all at once. How does the computer know exactly which cells to open?
The game uses a classic Computer Science algorithm called Flood Fill (the exact same algorithm used by the "Paint Bucket" tool in Photoshop).
When you click a zero cell, a function runs with the following logic:
This is called Recursion (a function that calls itself). The function will spider outwards in every direction until it hits cells that are adjacent to mines (numbers greater than 0), creating the natural boundaries of the cleared area.
Because recursion adds a new frame to the Call Stack for every iteration, clearing a massive 1000x1000 Minesweeper board using recursion could cause a "Stack Overflow" crash. In professional massive-scale implementations, developers rewrite the recursive flood fill using a Queue or Stack data structure (an Iterative approach) to avoid crashing the browser.
What is it called when a function calls itself from within its own code?