15puzzle-utils

An npm library for creating and manipulating NxM sliding puzzles

        npm install 15puzzle-utils
    
                
                    // Import the library
                    import { Puzzle } from '15puzzle-utils';
                
            
                
                    // Creates a 4x4 shuffled solvable puzzle
                    const puzzle = new Puzzle();
                
            
                
                    // Creates a NxM shuffled solvable puzzle
                    const puzzle = new Puzzle(3, 5);
                
            
                
                    // Creates puzzle from specific array (this one is unsolvable!)
                    const puzzle = new Puzzle([
                        [ 1,  2,  3,  4],
                        [ 5,  6,  7,  8],
                        [ 9, 10, 11, 12],
                        [13, 15, 14,  0]
                    ]);
                
            
                
                    // Creates puzzle from specific array (this one is already solved)
                    const puzzle = new Puzzle([
                        [ 1,  2,  3,  4],
                        [ 5,  6,  7,  8],
                        [ 9, 10, 11, 12],
                        [13, 14, 15,  0]
                    ]);
                
            
                
                    // Checks if the current puzzle is in solved state
                    puzzle.isSolved();
                
            
                
                    // Checks if the current puzzle can be solved
                    puzzle.isSolvable();
                
            
                
                    // Checks if tile(s) at position can slide toward empty space
                    puzzle.canSlide(row, col);
                
            
                
                    // Slides tile(s) at position toward empty space
                    // Returns movement direction (U=up, D=down, L=left, R=right) and tile count
                    // Examples: "U" (1 tile up), "L3" (3 tiles left)
                    puzzle.slide(row, col);
                
            
                
                    // Gets puzzle as 2D array (intuitive for small puzzles)
                    puzzle.getGrid();
                
            
                
                    // Gets puzzle dimensions
                    puzzle.getWidth();
                    puzzle.getHeight();
                
            
                
                    // Gets value of specific cell
                    puzzle.getValue(row, col);
                
            
                
                    // Gets position of empty space as {row, col}
                    puzzle.getEmptyPosition();
                
            
                
                    // Returns string representation of current puzzle state
                    puzzle.toAsciiString();