npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

boogie-solver

v2.2.0

Published

Solver for boggle

Downloads

4

Readme

Boogie Solver

A module for solving Boggle boards.

The project is written in Typescript, and provides definitions to consumers. You can find a demonstration project that uses this package from npm at: https://github.com/pete-otaqui/boogie-solver-demo

Usage

First get the package:

$ npm install boogie-solver

Then you can roll the dice and use the (promise-returning / async-friendly) solve() function to find the possilbe words in the board:

import { rollDice, solveTrie, sowpodsTrie } from "boogie-solver";

async function main() {
  const BOARD_WIDTH = 4;
  const BOARD_HEIGHT = 4;

  const dice = rolleDice(BOARD_WIDTH, BOARD_HEIGHT);
  const solution = await solveTrie(dice, sowpodsTrie);

  console.log(solution.words.length); // unique words found
  console.log(solution.paths.length); // paths for words found, with positions
}

main();

You can also provide your own set of "dice" in a 2-d array of rows:

import { solveTrie, sowpodsTrie } from "boogie-solver";

async function main() {
  // provide a 4x4 grid of dice, in an array of row arrays.
  const dice = [
    ["h", "e", "w", "y"],
    ["c", "i", "h", "h"],
    ["l", "v", "a", "o"],
    ["r", "e", "a", "s"],
  ];

  const solution = await solveTrie(dice, sowpodsTrie);

  console.log(solution.words.length); // unique words found
  console.log(solution.paths.length); // paths for words found, with positions
}

main();

About the dice used

Note that the set of dice is a specific one (although there have been multiple different versions of the original game). It expects one face to be "Qu" rather than just "Q" on it's own.

A note about the "trie" structures and TypeScript

It's not possible under many circumstances to import a large tree structure with TypeScript - it uses much more memory than a simple require of the same file.

This limitation means that there a couple of extra hoops to jump through, both when using this library and also when developing for it.

This library provides two different word tries (trees) - Sowpods and OED.

If you want to provide your own trie, it should be structured based on the letters of each word, with a { _: true } property for whole words.

import { solveTrie } from "boogie-solver";

// manually construct a trie which contains the words quip, quips, quit, quits.
// note that "qu" is considered a single die face.
const trie = {
  qu: {
    i: {
      p: {
        _: true, // "qu-i-p" is a word
        s: {
          _: true, // "qu-i-p-s" is a word
        },
      },
      t: {
        _: true, // "qu-i-t" is a word
        s: {
          _: true, // "qu-i-t-s" is a word
        },
      },
    },
  },
};

async function main() {
  const dice = [["qu", "i", "s"], ["x", "p", "t"]];

  const solution = await solveTrie(dice, trie);
  console.log(solution.words); // ["quip", "quips", "quit", "quits"]
}

main();

Development Notes

Project structure

All the code is in the ./src/ directory, and is compiled to ./build/.

You will notice that there is a ./src/types.ts file that declares all the types for the project - these are manually imported into the other files.

Scripts

  • npm run build - cleans and comiles the project to the ./build/ directory
  • npm run build:clean - deletes the ./build/ directory
  • npm run build:compile - compiles the typescript to the ./build/ directory
  • npm run demo - runs a demo of the module, outputting to the console
  • npm run lint - runs tslint on the source code.
  • npm run pre-commit - useful script to use in a git pre-commit hook: runs lint, test and build scripts
  • npm test - runs the test scripts, collects coverage and puts the report in the ./coverage/ directory, as well using ./.nyc_output/.
  • npm run test:quick - just runs the native test scripts, really quickly and with better error messaging if you need it.
  • npm run tries - build trie structures from word lists in the /src/word-lists/ directory, only required if you add/update the word lists.

Notes

Areas that could do with improvement:

  1. Make the dice used more flexible, i.e. not always the same set. This also requires the solve(), solveTrie() and other routines to "know" what dice are being used, rather than assuming that "qu" will always be the only double-letter die face.
  2. The trie building / parsing functions are a fairly naïve port from https://github.com/pillowfication/pf-boggle and the same user's sowpods repo. While these are efficient, there's quite a lot of mutation of things that is very effective, but I would prefer to make more purely functional to match the style of the rest of the codebase.
  3. With the trie-based solutions in place, the non-trie based parts should be removed, since they are essentially cruft now.
  4. Would be nice to understand why TypeScript explodes when importing the trie json files, and if there is any way to make the compiler not try and do whatever parsing is causing that (and then we could just import them normally and skip all the require and cp shenanigans).