Minesweeper game reengineered in Python using the Tkinter GUI library, following the Model-View-Controller (MVC) design pattern.
run.py- The entry point for the program. Accepts command-line arguments for difficulty, viewer type, and testing mode.images/- GIF images used by the Tkinter GUI for rendering tiles, flags, and treasures.model/- Package containing classes that represent the underlying Minesweeper game logic, including the board and cells.controller/- Package that connects the model to a specific view, serving as the game's logic and mediator.view/- Package containing multiple views (e.g., text-based or GUI) for interacting with the game.
- Clone the repository:
git clone https://github.com/Kohlil/python-tkinter-minesweeper.git
- Ensure you have Python 3.13 installed. Install Tkinter if necessary:
- Linux/WSL/MacOS:
brew install python-tk
- Linux/WSL/MacOS:
- Create a virtual environment:
python3.13 -m venv .venv
- Activate the virtual environment:
source .venv/bin/activate - Install dependencies:
pip install -r requirements.txt
- Run the Minesweeper game:
python run.py -h
The run.py program accepts the following command-line arguments:
<difficulty>: Select the difficulty level. Options:BEGINNER: 8x8 board with up to 10 mines.INTERMEDIATE: 16x16 board with up to 40 mines.EXPERT: 30x16 board with up to 99 mines.
<viewer>: Choose the viewer type. Options:tkinter: GUI-based view using the Tkinter library.text: Text-based view for playing in the terminal.
--testing-mode: Enable testing mode to load a predefined board from a CSV file. !! MUST BE BEGINNER DIFFICULTY !!
python run.py INTERMEDIATE tkinter
python run.py BEGINNER text --testing-modeThis project has been refactored to follow the MVC design pattern, improving modularity and separation of concerns. The reengineered system separates logic into three main components: model, view, and controller.
The model package contains all classes representing the Minesweeper game state. This includes the Board, which manages the grid, and Cell, which represents individual tiles.
The view package contains subclasses for different types of user interfaces (e.g., text-based, GUI). Each view inherits from the abstract MinesweeperViewer class, enabling the controller to interact with all views uniformly.
The controller package connects the model to a specific view. It handles user interactions, updates the model, and ensures the view reflects the latest game state.
Below is a table mapping functions from the original project to their corresponding locations in the reengineered system.
| Original | Reengineered |
|---|---|
__init__ |
view.MinesweeperViewer.__init__ |
setup (board creation) |
model.board.Board.place_items |
setup (count mines) |
model.board.Board.count_mines_treasures |
setup (view creation) |
view.MinesweeperViewer.initialize_board |
restart |
controller.Controller.handle_game_over |
refreshLabels |
view.MinesweeperViewer.update |
gameOver |
controller.Controller.handle_game_over |
updateTimer (model) |
model.board.Board.update_timer |
updateTimer (view) |
view.MinesweeperViewer.update_timer |
updateTimer (controller) |
controller.Controller.update_timer |
getNeighbors |
model.board.Board.get_neighbors |
onClickWrapper, onRightClickWrapper |
view.MinesweeperViewer |
onClick (controller) |
controller.Controller.handle_click |
onClick (model) |
model.board.Board.reveal_cell |
onRightClick (controller) |
controller.Controller.handle_flag |
onRightClick (model) |
model.board.Board.toggle_flag |
clearSurroundingTiles |
model.board.Board.reveal_cell (recursive) |
main |
run.py |
- Multiple Difficulty Levels: Beginner, Intermediate, and Expert difficulties with varying board sizes and mine counts.
- GUI and Text-Based Views: Play using a graphical interface or a terminal-based view.
- MVC Architecture: Clean separation of game logic (model), user interface (view), and game control (controller).
- Custom Boards: Load custom board configurations from a CSV file.
- Testing Mode: Easily enable testing mode via command-line arguments for pre-configured games.
Enjoy playing Minesweeper in your preferred format and difficulty level!
