Skip to main content

Lib to make NxM - 1 sliding puzzles

Project description

Puzzle15

codecov PyPI version

A lib for a sliding block puzzle game: https://en.wikipedia.org/wiki/15_puzzle.

Allows making custom width and height puzzles.

Puzzles can be randomly generated or given as input.

Known clients: GitHub.

Functionality

  • Create N x M puzzle
    • Random puzzle from given width and height.
    • Given puzzle from string. Giving unsolvable puzzle causes error.
  • Check possible moves (up, right, down, left) in the current puzzle position.
  • Make a move.
  • See puzzle representation.
  • Check if puzzle is solved.

Usage

from puzzle15.puzzle import Puzzle

# Create a 4x4 random puzzle
puzzle = Puzzle.from_dimensions(4, 4)
print(puzzle)

# Create a puzzle from a string (use -1 for the blank)
puzzle_another = "1 2 3 4|5 6 7 8|9 10 11 12|13 14 15 -1"
puzzle = Puzzle.from_string(puzzle_another)
print(puzzle)

# Check possible moves
moves = puzzle.possible_moves()
print("Possible moves: ", moves)

# Make a move
if 'up' in puzzle.possible_moves():
    puzzle.move('up')
    print(puzzle)

# See puzzle representation
print(puzzle)
grid = puzzle.grid()
print(grid)

# Check if puzzle is solved
if puzzle.is_solved():
    print("Puzzle is solved!")
else:
    print("Puzzle is not solved.")

Caution

Please only use str(puzzle) method when printing. Otherwise make use of puzzle.grid() method.

If you couple to str(puzzle) and this method will change by introducing different formatting or showing the board in a different way, your code will break.

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

puzzle15-1.0.1.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

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

puzzle15-1.0.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file puzzle15-1.0.1.tar.gz.

File metadata

  • Download URL: puzzle15-1.0.1.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for puzzle15-1.0.1.tar.gz
Algorithm Hash digest
SHA256 346b498c89e50762ad47ef90744b412df2f8986756f076b1f3423c46ddeed542
MD5 56321c9c5914e10e332191f00b63f736
BLAKE2b-256 82d956c5266cab2dd0273aa5a426b0343c49e9692e0cda2a0024934072ef124d

See more details on using hashes here.

File details

Details for the file puzzle15-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: puzzle15-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for puzzle15-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 453889de17ba72375b33b8c4d69ee8c94e4c7209675831fd230c020b7580c017
MD5 d5e75103b8485aa90447f8ea5c02cfd7
BLAKE2b-256 752e566fbcb509a240a38559d4698d43c2cc749f4f3a26abee31942f4ccd5c97

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