Headless Puzzle
Headbreaker provides a Puzzle object which allows you to fully manipulate its model and individual Pieces. Since Puzzles are not coupled to any visual representation, they can be loaded in headless environments like a Node.js server.
Example
Section titled “Example”// ES module importimport { Puzzle, Tab, Slot, vector } from 'headbreaker-ts';
// Or CommonJS// const { Puzzle, Tab, Slot, vector } = require('headbreaker-ts');
// Create a puzzleconst puzzle = new Puzzle();puzzle .newPiece({right: Tab}) .locateAt(0, 0);puzzle .newPiece({left: Slot, right: Tab}) .locateAt(3, 0);puzzle .newPiece({left: Slot, right: Tab, down: Slot}) .locateAt(6, 0);puzzle .newPiece({up: Tab}) .locateAt(6, 3);
// Connect puzzle's nearby piecespuzzle.autoconnect();
// Translate puzzlepuzzle.translate(10, 10);
// Shuffle piecespuzzle.shuffle(100, 100);
// Relocate pieces to fit into a bounding box// while preserving their relative positions, if possiblepuzzle.reframe(vector(0, 0), vector(20, 20));
// Directly manipulate piecesconst [a, b, c, d] = puzzle.pieces;
// Drag a piece 10 steps right and 5 steps downa.drag(10, 5);
// Connect two pieces (if possible)a.tryConnectWith(b);Custom metadata
Section titled “Custom metadata”// Add custom metadata to piecesa.metadata.flavour = "chocolate";a.metadata.sugar = true;b.metadata.flavour = "chocolate";b.metadata.sugar = false;
c.metadata.flavour = "vainilla";c.metadata.sugar = false;d.metadata.flavour = "vainilla";d.metadata.sugar = true;Connection requirements
Section titled “Connection requirements”// Require pieces to match a given condition in// order to be connectedpuzzle.attachConnectionRequirement( (one, other) => one.metadata.flavour === other.metadata.flavour);
// Alternatively, set individual requirements for horizontal// and vertical connectionspuzzle.attachVerticalConnectionRequirement( (one, other) => one.metadata.flavour === other.metadata.flavour);puzzle.attachHorizontalConnectionRequirement( (one, other) => one.metadata.sugar !== other.metadata.sugar);
// Remove all connection requirementspuzzle.clearConnectionRequirements();Import and export
Section titled “Import and export”// Export and import puzzleconst dump = puzzle.export();const otherPuzzle = Puzzle.import(dump);