Lesson 1 · Game rules
Make a playable board
Connect two matching chips with a horizontal or vertical wire. It can bend at most twice and can only pass through empty cells between the endpoints. For this practice board, keep the wire inside the grid.
Predict the route
Both A chips are on row 2, in columns 2 and 4. A blocked cell sits between them. Can they still connect?
| 1 | 2 | 3 | 4 | 5 | |
|---|---|---|---|---|---|
| 1 | · | · | · | · | · |
| 2 | · | A | ■ | A | · |
| 3 | · | · | · | · | · |
| 4 | · | · | · | · | · |
Reveal the route
Go up from the first A, across row 1, then down into the second A. Up to right is one bend. Right to down is the second. The open row below works too.
Build it
- Represent the grid as rows of cells. Use a chip type such as
A,nullfor an empty cell, and a separate value for an obstacle. - When a player chooses two cells, check that they are distinct and contain matching chips.
- Search out from the first chip in the four cardinal directions. Track the current cell, the direction you entered from, and how many turns you used. The first move is free. Each direction change adds one turn.
- Skip occupied cells other than the destination, neighbors outside the grid, and moves that would add a third bend. Remember each visited combination of cell, direction, and turn count so the search terminates.
- Return a path when you reach the second chip. Draw the wire and clear the pair only after a valid path comes back.
Check your work
- Accept straight paths and paths with one or two bends.
- Reject a path that needs three bends, a pair of different chips, and a double-click on the same chip.
- Keep the board unchanged when a move is rejected.
Try it on your own
Move the obstacle and add a second one. Make a board where the upper route fails and the lower route still works. Trace the answer before running your code.
This board is a teaching reconstruction of the browser game’s core rule. The original build journal starts with the browser game already running.