This is a very simple & inefficient implementation of a minesweeper solver coded in python.
Install this application using pip directly referencing the dev branch from github:
$ pip install git+https://github.com/rhdzmota/minesweeper.git@dev#subdirectory=minesweeper&egg=minesweeper
Alteratively, you can clone this repository and install the minesweeper
package with:
$ pip install -e minesweeper
Test by running: minesweeper --help
or python -m minesweeper --help
Provide a grid with the following cell types:
[ ]
: empty cell.[?]
: non-visible cell.[*]
: flagged cell.[i]
: cell with a number hint between 1 to 8 (e.g.,[3]
,[1]
).
Consider the popular XKCD commic "Mine Captcha". We can represent that board configuration as following:
[2] [?] [1] [?]
[?] [?] [3] [?]
[3] [?] [?] [?]
[?] [1] [?] [1]
You can execute the program by passing the grid & timeout as arguments.
Example 1:
$ minesweeper --timeout 3 --grid """
[3] [?] [2] [?]
[?] [?] [ ] [?]
"""
Expected output:
[3] [*] [2] [ ]
[*] [*] [ ] [ ]
Example 2:
$ minesweeper --timeout 3 --grid """
[2] [?] [1] [?]
[?] [?] [3] [?]
[3] [?] [?] [?]
[?] [1] [?] [1]
"""
Expected output:
[2] [ ] [1] [ ]
[*] [*] [3] [ ]
[3] [*] [ ] [*]
[ ] [1] [ ] [1]
Once installed, you can also use this application as a python library.
Create a simple board:
from minesweeper.board import Grid, Cell
my_grid = Grid(
values=[
[Cell.num(2), Cell.unk(), Cell.num(1), Cell.unk()],
[Cell.unk(), Cell.unk(), Cell.num(3), Cell.unk()],
[Cell.num(3), Cell.unk(), Cell.unk(), Cell.unk()],
[Cell.unk(), Cell.num(1), Cell.unk(), Cell.num(1)]
]
)
my_grid.show()
You can solve a grid by using the Solver
class:
from minesweeper.solver import Solver
solver = Solver(gird=my_grid)
solver.run(timeout=10)
if solver.finished():
solution = solver.final_state
solution.show()
There are lot's of corner cases still be to implemented. This repo was originally created to complement a twitter post, so will probably not get updated any time soon (: