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.
    • Note that one of the cells if blank so numbers start from 1 and end in NxM - 1.
  • Check possible moves (up, right, down, left) in the current puzzle position.
  • Make a move. If move is invalid no action is taken.
  • 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)

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

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

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

# Make a random valid move
import random

moves = puzzle.possible_moves()
if moves:  # Make sure there are available moves
    random_move = random.choice(moves)
    puzzle.move(random_move)

# 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.2.tar.gz (16.6 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.2-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: puzzle15-1.0.2.tar.gz
  • Upload date:
  • Size: 16.6 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.2.tar.gz
Algorithm Hash digest
SHA256 0a167a4378d79f6f6344a37adb7b9089873c1978d23dfb15c3d81e86a7cfa77b
MD5 32d42b0349772c125a500a2a78c8f82b
BLAKE2b-256 7e42f9b4308b1cb1b23d2dcdc52fb07ee24be61ebb7bd08a66d20dec126b6ab9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: puzzle15-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.1 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f13a7f099ade3562d94df8ac296af26b5d51b57c1cc9daf5fe446d3449bceba2
MD5 f366b2e4cf8ba73209d4e029dc114234
BLAKE2b-256 10a0f0db1555614c171cfd00390c33805a75b6c2852713366b3398a3e3c026b3

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