Two-Turn Path Finding for Mahjong Connect Puzzles
By Nesim Razon, independent mobile developer · 2026-11-23
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. It takes two cells on the same row or the same column and answers: are all the cells strictly between them empty? A loop walks from the smaller coordinate plus one to the larger coordinate minus one and stops at the first filled cell. If the two cells are next to each other, the loop has nothing to visit and the answer is yes.
The board is a plain array of booleans, one per cell, true when a tile is there. A second small helper, filled, returns false for any coordinate outside the grid. That detail is what makes the outside border work later.
Case 0 and case 1: straight line and L shape
Case 0 is one call: if A and B share a row or column and clear says yes, the path is just the two points.
Case 1 tries both corners of the rectangle that A and B span. For A at row 1, column 1 and B at row 3, column 4, the corners are row 1, column 4 and row 3, column 1. For each corner, three things must hold:
- the corner cell itself is empty,
- the segment from A to the corner is clear,
- the segment from the corner to B is clear.
The corner is checked separately because clear ignores both end cells. Forget that check and you get paths that pass through a tile.
Case 2: Z and U shapes by scanning lines
Two turns means a middle segment, either vertical or horizontal, that both tiles can reach with a straight run. So the code scans candidate lines.
For vertical middle lines it loops over every column c. It skips the columns of A and B, because those would collapse into the earlier cases. For each c it checks that the two cells in that column, on A’s row and on B’s row, are empty, then that three segments are clear: A to its cell, that cell to the other one, and that one to B. The horizontal version does the same with rows.
A small table of what changes between the two scans:
- Vertical scan: columns from minus one to the column count, corners on A’s row and B’s row.
- Horizontal scan: rows from minus one to the row count, corners on A’s column and B’s column.
- Both scans keep the shortest path found, measured by total number of cells walked.
Why not a general search? A breadth-first search that tracks the number of turns would also work and would handle any limit. For a limit of two it is more machinery than needed. It needs a queue, a visited set that remembers direction and turn count, and care to avoid revisiting the same state. The scan version has no state, allocates nothing beyond the result, and each branch can be read and checked by eye. It also returns the corner points directly, which the game uses to draw the line on screen. When the limit is fixed and small, explicit cases are easier to trust than a clever general routine.
The empty frame around the board
Look at the scan range: it starts at minus one and ends one past the last row or column. That is a frame of empty cells around the grid. Because filled treats everything outside the grid as empty, a path may leave the board, run along the outside and come back in.
This is why two tiles on the top row can always join if nothing blocks the way to the edge. It also explains a test in the project: on a three-by-four board, tiles at the two ends of the top row with a blocker between them still connect. The path goes up into the frame, along the top, and down again, two turns in total.
The frame is one cell thick. A path never needs more, because any longer detour outside the grid can be replaced by one that hugs the edge.
A worked example. A sits at row 0, column 0 and B at row 0, column 3, with a blocker at column 1. Straight fails, both L corners are occupied, and the column scan finds nothing. The row scan reaches row minus one and finds A, then up, then along, then down to B: 1 plus 3 plus 1, five cells. Row 1 gives the same length, but the code keeps the first path it found, so the frame wins the tie.
Cost and edge cases
How much work is one check? The scans try about columns plus rows plus four candidate lines, and each candidate costs at most three straight runs of up to a board side. On the largest Connect board, sixteen columns by nine rows, that is under thirty candidates with short loops. A tap does not feel it.
The expensive part is asking the question for many pairs, for a hint, for a stuck check or for a shuffle. The code groups tiles by class first and only tests pairs inside a class. Spread evenly over thirty classes, a full sixteen-by-nine board has about three hundred such pairs instead of more than ten thousand. That is an estimate, not a measurement.
Edge cases worth a test:
- The same tile twice: return no path.
- Two neighbours: the straight case answers yes at once.
- A corner cell that is occupied: reject it before checking the segments.
- A fully packed board: inner tiles with no free line to the frame have no path, even when they are close.
Testing it and using it
The tests are small and concrete. One builds a three-by-four board and checks the straight case, then adds a blocker and checks that a path with at most two turns still appears. Another fills a five-by-five board completely and confirms that the centre tile and the tile at row 1, column 1 have no path. A third plays all 150 levels through the generator and requires at least one legal move in each.
The generator uses the same function backwards. It starts from a full board, repeatedly picks two tiles the path finder says can connect, removes them, applies gravity where the level has it, and records the order. That order is a solution, so the level is solvable by construction.
Tilelings uses this finder for its Connect mode, which has 150 levels. If you are building a similar puzzle, write the straight, L and scan cases as three short blocks, keep one clear helper, and test the frame early. A last tip: print the corner list for a few hand-made boards and compare it with a drawing on paper. A wrong corner shows up immediately as a line through a tile.
Frequently asked questions
What does at most two turns mean?
The path has at most three straight segments, so it can be a straight line, an L shape or a Z or U shape.
Can the path go outside the board?
In this implementation yes, through a one-cell empty frame around the grid. Anything beyond one cell is never needed.
Why are the two tiles not counted as blockers?
The path starts and ends on them, so the line check looks only at the cells strictly between the endpoints. The corner cells are checked separately.
How do you test a path finder like this?
With small hand-built boards for each shape, a fully packed board that must fail, and a generator test that checks every level has a move.
More from the blog
Play Billing 8.0.0 Crash: “This Stopwatch Is Already Running” and the Fix
Mahjong Solitaire History and How It Differs From Mahjong
How We Guarantee a Mahjong Solitaire Board Is Solvable
Solving Every Gravity Puzzle Level Before a Player Sees It
Same Seed, Same Level on Android and iOS in Kotlin Multiplatform- Measuring Play Installs: UTM Links and Listing Experiments