Skip to content

nirav-email81/minesweeper-py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎮 Minesweeper Python Game

A classic Minesweeper game implementation in Python with a command-line interface. Features multiple difficulty levels, custom game options, and a fully functional game engine.

Features

Multiple Difficulty Levels

  • Easy: 8×8 board with 10 mines
  • Medium: 10×10 board with 10 mines
  • Hard: 12×12 board with 40 mines
  • Expert: 16×16 board with 99 mines
  • Custom: Define your own board size and mine count

🎯 Core Gameplay

  • Reveal cells to find safe areas
  • Flag suspected mines
  • Flood-fill algorithm for auto-revealing empty areas
  • Move counter and game statistics
  • Win/lose detection

🔧 Technical Features

  • Object-oriented design with separate Board and Game classes
  • Input validation and error handling
  • Clear command-line interface with ASCII art
  • No external dependencies (uses only Python standard library)

Project Structure

minesweeper/
├── main.py              # Game entry point and UI
├── src/
│   ├── game.py         # Game controller logic
│   ├── board.py        # Board management and cell logic
│   └── __init__.py     # Package initialization
├── requirements.txt    # Dependencies (none required)
└── README.md          # This file

Installation

Prerequisites

  • Python 3.7 or higher

Setup

  1. Navigate to the project directory:
cd minesweeper
  1. (Optional) Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. No external dependencies to install! The game uses only Python's standard library.

How to Play

Starting the Game

python main.py

Game Commands

Reveal a cell:

row col

Example: 3 5 - reveals cell at row 3, column 5

Flag a cell:

row col f

Example: 2 2 f - flags cell at row 2, column 2

Other commands:

  • help - Display instructions
  • new - Start a new game
  • quit or exit - Exit the game

Game Board Legend

Symbol Meaning
Hidden cell
💣 Mine (revealed)
🚩 Flagged cell
0-8 Number of adjacent mines
(space) Safe area with no adjacent mines

Gameplay Tips

  1. Start in the middle - This often gives you the most information
  2. Use logic - If a cell shows "2" and two adjacent cells are flagged, other neighbors are safe
  3. Flag strategically - Mark cells you suspect are mines to avoid clicking them
  4. Watch for patterns - Empty areas (0 adjacent mines) automatically expand
  5. Take your time - There's no timer, so think before you click!

Example Game Session

   ──────────────────────────────────────────────────────────────
   0  1  2  3  4  5  6  7  8  9
   ──────────────────────────────────────────────────────────────
 0 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 1 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 2 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 3 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 4 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 5 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 6 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 7 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 8 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
 9 |  ●  ●  ●  ●  ●  ●  ●  ●  ●  ●|
   ──────────────────────────────────────────────────────────────

┌─ STATS ─────────────────────┐
│ Difficulty: Easy             │
│ Moves: 0                     │
│ Mines: 10                    │
│ Flagged: 0                   │
│ Remaining: 10                │
│ Unrevealed: 100              │
└──────────────────────────────┘

Enter move (row col) [or 'help']: 5 5

Code Architecture

board.py

  • Cell: Represents a single board cell with mine status, reveal state, and flag state
  • Board: Manages the game board, mine placement, and cell logic
    • Flood-fill algorithm for revealing safe areas
    • Adjacent mine calculation
    • Win condition detection

game.py

  • Game: High-level game controller
    • Move processing
    • Game state management
    • Statistics tracking
    • Difficulty level detection

main.py

  • User interface and game loop
  • Input parsing and validation
  • Menu system
  • Game display and formatting

Features Breakdown

Mine Placement

Mines are randomly placed on the board to ensure variety and replayability.

Flood Fill Algorithm

When you reveal a cell with no adjacent mines, the game automatically reveals all connected empty areas, making the game flow smoother.

Statistics

Track your progress with:

  • Move counter
  • Flagged mines count
  • Remaining unrevealed cells
  • Game difficulty level

Future Enhancement Ideas

  • 🎨 GUI version using Tkinter or PyGame
  • ⏱️ Timer functionality
  • 🏆 Leaderboard/high scores
  • 🎯 Difficulty presets with time challenges
  • 💾 Save/load game functionality
  • 🌓 Dark mode theme

Development Notes

  • Pure Python implementation with no external dependencies
  • Follows PEP 8 style guidelines
  • Type hints included for better code clarity
  • Comprehensive docstrings for all functions

License

This project is open source and available for educational purposes.

Author

Created as a classic game implementation in Python.


Enjoy playing Minesweeper! 🎮

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages