Gate Slide is a free colour block puzzle that runs in the browser: drag chunky blocks to the doors of their colour and they slide out and get shredded. It follows the style made famous by Color Block Jam, and it is the fourth game in our series of small Three.js puzzles. This post covers the parts that were new: blocks of any shape, sliding by drag, checking whether a block fits a door, building levels backwards, and a solver fast enough to rate them.

Blocks, walls and doors

A level is a grid with three kinds of things on it:

  • Blocks have a colour, a shape and a position. The shape is a list of cells relative to the block’s corner, so a square is four cells and a T block is four cells in a T.
  • Walls, the grey pillars, are single cells that never move.
  • Doors sit in the frame around the board. Each has a side (top, bottom, left or right), a starting cell, a width and a colour.

Blocks never rotate, which keeps both the rules and the controls simple.

Sliding: every reachable position at once

In Gate Slide a block can slide any distance, around corners, in one move, as long as it never overlaps another block or a wall. So instead of moving one cell at a time, the game works out every position the block could reach.

That is a breadth-first search over positions: start where the block is, try one step in each direction, keep the ones where every cell of the shape is inside the board and empty, and repeat until nothing new turns up. The result is the set of places the block could be dragged to.

Leaving through a door is a simple test on a position. Take the block’s cells, find their bounding box, and check whether it touches the edge where a door of the same colour sits, with the whole block inside the door’s width. If it does, the block leaves.

Drag that follows your finger

When you press on a block, the game runs the search once and keeps the set of reachable positions. As you drag, it works out where the pointer is on the board and snaps the block to the reachable position nearest to it, animating the hop. The block cannot jump through obstacles, because positions behind them are not in the set unless there is a route around.

Letting go counts as one move if the block ended somewhere new. If that position touches a matching door, the block slides through it, shrinks into the frame and bursts into particles, while the door shakes like a shredder.

Building levels backwards

Random boards are usually either trivial or impossible, so we build each level in reverse:

  1. Place the doors, one per colour, on free stretches of the frame.
  2. Bring blocks in through their doors. Each new block is placed touching its own door, as if it had just been about to leave, then slid to a random reachable spot, preferring spots away from the door.
  3. Repeat for every block. Each block enters a board that already holds the earlier ones, so later blocks often end up in front of earlier blocks’ doors.

Removing the blocks in the reverse order always works, because each one can retrace the path it came in by. That guarantees a solution.

There is a catch. A level built only this way never needs anything cleverer than finding the right order. To create boards where a block has to move aside, the generator also makes occasional backwards slides: after a block enters, it moves an existing block to another reachable spot. In the forward direction, that becomes a move the player has to make to free a route.

A solver that knows what to skip

The first version of the solver tried every reachable position for every block at every step. On boards with six or more blocks, it ran for minutes.

The fix was one observation: if a block can reach its door, sending it out is never a mistake. It has to leave eventually, it costs one move whenever it happens, and leaving only frees space for everything else. So the solver clears every block that can leave before branching, and only searches the moves that reposition blocks which cannot leave yet. The search went from minutes to milliseconds.

The fewest moves is then the number of blocks plus the fewest repositioning moves. That is the target for three stars, and the number of repositioning moves is our measure of how hard a level is. Our levels go from boards that need none to a final board of 14 blocks that needs three.

A verification script solves every level, replays the solution through the rules and checks that it clears the board in exactly the stored number of moves.

Drawing it

Gate Slide is drawn from almost straight above, so the board reads like a tray of toy blocks.

  • Blocks are built cell by cell from rounded boxes, each with a small stud on top, with short bridges between neighbouring cells so a shape reads as one solid piece.
  • The frame is built from segments around the board, leaving gaps where the doors are.
  • Doors are coloured bars with dark teeth, like a shredder. When you pick up a block, the door of its colour grows slightly so you can find it.

Testing it end to end

As with our other games, a small test hook lets a headless browser make moves in the running game. The test loads each level, plays the solver’s moves and checks that the level-cleared card appears. A second test drags a block with the mouse, from pressing on it to letting go, to make sure the drag controls themselves work. All 20 levels passed.

You can see the result by playing Gate Slide.