November 1st, 2024

Generating Lever-Door Puzzles with JavaScript

The article details the creation of lever-door puzzles for a browser MMO, utilizing a JavaScript script and graph structures to generate complex puzzles, with code available on GitHub.

Read original articleLink Icon
Generating Lever-Door Puzzles with JavaScript

The article discusses the creation of lever-door puzzles for a browser-based MMO, inspired by similar puzzles in games like Runescape. These puzzles require players to navigate through rooms by activating levers that toggle the states of doors, with the goal of reaching a final room for a reward. The author outlines a method for generating these puzzles using a graph structure, where rooms are nodes and doors are edges. The complexity of the puzzle generation is highlighted, as the number of possible configurations grows exponentially with the number of doors and switches. To manage this complexity, the author employs a script written in JavaScript that utilizes a breadth-first search algorithm to explore potential puzzles efficiently. The script defines the graph, calculates possible door changes, and implements a recursive function to test various scenarios. The author also mentions optimizations to reduce the number of trials needed to find a viable puzzle configuration. After running the script for an hour, the author successfully generated a puzzle with a 16-step shortest path. The article concludes with an invitation to explore the full code on GitHub, suggesting that this puzzle format could be developed into a standalone game.

- Lever-door puzzles involve navigating rooms by toggling doors with levers.

- The puzzle generation process is complex, requiring a graph structure and extensive calculations.

- A JavaScript script is used to automate the generation of puzzles through a breadth-first search.

- The author achieved a successful puzzle configuration after running the script for an hour.

- The full code is available on GitHub for others to explore and create similar puzzles.

Link Icon 1 comments
By @AnotherGoodName - 5 months
There’s a good maths puzzle hidden in the table of levers toggling doors that has my curiosity while reading this.

Given levers that toggle at least one door and with a stipulation that no single lever toggles the state of exactly the same set of doors as another lever how many levers do you need before you can precisely toggle the state of X doors? I personally hypothesise the answer as X but welcome thought on this from those that know their linear maths.

I’ll give an example of 2 levers and 2 doors. 2 distinct levers can toggle all states of 2 doors.

Eg. Lever 1 toggles 1,1 ( notation for both doors)

Lever 2 toggles 1, 0 ( just the first door )

Since the levers xor over each other I can make a truth table of all four possible states of the doors

( 0, 0 )

( 0, 1 ) - both levers for to this

( 1, 0 ) - just the second lever

( 1, 1 ) - just the first lever

Now i could change what the levers do, eg. I could make the first lever toggle ( 0, 1 ) and I can still build the above complete truth table.

A quick sketch without stating it here and I can see I can do the same with 3 levers and 3 doors. Is it true that x unique levers can always toggle all states of x unique doors?