Tic Tac Toe Showdown: Turn-Based Line-Matching Game
Tic Tac Toe Showdown teardown: alternating placement, line detection and a minimax AI on a configurable 3x3 grid, with a playable embedded demo.
Breakdown
- Connect your pieces into a line of the specified length first
- State Flow: boot → menu → gameplay → result; Core Actions: Alternating Turns + Piece Placement + Line Matching + Score Comparison + Decision Making
- Your Task: Take turns with an opponent to place pieces on the grid, aiming to align identical pieces in a row.
- playerCount = 2 (Adjustable 1~4)
- gridRows = 3 (Adjustable 3~30)
- gridCols = 3 (Adjustable 3~30)
- matchSize = 3 (Adjustable 3~5)
- directions = hv
- common-ui: Generic UI Event Binding Module: Automatically binds click events to button nodes in the skin and supports auto-transition from the boot scene to the menu.
- core/board-turn: Generic Turn-Based Board Core: Manages grid board state, piece placement, and win detection (line/chain). Suitable for Tic Tac Toe, Gomoku, Four-in-a-Row, and simplified Go.
- core/board-ai: Generic Board AI: Provides move decisions for board-turn. Current implementation uses minimax, supporting small-scale zero-sum games like Tic Tac Toe.
Key Code
Demo
Embedded playable demo: take turns placing pieces and be the first to complete a line (choose 1 Player to face a minimax AI).
Article
Tic Tac Toe Showdown: Take Turns, First to Connect Wins
The Essence in One Sentence
On the surface, it's a simple clone of Tic-Tac-Toe; in reality, the core mechanism is a turn-based game and win/loss determination under a finite state space. This article focuses on explaining how to decouple board rules from AI strategy using generic modules.
> The Essence in One Sentence: Take turns placing pieces; the first to form a line wins.
How to Play This Game
What you do: Take turns with an opponent to place pieces on the grid cells, aiming to connect identical pieces in a line. Be the first to connect your pieces in a line of the specified length.
Behavior Loop: State flow: boot → menu → gameplay → result; Core behaviors: Take turns + Place piece + Match line + Compare values + Make choice
Only Three Rules, But Deep Enough
- Take turns
- Place piece
- Match line
- Compare values
- Make choice
The charm of a turn-based board game lies in: extremely simple rules, but every move requires thinking about "If I play here, where does the opponent play next."
| Parameter | Value in This Game | Adjustable Range | |---|---|---| | playerCount | 2 | 1~4 | | gridRows | 3 | 3~30 | | gridCols | 3 | 3~30 | | matchSize | 3 | 3~5 | | directions | hv | - | | threshold | 0 | - |
How It's Built
This game consists of 3 public modules:
- common-ui: Generic UI event binding module: Automatically binds click events to button nodes in the skin, and supports automatically advancing from the boot scene to the menu.
- core/board-turn: Generic turn-based board core: Manages grid board state, piece placement, and win/loss determination (lines/connects). Suitable for Tic-Tac-Toe, Gomoku, Four-in-a-Row, simplified Go, and other board games.
- core/board-ai: Generic board AI: Provides piece placement decisions for board-turn. Current implementation uses minimax, supporting small-scale zero-sum board games like Tic-Tac-Toe.
> The same set of modules with different rules becomes a different game—that's the meaning of modularity.
Key Code
core/board-turn · checkWinner()
> Real implementation from module core/board-turn
core/board-ai · checkWinner()
> Real implementation from module core/board-ai
Design Highlights
- 3×3 small board: Finite state space, opening strategy can be exhaustively searched
- Connect 3 pieces to win: Balance between offense and blocking
- AI strength = Opponent model: Three experience levels: Random / Blocking / Calculated kill
What You Can Learn
- Minimum state machine for board games (boot → menu → gameplay → result)
- How to write win/loss determination as a pure function (easy to test, easy to reuse)
- AI opponent is essentially a choice of "opponent model" strength
Edge Cases
- Need to handle the issue where board size exceeds 3×3, causing state space explosion and making AI exhaustive search impossible
- Need to handle invalid input validation for out-of-bounds player move coordinates or duplicate moves
- Need to handle draw/end state judgment when the board is full without any connected lines
- Need to handle the decision boundary for AI when it cannot force a win, choosing to block the opponent or move randomly
- Need to handle whether player state reset is clean after the game ends to avoid residual data
Design Trade-offs
- Benefit: Extremely simple rules and finite state space; Cost: Insufficient strategy depth; Alternative: Adjust board size or line length to increase complexity
- Benefit: Win/loss determination written as a pure function is easy to test and reuse; Cost: AI strength completely depends on the opponent model; Alternative: Introduce more complex heuristic algorithms or evaluation functions
You may also like

This article deconstructs the layered mechanism for grid-based movement and collision detection, built upon grid coordinates and physics tags. It details the push-box displacement logic and blockage checks using an occupancy table and reachability model, providing a direct technical path from rule definition to code implementation to help readers master core physics interaction principles.
Tic-tac-toe is a solved game — optimal play always ends in a draw. See how 3D rotation, sound and confetti rebuild the feel without changing a single rule.
A bubble-shooter teardown: aim angle folds into a column pick, 8-way pops raise density, and a ceiling drop every 10 shots makes pressure countable.