Skip to main content

Minesweeper game core library

Project description

Build Status PyPI - License PyPI Azure DevOps coverage

Introduction

This minesweeper core library contains the basic functionality of the game.

Minesweeper is a single-player puzzle computer game. The objective of the game is to clear a rectangular board containing hidden "mines" or bombs without detonating any of them, with help from clues about the number of neighboring mines in each field. The game originates from the 1960s, and has been written for many computing platforms in use today. It has many variations and offshoots.

Motivation

The motivation of these minesweeper projects is to learn the tooling around python projects, how to create CI/CD pipelines for python projects, and distributing python eggs.

Getting Started

Installation

python3 -m pip install pygame-minesweeper-core
# or
pip install pygame-minesweeper-core

General

There are two classes in this library Board and BoardTile. The Board class contains data of the whole board/game while the BoardTile class only contains data of a single tile. To initialize a new game create a Board class with the required params. A board will be always generated randomly and the first tile that gets opened will never contain a mine. If an empty tile gets opened, adjacent tiles will also get opened.

Board

from minesweeper import core

board = core.Board(rows=10, cols=10, mines=30)

There is only a handful of methods and properties you should care about when using the Board class and those are:

methods returns
game_new(self, rows: int, cols: int, mines: int): initialize a new game with given parameters
game_reset() initialize a new game with the same parameters
tile_open(i: int, j: int) -> List[BoardTile] The value i is the row/y-axis and j is the col/x-axis. The function returns a list of BoardTiles objects that represents tiles that get opened. The function will return an empty list if you try to open a tile that is already opened, if the game is lost or won, or if you open tile that is out of bounds. The functions open adjacent tiles recursively if the tile has zero adjacent mines. The first tiles that get opened can never be a mine.
tile_valid(i : int, j : int) -> bool Returns true if i and j is inside boundaries
properties returns
str -> str string representation of the board
is_game_over -> bool returns true of the player has lost
is_game_finished -> bool returns true if the player has won
rows -> int number of rows in the game
cols -> int number of column in the game
mines -> int number of mines in the game
solution -> str a string representation of the solution
timer -> float a floating point number of play time. The timer will start when the first tile gets opened and when stops when either player has lost or won.

BoardTile

properties returns
i -> int The row of the tile
j -> int the columns of the tile
type -> str string representation of the tile
number -> int int representation of the tile
static returns
mine "x"
unopened "t"
zero "0"
one "1"
two "2"
three "3"
four "4"
five "5"
six "6"
seven "7"
eight "8"

Example

from minesweeper import core


def main():
    board = core.Board(rows=10, cols=10, mines=30)

    tiles = board.tile_open(5, 5)
    for tile in tiles:
        print(f"tile={tile.type}, ({tile.i}, {tile.j})")
    print(board.is_game_over)
    print(board.is_game_finished)
    print(board)
    print(board.solution)


if __name__ == "__main__":
    main()
# Outputs
tile=0, (5, 5)
tile=1, (5, 6)
tile=0, (5, 4)
tile=2, (5, 3)
tile=1, (6, 4)
tile=0, (6, 5)
tile=0, (6, 6)
tile=0, (6, 7)
tile=1, (6, 8)
tile=1, (7, 7)
tile=1, (7, 8)
tile=2, (7, 6)
tile=1, (5, 7)
tile=2, (5, 8)
tile=1, (7, 5)
tile=2, (7, 4)
tile=3, (6, 3)
tile=1, (4, 4)
tile=0, (4, 5)
tile=1, (4, 6)
tile=1, (3, 5)
tile=2, (3, 6)
tile=2, (3, 4)
tile=4, (4, 3)

False

False

t t t t t t t t t t
t t t t t t t t t t
t t t t t t t t t t
t t t t 2 1 2 t t t
t t t 4 1 0 1 t t t
t t t 2 0 0 1 1 2 t
t t t 3 1 0 0 0 1 t
t t t t 2 1 2 1 1 t
t t t t t t t t t t
t t t t t t t t t t

x 3 3 x x 3 x 2 x x
3 x x 3 4 x 3 2 3 x
2 x 5 3 3 x 2 1 2 2
2 4 x x 2 1 2 2 x 1
x 5 x 4 1 0 1 x 3 2
x 5 x 2 0 0 1 1 2 x
x 4 3 3 1 0 0 0 1 1
1 2 x x 2 1 2 1 1 0
0 1 3 3 3 x 3 x 3 2
0 0 1 x 2 2 x 3 x x

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygame-minesweeper-core-1.0.13.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygame_minesweeper_core-1.0.13-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pygame-minesweeper-core-1.0.13.tar.gz.

File metadata

  • Download URL: pygame-minesweeper-core-1.0.13.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.11

File hashes

Hashes for pygame-minesweeper-core-1.0.13.tar.gz
Algorithm Hash digest
SHA256 73827de9e170311907507eee546f2300e8f3d27f77fc7126387f6fcf95f2d5bf
MD5 07609dd56d307f922c6606b7763ebaef
BLAKE2b-256 48060588740e87d89ff197d35ba1a240e74ff65bf51ed5af05ba3d3a08f79fa7

See more details on using hashes here.

File details

Details for the file pygame_minesweeper_core-1.0.13-py3-none-any.whl.

File metadata

  • Download URL: pygame_minesweeper_core-1.0.13-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.6.11

File hashes

Hashes for pygame_minesweeper_core-1.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 987d4879a95d03cf0e649353ef26bd7fcab8c3f507047ccf6c8b3a5df6bdc36a
MD5 2c1c2bb701d0a355019d7b36aea01f3a
BLAKE2b-256 6f1179d26e4936631aba46cc815c3c1750be95a4bb3bad5f07684bec251361cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page