Skip to content

jeremytsang1/minesweeper

Repository files navigation

Minesweeper

Table of Contents

Description

My personal implementation of the classic Minesweeper game.

Rules

  • The objective of the game is to open all cells not containing mines. If the player opens a cell containing a mine they lose the round. Note that when opening a cell there are three options to its contents:
    1. containing a bomb
    2. containing a the number of adjacent cells that contain bombs
    3. empty (no adjacent cells contain bombs)
  • The player is capable of the 3 following actions

Opening a cell

This will open a single cell. If the cell contains a bomb the player dies. If it contains a number then only the opened cell is opened. If is empty all adjacent cells are opened. That is empty cells can perform a Depth First Search that terminates on number cells.

Toggling a flag on a cell

This can be used to mark a cell as having a bomb. This is useful for chording as seen below.

Chording a cell

If a given number cell has a number of flags adjacent equal to its number it can be chorded. This means all non-flagged unopened cells will be opened as if they were opened as in (1). If a flag is not properly assigned (i.e. flagging a cell that does not contain a bomb) this could lead to the player’s death.

Generation of random boards

Note that in this particular implementation mines will not be laid until the player firsts opens a cell. This is a usability measure to prevent the player from dying on their first cell opening. A random puzzle of a board with m rows, n columns, and k (with k < m * n) mines is generated by taking a list all cell coordinates ((m * n) - 1 to be exact as we exclude the player’s first choice), shuffling said list, and then placing mines on the first k coordinates in the list. Since a shuffle of l items can be done in O(l) time (See the Fisher Yates algorithm) this would mean that our puzzle generation would take at most O(m * n).

This particular generates random boards of the following preset difficulties (EASY, MEDIUM, HARD) as well as user specified row, col, mine counts. A summary is listed in the table below

difficultyrowscolsmines
EASY101010
MEDIUM161640
HARD163099
CUSTOMuser specifieduser specifieduser specified

Requirements

  • 3.6 <= Python <= 3.8.12
  • For specific Python packages see requirements.txt.

Installation

Option 1: Local

  • Unzip the .zip file.
  • cd into the top root directory of the unzipped file.
  • Install dependencies
    pip install -r requirements.txt
        
  • Run the main script
    ./main.py
        
  • Follow instructions on terminal to choose TUI or GUI mode.

Option 2: repl.it

  1. Navigate to the following link: https://repl.it/@jatsang52/minesweeper#main.py

    screenshots/repl-it-step-1.png

  2. In the repl.it terminal, install dependencies with pip install -r requirements.txt

    screenshots/repl-it-step-2.png

  3. After the installation completes press the Run button.

    screenshots/repl-it-step-3.png

  4. In the repl.it terminal choose TUI or GUI mode.

    screenshots/repl-it-step-4.png

  5. In the repl.it terminal if you choose GUI mode make sure to choose option 1 to disable sound (repl.it doesn’t play nicely with pygame apps with sound)

    screenshots/repl-it-step-5a.png

    screenshots/repl-it-step-5b.png

Modes

Graphical User Interface (GUI)

Controls

  • After pressing Play left click anywhere to start the game.
  • Mouse controls
    controlaction
    left clickopen cell
    right clicktoggle flag
    middle clickchord cell
  • Keyboard controls
    keyaction
    qQuit to main menu

Screenshots

screenshots/screenshot_gui_hard.png

Video

Youtube: Minesweeper GUI Demo

Terminal User Interface (TUI)

Controls

  • Follow menu directions on screen.

Screenshots

  • Starting an EASY difficulty game.

    screenshots/screenshot_tui_easy.png

  • Starting an CUSTOM difficulty game.

    screenshots/screenshot_tui_custom.png

Video

Youtube: Minesweeper TUI Demo

Credits

Icons

Numbers

Number icons made by Freepik from www.flaticon.com

Bombs

Bomb Icons made by Freepik from www.flaticon.com

Flags

Flag Icons made by Vectors Market from www.flaticon.com

Squares

Square Icons made by Freepik from www.flaticon.com

Emoticons

Emoticon Icons made by Pixel perfect from www.flaticon.com

Sounds

End Game

About

Minesweeper in Python

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published