Menu

1 / 5

Two-Turn Path Finding for Mahjong Connect Puzzles

DEV Community: kotlin·Nesim Razon·11 days ago
#4uhfHWkV
#devtokotlin#devto#dev#path#column
Long article (1464 words) — no AI summary yet.
Reading 0:00
15s threshold

In a Connect puzzle you may remove two identical tiles if an empty path joins them with at most two turns. That is three straight segments and no more. The check runs on every tap, for every hint and every shuffle, so it has to be small and correct. Here is how the path finder in Tilelings works, case by case, with the edge cases that need care.

The rule in one picture

Take two tiles, A and B. A path is a chain of horizontal and vertical segments that starts at A, ends at B and passes only through empty cells. The two end cells are the tiles themselves, so they do not count as blocked. At most two turns means at most three segments.

That gives exactly four shapes to test, from simplest to most complex.

0 turns: one straight line.

1 turn: an L shape, with one corner.

2 turns: a Z or U shape, with two corners.

Nothing else. If none of the three works, the pair cannot connect.

One helper: is this line clear?

Everything is built from a single helper. In the code it is called clear.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.