https://github.com/sleekpanther/minesweeper-algorithm
Simple iterative solution to counts mines in Minesweeper
https://github.com/sleekpanther/minesweeper-algorithm
Last synced: 7 months ago
JSON representation
Simple iterative solution to counts mines in Minesweeper
- Host: GitHub
- URL: https://github.com/sleekpanther/minesweeper-algorithm
- Owner: SleekPanther
- Created: 2018-04-19T11:56:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-05-27T01:07:25.000Z (over 7 years ago)
- Last Synced: 2025-01-15T13:08:07.126Z (9 months ago)
- Language: Python
- Homepage:
- Size: 5.86 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Minesweeper Algorithm
Simple iterative solution to counts mines in [Minesweeper](https://en.wikipedia.org/wiki/Minesweeper_(video_game))## Usage
- Provide a 2D list of a rectangular board with **`-1` representing a mine** and `0` in all other spaces
- call `displayMines(board)` passing in the board and the number of mines adjacent to each square will be printed preserving mines as `-1`
- Works for non-square dimensions## Algorithm
- Copies the board to a new board where the counts will reside
- Loops over all squares in the input board
- If a mine is encountered, it checks the 8 surrounding squares & increments the count in each square that isn't a mine
- Surrounding squares are checked by a nested for loop, `xTemp` coordinates go from `x-1` to `x+1` & `yTemp` coordinates go from `y-1` to `y+1`
- All coordinates are checked to make sure they don't go out of bounds of the edges of the board
- The current square is skipped by also checking if the value at `board[ytemp]pxTemp]` is a mine before incrementing
## Runtime
O(mn) for looping over the entire board
O(8) for checking surrounding squares
O(8*mn) combined
⇒ **O(mn)**