The domain layer contains the core logic of the Mahjong engine, independent of any external protocols or IO. It defines the foundational primitives (tiles, seats, winds), the state machine that governs a Mahjong round, and the analytical services used to evaluate hands and calculate legal moves.
The engine represents the Mahjong world using several key value types.
Tile is a value type containing an int8 ID [internal/domain/game/tile/tile.go:99-101]. It provides methods for color/number extraction [internal/domain/game/tile/tile.go:148-154], red-five manipulation [internal/domain/game/tile/tile.go:202-226], and dora progression logic via NextForDora [internal/domain/game/tile/tile.go:198-200].Seat type represents the four player positions (0-3). The Wind type represents the four directions (East, South, West, North) [internal/domain/game/wind/wind.go:7-14]. Winds can be incremented using the Next() method to follow the turn order [internal/domain/game/wind/wind.go:46-59].For details, see Tile, Wind, and Seat Primitives.
The following diagram bridges natural language concepts to the specific code entities used in the domain layer.
| Concept | Code Entity | File Path |
|---|---|---|
| Tile Type | tile.Tile | [internal/domain/game/tile/tile.go:99] |
| Player Position | seat.Seat | [internal/domain/game/seat/seat.go:5] |
| Round Direction | wind.Wind | [internal/domain/game/wind/wind.go:7] |
| Standard Tile Count | NumTileType34 | [internal/domain/game/tile/tile.go:8] |
| Dora Indicator Map | doraIndicatorToDora | [internal/domain/game/tile/tile.go:90] |
Sources:
The engine distinguishes between Events (things that happened in the game) and Actions (decisions made by players).
event.Event interface. These represent the "truth" of the game history as broadcast by the server, such as Draw, Discard, Dora, and EndRound.action.Action interface. These are intent-based decisions sent by the player to the server, such as a Discard action containing the actor's seat and whether the tile was a tsumogiri.For details, see Events and Actions.
The round.State struct is the heart of the engine. It manages the lifecycle of a single Mahjong round. It uses an EventApplier to transition state based on incoming events, ensuring that the internal representation of the wall, rivers, and player hands remains synchronized with the game server.
Chii is only called from the player to the left).VisiblePlayer (what everyone can see) and InvisiblePlayer (private information like the player's own hand).For details, see Round State Machine.
The diagram below illustrates how the round.State interacts with events to progress the game.
Sources:
To support AI decision-making, the domain layer provides high-level viewers and analysis services.
Turn() or the number of Dora indicators.round.State.shanten (distance to a winning hand), identifying yaku (scoring patterns), and calculating final scores (Fu/Han).For details, see:
Sources: