

> swap_tiles ( board, 7, 5 ) array (,, ]) Solving Boards You can also use a tile number and it will be located automatically. Notice that if only one tile coordinate is provided to swap_tiles() the blank location is located automatically and used. > board array (,, ]) > moves = get_next_moves ( board ) > moves > swap_tiles ( board, moves ) array (,, ]) Moves are represented as coordinates adjacent to the blank. Provided a board size, you can find the target position for a particular tile.įor example, to locate the destination of the 6 tile on a 3x3 board: > get_goal_yx ( 3, 3, 6 ) ( 1, 2 ) Note: Coordinates are in (row, column) order. > board = shuffle ( new_board ( 3, 3 )) > board array (,, ]) > find_blank ( board ) ( 1, 1 ) > find_tile ( board, 3 ) ( 1, 0 ) The search() routine will validate the provided board before beginning, and may throw a ValueError if the board is illegal. Not all board configurations are solvable.

You can easily build your own boards using numpy or any of the provided convenience methods: > board = from_rows (,, ) > print_board ( board ) 1 2 3 4 5 6 7 8 > board = from_cols (,, ) > print_board ( board ) 1 4 7 2 5 8 3 6 > board = from_iter ( 3, 3, ) > print_board ( board ) 1 2 3 4 5 6 7 8 > board = from_iter ( 3, 3, , row_major = False ) > print_board ( board ) 1 4 7 2 5 8 3 6 > flatten_board ( board ) > freeze_board ( board ) (( 1, 4, 7 ), ( 2, 5, 8 ), ( 3, 6, 0 ))

The board is modified in-place and returned for chaining convenience.īoards are stored as numpy arrays. Using any of the provided shuffle() methods will guarantee a solvable board. Installation pip install slidingpuzzle Simple Example from slidingpuzzle import * board = from_rows (,, ) solution = search ( board ) print_board ( board ) print ( solution ) 8 3 1 4 2 5 6 7 solution= solution_len=22, generated=1059, expanded=618, unvisited=442, visited=394 Working with Boards > from slidingpuzzle import * > board = new_board ( 3, 3 ) > print_board ( board ) 1 2 3 4 5 6 7 8 > print_board ( shuffle ( board )) 3 5 7 2 1 6 8 4

Installation | Documentation ( Latest | Stable)Ī package for solving sliding tile puzzles.
