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)
end64See 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
MattParkerJigsawPuzzleProblem.ALL_EDGE_TYPESMattParkerJigsawPuzzleProblem.AbstractPuzzlePieceMattParkerJigsawPuzzleProblem.BallOrSocketMattParkerJigsawPuzzleProblem.EdgeMattParkerJigsawPuzzleProblem.EdgeTypeMattParkerJigsawPuzzleProblem.GridCellMattParkerJigsawPuzzleProblem.ImmutablePuzzlePieceMattParkerJigsawPuzzleProblem.MultipleSolutionPuzzleMattParkerJigsawPuzzleProblem.MutablePuzzlePieceMattParkerJigsawPuzzleProblem.add_one_pieceMattParkerJigsawPuzzleProblem.edge_indexMattParkerJigsawPuzzleProblem.edges_mateMattParkerJigsawPuzzleProblem.fit_pieceMattParkerJigsawPuzzleProblem.get_neighboring_edgeMattParkerJigsawPuzzleProblem.grid_to_htmlMattParkerJigsawPuzzleProblem.grids_to_htmlMattParkerJigsawPuzzleProblem.mating_piece_indicesMattParkerJigsawPuzzleProblem.new_gridMattParkerJigsawPuzzleProblem.oppositeMattParkerJigsawPuzzleProblem.perimeter_edge_indicesMattParkerJigsawPuzzleProblem.perimeter_edge_indicesMattParkerJigsawPuzzleProblem.perimetersMattParkerJigsawPuzzleProblem.piece_edgeMattParkerJigsawPuzzleProblem.puzzle_piecesMattParkerJigsawPuzzleProblem.rotationMattParkerJigsawPuzzleProblem.solveMattParkerJigsawPuzzleProblem.terserepMattParkerJigsawPuzzleProblem.writing_html_file
Descriptions
MattParkerJigsawPuzzleProblem.ALL_EDGE_TYPES — Constant
ALL_EDGE_TYPESALL_EDGE_TYPES is a vector of all of the EdgeTypes that have been created.
MattParkerJigsawPuzzleProblem.AbstractPuzzlePiece — Type
AbstractPuzzlePieceAbstractPuzzlePiece is the abstract supertype for all types of jigsaw puzzle piece.
MattParkerJigsawPuzzleProblem.BallOrSocket — Type
BallOrSocket
Ball
Socket
StraightMattParkerJigsawPuzzleProblem.Edge — Type
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.
MattParkerJigsawPuzzleProblem.EdgeType — Type
EdgeType(isperimeter::Bool)Creates a unique EdgeType.
isperimeter indicates if the EdgeType is for an edge on the perimeter of the puzzle.
MattParkerJigsawPuzzleProblem.GridCell — Type
GridCell(::AbstractPuzzlePiece, rotation::Int)A GridCell is the container for a puzzle piece in a puzzle grid.
MattParkerJigsawPuzzleProblem.ImmutablePuzzlePiece — Type
ImmutablePuzzlePiece(from::MutablePuzzlePiece)Constructs an ImmutablePuzzlePiece from a MutablePuzzlePiece.
MattParkerJigsawPuzzleProblem.MultipleSolutionPuzzle — Type
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.
MattParkerJigsawPuzzleProblem.MutablePuzzlePiece — Type
MutablePuzzlePiece()Constructs a MutablePuzzlePiece with no edges defined yet.
MattParkerJigsawPuzzleProblem.add_one_piece — Method
add_one_piece(solver::Solver, grid::Grid)Looks for a piece to fit into the grid. If there is more than one mating piece then additional grids are created for them.
MattParkerJigsawPuzzleProblem.edge_index — Method
edge_index(::Int)::IntTurns 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.
MattParkerJigsawPuzzleProblem.edges_mate — Method
edges_mate(::Edge, ::Edge)::BoolTwo Edges mate if they have the same EdgeType and their bss are opposites.
MattParkerJigsawPuzzleProblem.fit_piece — Method
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.
MattParkerJigsawPuzzleProblem.get_neighboring_edge — Method
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.
MattParkerJigsawPuzzleProblem.grid_to_html — Function
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.
MattParkerJigsawPuzzleProblem.grids_to_html — Method
grids_to_html(grids::Vector{Grid})Generates HTML elements for the grids.
MattParkerJigsawPuzzleProblem.mating_piece_indices — Method
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.
MattParkerJigsawPuzzleProblem.new_grid — Method
new_grid(number_of_rows, number_of_columns)::GridCreates an empty puzzle grid of the specified dimensions.
MattParkerJigsawPuzzleProblem.opposite — Method
opposite(::CardinalDirection)::CardinalDirectionReturns the opposite direction. N() and S() are opposites. E() and W() are opposites.
MattParkerJigsawPuzzleProblem.perimeter_edge_indices — Method
perimeter_edge_indices(p::AbstractPuzzlePiece)Returns a vector of the indices of edges of the puzzle piece p that are perimeter edges.
MattParkerJigsawPuzzleProblem.perimeter_edge_indices — Method
perimeter_edge_indices(grid::Grid, row::int, col::Int)Returns a vector of the indices of edges of the specified cell of grid that are perimeter edges.
MattParkerJigsawPuzzleProblem.perimeters — Method
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.
MattParkerJigsawPuzzleProblem.piece_edge — Method
piece_edge(puzzle_piece, index)::EdgeReturns the Edge of puzzle_piece which corresponds to the specified edge_index.
MattParkerJigsawPuzzleProblem.puzzle_pieces — Method
puzzle_pieces(puzzle::MultipleSolutionPuzzle)::Vector{MutablePuzzlePiece}Returns a vector of all of the MutablePuzzlePieces of puzzle.
MattParkerJigsawPuzzleProblem.rotation — Method
rotation(rot::Int)Normalizes the rotation of the placement of a puzzle piece to one of 0, 1, 2, or 3.
MattParkerJigsawPuzzleProblem.solve — Method
solve(solver::Solver)Run the solver to find solutions to the jigsaw puzzle.
MattParkerJigsawPuzzleProblem.terserep — Method
terserep(puzzle)Output a terse printed representation of the puzzle.
MattParkerJigsawPuzzleProblem.writing_html_file — Method
writing_html_file(body, filename)Write an HTML file to filename.
body is a vector of elements to be included in the HTML document.