MattParkerJigsawPuzzleProblem

Documentation for MattParkerJigsawPuzzleProblem.

In this YouTube video Matt Parker asks for a program to help design and solve jigsaw puzzles.

The idea is to produce a set of jigsaw puzzle pieces where the pieces can be assembled in more than one way, and, ideally, in exactly two ways.

This is an attempt at that.

Usage

Here is a simple usage example:

using MattParkerJigsawPuzzleProblem

begin
    # Degenerate example of a puzzle, 2 × 2 grid
    # with 4 possible solutions:
    puzzle = MultipleSolutionPuzzle(2, 2, 4)
    pieces = map(ImmutablePuzzlePiece, puzzle_pieces(puzzle))
    # Find the solutions and show them in HTML:
    solver = Solver(size(puzzle), pieces)
    solve(solver)
    writing_html_file("two_by_two_example.html") do
        grids_to_html(solver.solved_grids)
    end
    length(solver.solved_grids)
end
64

See the resulting solution grids: twobytwo_example.html

The rest of the documentation (see the menu in the corner above) presents the theory of operation.

Index

Descriptions

MattParkerJigsawPuzzleProblem.EdgeType
Edge(::EdgeType, ::BallOrSocket)

Edge represents one edge of a puzzle piece. It has an edge_type. The bs field indicates whether the edge is a ball or socket.

source
MattParkerJigsawPuzzleProblem.MultipleSolutionPuzzleType
MultipleSolutionPuzzle(number_of_rows, number_of_columns, number_of_grids)

MultipleSolutionPuzzle is used to generate jigsaw puzzles with the specified numnber of rows and colukmns. It will attempy to generate number_of_grids puzzles with the same pieces. The pieces that are generated can be assembled least that number of of different ways. One must run the solver to see how many ways the pieces can actually be assembled.

Use puzzle_pieces to get the pieces.

source
MattParkerJigsawPuzzleProblem.edge_indexMethod
edge_index(::Int)::Int

Turns the argument value into a valid edge index using the modulus operator. This allows us to use aritmetic on edge indices and still get a valid index.

source
MattParkerJigsawPuzzleProblem.fit_pieceMethod
fit_piece(continuation, grid::Grid, row::Int, col::Int,
               piece::ImmutablePuzzlePiece)

Attempts to fit piece into the specified location of grid. continuation is called for each rotation of piece that fits.

source
MattParkerJigsawPuzzleProblem.get_neighboring_edgeMethod

getneighboringedge(grid::Grid, row::Int, col::Int, direction::CardinalDirection)

Returns the Edge of the "neighboring cell" to the specified grid location. If that location is out of bounds then Edge(EdgeType(true, 0), Straight()) is returned. Otherwise, if there is no GridCell at the specified location yet, then missing is returned.

source
MattParkerJigsawPuzzleProblem.grid_to_htmlFunction
grid_to_html(grid::Grid, piece_numbers=nothing)

Generates HTML representing a tabular view of grid.

If piece_numbers is specified it is a Dict mapping from a puzzle piece to s small integer id to be printed in the center of the piece.

source
MattParkerJigsawPuzzleProblem.mating_piece_indicesMethod
mating_piece_indices(continuation, piece1::ImmutablePuzzlePiece, piece1::ImmutablePuzzlePiece)

for each Edge of piece1 that mates with an Edge of piece2, Calls continuation, on the indices into those two pieces of those mating edges.

source
MattParkerJigsawPuzzleProblem.perimetersMethod
perimeters(puzzle::MultipleSolutionPuzzle, row::Int, col::Int)

Returns a four element vector indicating for each direction (N, E, S, W) whether that side of the cell at row, col is a perimeter of the puzzle.

source