Tiny Tracks is a free train puzzle that runs in the browser. You turn track tiles to join each coloured shed to its station, press Go, and all the trains run at once. It is the third game in our series of small Three.js puzzles, after Frostpush and Blastboard. This post covers what was new this time: describing track with a few numbers, building levels that are solvable by construction, simulating trains and crashes, and moving trains smoothly around curves.

Track as sides of a square

Every tile is a square with four sides: north, east, south and west, numbered 0 to 3 going clockwise. A track piece is just the set of sides it joins:

  • A straight joins two opposite sides, north and south, or east and west.
  • A curve joins two neighbouring sides. At rotation 0 it joins north and east, and each quarter turn adds one to both sides.
  • A crossing joins north to south and east to west, and trains always go straight through.
  • A depot and a station each have a single open side.

With that, the key question, “a train enters this tile from the west, where does it leave?”, has a one-line answer: find the other side the piece joins, or report that the train cannot enter at all. Rotating a tile is adding one to its rotation, modulo four.

Simulating the trains

The simulation moves every running train through one tile per beat, all at the same time. For each train it works out the next tile and asks it for an exit side. That gives four ways to fail:

  • The next square is off the board, or has no track.
  • The track does not connect to the side the train enters from, so the train comes off the rails.
  • The train reaches a station of the wrong colour.
  • Two trains crash.

Crashes need care, because the trains move together. We check three cases after working out every train’s next move: two trains entering the same tile on the same beat, two trains swapping tiles head-on, and a train running into one already parked in its station. Crossings count as a shared tile, which is what makes timing matter on later levels.

The simulation returns the full path of each train, beat by beat, plus a short reason when something fails, such as “the red train came off the rails”. The game shows that sentence to the player.

Levels that are solvable by construction

Rotation puzzles are easy to make impossible by accident, so the generator works backwards from a solution:

  1. Place a depot on the edge of the board, facing inwards.
  2. Walk a route. From the depot, take a random walk across free squares, going straight a little under half the time and turning left or right otherwise. The last square becomes the station of the same colour, opening towards the way the train arrives.
  3. Record the tile each step needs. If the train enters and leaves through opposite sides, the square gets a straight. If not, a curve with the rotation that joins those two sides.
  4. Allow crossings on later levels. If a route meets another route’s straight at right angles, that square becomes a fixed crossing, as long as the route can carry on straight across it.
  5. Repeat for each train, then run the simulation on the finished layout to make sure every train arrives without a crash, including at crossings.

At that point the level is solved. To make it a puzzle, the generator adds decoy tiles on some of the empty squares, locks a few route tiles on the easiest levels as hints, and then turns every unlocked route tile to a rotation that does not join the right sides. It keeps the solution alongside the scrambled level.

A verification script checks each level both ways: the stored solution gets every train home, and the scrambled starting layout does not. Levels are built from a short list of settings, one per level: board size, number of trains, route length, share of decoys, crossings on or off and number of locked hints.

Drawing the track

Each tile is built once in its rotation-0 position and then turned as a group, so a quarter turn is a single change to the group’s rotation and easy to animate.

  • Straight rails are two thin boxes with five sleepers across them.
  • Curved rails are two quarter rings from Three.js’s torus geometry, one slightly inside the other, centred on the corner the curve wraps around, with sleepers fanned along the arc.
  • Locked tiles get a darker base and two bolt heads, so they read as fixed at a glance.

Depots are little sheds and stations are platforms with a coloured roof, both made of plain boxes in the train’s colour.

Moving trains around curves

The simulation says which tile each train is on at each beat, and which sides it enters and leaves by. The renderer turns that into smooth motion. A position along the route is measured in tiles, so 3.5 means halfway through the fourth tile. Within a tile:

  • If the train goes straight, it moves in a line from the middle of the entry side to the middle of the exit side.
  • If it turns, it follows a quarter circle of half a tile’s radius around the corner shared by the entry and exit sides.

The train faces the direction of travel by looking a tiny step ahead along the same path. A wagon follows half a tile behind the engine on the same path, which makes the trains bend around curves convincingly for very little code. Puffs of smoke from each chimney finish the effect.

Stars that reward thinking ahead

The other games score moves or shots. In Tiny Tracks the real skill is getting the routes right before pressing Go, so stars count runs. Every train home on the first run earns three stars, two or three runs earn two, and any finish earns one. When a run fails, the trains go back to their sheds but the track stays as the player left it, so a failed run teaches something without undoing their work.

Testing it end to end

As with the other games, a small test hook lets a headless browser drive the real game. The test sets each level to its stored solution, presses Go, waits for the trains and checks that the level-complete card appears. It also deliberately runs one level unsolved to check that the failure message shows and the trains reset. All 20 levels passed.

What we reused

The email screen, analytics, sound, saved progress, styles and page template came straight from the earlier games, along with the camera fitting from Frostpush. Most of the work was the track model, the generator and the train animation. Generating from a known solution has now worked for two very different puzzles, and it is the approach we will reach for first next time.

You can see the result by playing Tiny Tracks.