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.18.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pygame_minesweeper_core-1.0.18-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pygame-minesweeper-core-1.0.18.tar.gz
  • Upload date:
  • Size: 5.4 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.18.tar.gz
Algorithm Hash digest
SHA256 32e56b24d3d0d9ebe560d0022c4c42aca7f286268560c4ef656665b836527c3c
MD5 59d95af62a32221cdaa668b64e8db962
BLAKE2b-256 73dd71e1bd1b5f07475ad1aaa62c2fa9e30ec751746fdfc19c2fb690840af5af

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pygame_minesweeper_core-1.0.18-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.18-py3-none-any.whl
Algorithm Hash digest
SHA256 416d41b06186555cca864aed759b0448fd1349e9efbf5fc52f8448193c1f5a32
MD5 d1b931e47f5703f6c2b2d019efb77d86
BLAKE2b-256 2db7b31bb7b721736ab7430a3d46421bf0abad5c9023552436903d7ae8bec75e

See more details on using hashes here.

Supported by

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