### Development Environment Setup Flowchart (mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/7-development-and-testing
Flowchart showing the development environment setup: clone the repository, run `uv sync` to create a virtual environment and install production libraries and dev groups (lint, typing, test), then verify with `uv run pytest`.
```mermaid
graph TD
A["Local Clone"] --> B["uv sync"]
B --> C["Virtual Environment (.venv)"]
C --> D["Install Production Libs"]
C --> E["Install Dev Groups"]
E --> F["lint group (ruff)"]
E --> G["typing group (ty)"]
E --> H["test group (pytest)"]
I["uv run pytest"] --> J["Verify Setup"]
```
--------------------------------
### Install mahjong with pip
Source: https://deepwiki.com/MahjongRepository/mahjong/1-overview
Install the mahjong library using pip. Requires Python 3.10 or higher.
```bash
pip install mahjong
```
--------------------------------
### Usage Example: Configuring HandConfig with OptionalRules
Source: https://deepwiki.com/MahjongRepository/mahjong/5-configuration-and-rules
Shows how to define rule variants and hand context for the HandCalculator. The options include open tanyao, aka dora, and kiriage. The final calculation call is commented out.
```python
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules
# 1. Define rule variants
options = OptionalRules(has_open_tanyao=True, has_aka_dora=True, kiriage=True)
# 2. Define hand context
config = HandConfig(is_riichi=True, is_tsumo=True, options=options)
# 3. Pass to calculator
# result = HandCalculator.estimate_hand_value(tiles, win_tile, config=config)
```
--------------------------------
### HandConfig and OptionalRules Implementation Example
Source: https://deepwiki.com/MahjongRepository/mahjong/5.1-handconfig-and-optionalrules
Python example showing how to create a HandConfig with OptionalRules for custom scoring. If omitted, the library uses a default configuration (Closed Ron, No Riichi, No Dora). The commented line shows how to pass the config to HandCalculator.estimate_hand_value.
```python
from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules
from mahjong.constants import EAST, SOUTH
# Define custom rules
options = OptionalRules(
has_open_tanyao=True,
kiriage=True,
has_aka_dora=True
)
# Define hand context
config = HandConfig(
is_tsumo=True,
is_riichi=True,
player_wind=SOUTH,
round_wind=EAST,
options=options
)
# Use in calculation
# result = HandCalculator.estimate_hand_value(tiles, win_tile, config=config)
```
--------------------------------
### Applying Custom Rules with Kiriage Mangan and Sanbaiman Kazoe Limit
Source: https://deepwiki.com/MahjongRepository/mahjong/5.2-rule-variants-and-customization
Python example showing how to configure custom rules (kiriage=True and kazoe_limit=HandConstants.KAZOE_SANBAIMAN) and calculate a hand value using HandCalculator.estimate_hand_value.
```python
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules
# Define custom rules
options = OptionalRules(
kiriage=True,
kazoe_limit=HandConstants.KAZOE_SANBAIMAN
)
# Apply to hand configuration
config = HandConfig(options=options)
# Execute calculation
result = HandCalculator.estimate_hand_value(tiles, win_tile, config=config)
```
--------------------------------
### Standard shanten calculation with Shanten.calculate_shanten
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Example of calculating shanten for a complete regular hand using Shanten.calculate_shanten and TilesConverter.string_to_34_array. The result is -1 for a complete hand.
```python
from mahjong.shanten import Shanten
from mahjong.tile import TilesConverter
tiles = TilesConverter.string_to_34_array(sou="111234567", pin="11", man="567")
# This is a complete regular hand
shanten = Shanten.calculate_shanten(tiles)
# shanten == -1
```
--------------------------------
### CI Execution Flow Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/7.2-cicd-and-documentation-pipeline
Mermaid diagram showing the CI workflow dependency flow: preparation installs uv, validation runs lint and typecheck jobs, and testing runs a strategy matrix across Python 3.10, Python 3.14, and PyPy 3.11.
```mermaid
graph TD
subgraph "Preparation"
A["Checkout Code"] --> B["Install uv"]
end
subgraph "Validation"
B --> C["Job: lint"]
B --> D["Job: typecheck"]
C --> |"make lint"| C1["Ruff/Linters"]
D --> |"make type"| D1["Mypy/TypeCheck"]
end
subgraph "Testing"
C1 --> E["Job: tests"]
D1 --> E
E --> |"Strategy Matrix"| F["Python 3.10"]
E --> |"Strategy Matrix"| G["Python 3.14"]
E --> |"Strategy Matrix"| H["PyPy 3.11"]
end
```
--------------------------------
### Shanten and Agari check
Source: https://deepwiki.com/MahjongRepository/mahjong/2-getting-started
Use Shanten.calculate_shanten to get the number of tiles away from tenpai (0 is tenpai, -1 is complete). Use Agari.is_agari for a faster structural completeness check (4 melds + 1 pair or special hands).
```python
from mahjong.shanten import Shanten
from mahjong.agari import Agari
# Shanten check
tiles_34 = TilesConverter.string_to_34_array(man='13569', pin='123459', sou='443')
shanten_val = Shanten.calculate_shanten(tiles_34) # Returns 2
# Agari check (Faster than Shanten for completion check)
is_complete = Agari.is_agari(tiles_34) # Returns False
```
--------------------------------
### Error handling for invalid tile count
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Raises ValueError when the tile count is divisible by 3 or exceeds 14, as hands must be 3n+1 or 3n+2 tiles. This example shows an invalid count of 3 tiles.
```python
# Raises ValueError: Invalid tile count = 3
Shanten.calculate_shanten(TilesConverter.string_to_34_array(man="123"))
```
--------------------------------
### Set up development environment with uv
Source: https://deepwiki.com/MahjongRepository/mahjong/1-overview
Clone the repository and set up the development environment using uv. Run tests with uv run pytest.
```bash
git clone https://github.com/MahjongRepository/mahjong.git
cd mahjong
uv sync
uv run pytest
```
--------------------------------
### Build and Release Workflow Sequence Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/7-development-and-testing
Mermaid sequence diagram illustrating the build and release process using uv, from developer running make build-and-release to publishing distributions to PyPI.
```mermaid
sequenceDiagram
participant Dev as Developer
participant Make as Makefile
participant UV as uv build/publish
participant PyPI as PyPI Registry
Dev->>Make: make build-and-release token=...
Make->>Make: build-package
Note over Make: rm -rf build dist mahjong.egg-info
Make->>UV: uv build
UV->>Make: .whl & .tar.gz created
Make->>UV: uv publish --token ...
UV->>PyPI: Upload distributions
```
--------------------------------
### Main Calculation Flow Diagram (Mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Flowchart showing the decision process in `Shanten.calculate_shanten()`, including regular, chiitoitsu, and kokushi calculations.
```mermaid
flowchart TD
START["Shanten.calculate_shanten(tiles_34)"] --> COUNT["sum(tiles_34)"]
COUNT --> REG["_RegularShanten(tiles_34).calculate()"]
COUNT --> CHECK_SPECIAL{"count >= 13?"}
CHECK_SPECIAL -- "Yes" --> CHII_OPT{"use_chiitoitsu?"}
CHECK_SPECIAL -- "No" --> MIN["min(results)"]
CHII_OPT -- "Yes" --> CHII["calculate_shanten_for_chiitoitsu_hand()"]
CHII_OPT -- "No" --> KOKU_OPT{"use_kokushi?"}
CHII --> KOKU_OPT
KOKU_OPT -- "Yes" --> KOKU["calculate_shanten_for_kokushi_hand()"]
KOKU_OPT -- "No" --> MIN
KOKU --> MIN
MIN --> END["Return minimum shanten"]
```
--------------------------------
### Tile Conversion Diagram (Mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/9-glossary
Mermaid diagram illustrating how TilesConverter transforms data between natural language and code entity spaces. Shows the flow from MPSZ strings to physical tiles to tile counts and back.
```mermaid
graph TD
subgraph "Natural Language Space"
A["'123m' (MPSZ String)"]
B["Physical Tiles (Indices 0-135)"]
C["Tile Counts (34 types)"]
end
subgraph "Code Entity Space"
D["TilesConverter.string_to_136_array"]
E["TilesConverter.to_34_array"]
F["TilesConverter.to_136_array"]
end
A --> D
D --> B
B --> E
E --> C
C --> F
F --> B
```
--------------------------------
### Tsumo and Riichi configuration
Source: https://deepwiki.com/MahjongRepository/mahjong/2-getting-started
Pass a HandConfig object to HandCalculator.estimate_hand_value. Enable tsumo with is_tsumo=True, and riichi with is_riichi=True. Use dora_indicators and ura_dora_indicators for Dora and Ura Dora tiles.
```python
from mahjong.hand_calculating.hand_config import HandConfig
# Tsumo (self-draw)
result = HandCalculator.estimate_hand_value(
tiles, win_tile, config=HandConfig(is_tsumo=True)
)
# Riichi with Dora and Ura Dora
dora_indicators = TilesConverter.string_to_136_array(man='1')
ura_dora_indicators = TilesConverter.string_to_136_array(pin='2')
result = HandCalculator.estimate_hand_value(
tiles,
win_tile,
dora_indicators=dora_indicators,
ura_dora_indicators=ura_dora_indicators,
config=HandConfig(is_riichi=True, is_tsumo=True),
)
```
--------------------------------
### Integration with Core Systems: Mermaid Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/5-configuration-and-rules
Mermaid diagram showing how HandConfig options flow into yaku detection, fu calculation, and score calculation. It illustrates the impact of configuration on the calculation pipeline.
```mermaid
graph LR
subgraph "Input"
HC["HandConfig"]
end
subgraph "Core Logic"
YAKU["Yaku Detection"]
FU["Fu Calculation"]
SCORE["Score Calculation"]
end
HC -- "options.has_open_tanyao" --> YAKU
HC -- "options.fu_for_open_pinfu" --> FU
HC -- "options.kiriage / kazoe_limit" --> SCORE
HC -- "is_riichi / is_tsumo" --> YAKU
```
--------------------------------
### Tile format conversion flow (Mermaid diagram)
Source: https://deepwiki.com/MahjongRepository/mahjong/2-getting-started
Mermaid diagram showing how string notation converts to 136-tile and 34-tile arrays, and which components require each format. 136-tile arrays are needed for HandCalculator and Meld Objects; 34-tile arrays are needed for Shanten Calculator and Agari.is_agari.
```mermaid
graph TB
INPUT["'22444m333567p444s'"] -- "string_to_136_array" --> T136["136-tile Array
Individual IDs (0-135)"]
INPUT -- "string_to_34_array" --> T34["34-tile Array
Counts per type (0-8)"]
T136 -- "Required for" --> HC["HandCalculator
(Dora/Aka Dora detection)"]
T136 -- "Required for" --> MELD["Meld Objects"]
T34 -- "Required for" --> SHANTEN["Shanten Calculator"]
T34 -- "Required for" --> AGARI["Agari.is_agari"]
```
--------------------------------
### Documentation Build and Deploy Mermaid Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/7.2-cicd-and-documentation-pipeline
Mermaid graph showing the flow from source code and configuration files through the Sphinx builder to the hosted GitHub Pages site. The diagram includes the upload-pages-artifact step.
```mermaid
graph LR
subgraph "Source Space"
SRC["mahjong/ (Source Code)"]
CONF["docs/source/conf.py"]
IDX["docs/source/index.rst"]
end
subgraph "Processing Space"
SPHINX["Sphinx Builder"]
UV["uv sync --group docs"]
end
subgraph "Deployment Space"
HTML["docs/build/html/"]
GHP["GitHub Pages"]
end
SRC --> SPHINX
CONF --> SPHINX
IDX --> SPHINX
UV --> SPHINX
SPHINX --> HTML
HTML --> |"upload-pages-artifact"| GHP
```
--------------------------------
### Open Hands (Melds) with OptionalRules
Source: https://deepwiki.com/MahjongRepository/mahjong/2-getting-started
Provide a list of Meld objects when the hand is not closed (Menzen). Use OptionalRules to enable features like Open Tanyao (has_open_tanyao=True).
```python
from mahjong.meld import Meld
from mahjong.hand_calculating.hand_config import OptionalRules
melds = [Meld(meld_type=Meld.PON, tiles=TilesConverter.string_to_136_array(man='444'))]
# Use OptionalRules to enable features like Open Tanyao (Kuitan)
result = HandCalculator.estimate_hand_value(
tiles,
win_tile,
melds=melds,
config=HandConfig(options=OptionalRules(has_open_tanyao=True))
)
```
--------------------------------
### CI/CD Pipeline Architecture Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/7-development-and-testing
Mermaid diagram showing the GitHub Actions pipeline: lint, typecheck, and tests jobs run on push to master and pull requests, while documentation build and deploy to GitHub Pages.
```mermaid
graph TD
subgraph "CI Pipeline (lint_and_test.yml)"
L["Lint Job"] --> T["Tests Job"]
TC["Typecheck Job"] --> T
end
subgraph "Documentation (publish_doc.yml)"
D["Build Sphinx Docs"] --> P["Deploy to GitHub Pages"]
end
Trigger["git push master"] --> L
Trigger --> TC
Trigger --> D
```
--------------------------------
### Regular Hand Engine Entity Mapping (Mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Class diagram showing the relationship between `Shanten` and `_RegularShanten`, including the internal fields and methods used for regular hand calculation.
```mermaid
classDiagram
class Shanten {
+calculate_shanten_for_regular_hand(tiles_34)
}
class _RegularShanten {
-list _tiles
-int _min_shanten
-int _number_melds
-int _number_tatsu
-int _number_pairs
+calculate(count_of_tiles)
-_scan(init_mentsu)
-_run(depth)
-_update_result()
}
Shanten ..> _RegularShanten : instantiates
```
--------------------------------
### Architecture diagram (mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/1-overview
Mermaid diagram showing the layered architecture of the mahjong library, with public API classes orchestrating core processing components.
```mermaid
graph TB
subgraph "Public API Layer [mahjong/__init__.py]"
HC["HandCalculator"]
SC["Shanten"]
TC["TilesConverter"]
AG["Agari"]
end
subgraph "Core Processing [mahjong/hand_calculating/]"
HD["HandDivider"]
FC["FuCalculator"]
YS["Yaku (Base Class)"]
SCORES["ScoresCalculator"]
end
subgraph "Configuration [mahjong/hand_calculating/hand_config.py]"
HCONF["HandConfig"]
RULES["OptionalRules"]
end
subgraph "Data Representations"
T136["136-format (int list)"]
T34["34-format (count array)"]
MELD["Meld objects"]
STR["mpsz-notation (string)"]
end
HC --> HD
HC --> FC
HC --> YS
HC --> SCORES
HC --> HCONF
SC --> T34
AG --> T34
TC --> T136
TC --> T34
TC --> STR
HD --> T34
FC --> T136
HCONF --> RULES
```
--------------------------------
### Hand calculation workflow (mermaid)
Source: https://deepwiki.com/MahjongRepository/mahjong/1-overview
Mermaid flowchart illustrating the hand calculation workflow, from input conversion to selecting the highest scoring HandResponse.
```mermaid
flowchart TD
INPUT["Tiles + Win Tile + HandConfig"] --> CONVERT["TilesConverter.string_to_136_array()"]
CONVERT --> DIVIDE["HandDivider.divide_hand()"]
DIVIDE --> COMBINATIONS["Hand Decompositions (4 melds + 1 pair)"]
COMBINATIONS --> EVAL_LOOP["For each decomposition:"]
EVAL_LOOP --> FU_CALC["FuCalculator.calculate_fu()"]
EVAL_LOOP --> YAKU_DETECT["Yaku.is_condition_met() check"]
FU_CALC --> COMBINE["Combine Fu + Han"]
YAKU_DETECT --> COMBINE
COMBINE --> SCORE_CALC["ScoresCalculator.calculate_scores()"]
SCORE_CALC --> SELECT_BEST["Select highest scoring HandResponse"]
SELECT_BEST --> RESULT["HandResponse object"]
```
--------------------------------
### Kazoe Yakuman Limits Mapping (HandConstants)
Source: https://deepwiki.com/MahjongRepository/mahjong/5.1-handconfig-and-optionalrules
Mermaid diagram showing how the three Kazoe Yakuman modes in HandConstants map to scoring outcomes. KAZOE_LIMITED (0) yields 1x Yakuman for 13+ han, KAZOE_SANBAIMAN (1) yields Sanbaiman, and KAZOE_NO_LIMIT (2) yields 1x Yakuman for 13+ han, 2x for 26+, etc.
```mermaid
graph LR
subgraph "HandConstants"
K_LIM["KAZOE_LIMITED (0)"]
K_SAN["KAZOE_SANBAIMAN (1)"]
K_NOL["KAZOE_NO_LIMIT (2)"]
end
subgraph "Scoring Outcome"
OUT1["13+ Han = 1x Yakuman"]
OUT2["13+ Han = Sanbaiman"]
OUT3["13+ Han = 1x Yakuman, 26+ = 2x, etc."]
end
K_LIM --> OUT1
K_SAN --> OUT2
K_NOL --> OUT3
```
--------------------------------
### Rule Configuration Flow Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/5.2-rule-variants-and-customization
Mermaid diagram showing how OptionalRules and HandConfig flow into HandCalculator.estimate_hand_value(), which then uses the Yaku Registry and ScoresCalculator. The diagram highlights that yaku detection checks config.options.has_open_tanyao and score calculation checks config.options.kiriage / kazoe_limit.
```mermaid
graph TD
subgraph "User Input"
OR["OptionalRules(has_open_tanyao, kiriage, etc.)"]
HC["HandConfig(options=OR)"]
end
subgraph "Calculation Pipeline"
CALC["HandCalculator.estimate_hand_value()"]
YAKU["Yaku Registry (yaku_list)"]
SCORE["ScoresCalculator.calculate_scores()"]
end
OR --> HC
HC --> CALC
CALC --> YAKU
CALC --> SCORE
YAKU -- "Checks config.options.has_open_tanyao" --> YAKU
SCORE -- "Checks config.options.kiriage / kazoe_limit" --> SCORE
```
--------------------------------
### Hand Evaluation Data Flow Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/3-core-components
Mermaid flowchart illustrating the hand evaluation data flow. It shows how input tiles, win_tile, and melds are converted by TilesConverter into 136-tile and 34-tile formats, validated by Agari.is_agari, analyzed by HandCalculator.estimate_hand_value and HandDivider.divide_hand, and finally scored by FuCalculator.calculate_fu, Yaku detection, and ScoresCalculator.calculate_scores. The diagram also shows the error path when the hand is not valid.
```mermaid
flowchart TD
INPUT["Input tiles, win_tile, melds"]
subgraph "Format Conversion"
TC["TilesConverter"]
T136["136-tile format"]
T34["34-tile format"]
end
subgraph "Validation Layer"
AG["Agari.is_agari"]
VALID{"Valid Winning Hand?"}
end
subgraph "Hand Analysis"
HC["HandCalculator.estimate_hand_value"]
HD["HandDivider.divide_hand"]
COMBINATIONS["Hand Decompositions"]
end
subgraph "Scoring Pipeline"
FC["FuCalculator.calculate_fu"]
YS["Yaku detection"]
SC["ScoresCalculator.calculate_scores"]
end
OUTPUT["HandResponse"]
ERROR["Error Code"]
%% Main flow
INPUT --> TC
TC --> T136
TC --> T34
T34 --> AG
AG --> VALID
VALID -->|No| ERROR
VALID -->|Yes| HC
T136 --> HC
HC --> HD
HD --> COMBINATIONS
COMBINATIONS --> FC
COMBINATIONS --> YS
FC --> SC
YS --> SC
SC --> OUTPUT
```
--------------------------------
### Mermaid flowchart of recursive search logic
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Flowchart showing the recursive search logic of the _RegularShanten engine, including attempts to form triplets, sequences, pairs, and tatsu, with backtracking steps.
```mermaid
flowchart TD
RUN["_run(depth)"] --> FIND["Find next tile index with count > 0"]
FIND --> TRIPLET["Try forming triplet (_increase_set)"]
TRIPLET --> RUN_RECURSE1["_run(depth)"]
RUN_RECURSE1 --> TRIPLET_BACK["_decrease_set"]
FIND --> SHUNTSU["Try forming sequence (_increase_syuntsu)"]
SHUNTSU --> RUN_RECURSE2["_run(depth)"]
RUN_RECURSE2 --> SHUNTSU_BACK["_decrease_syuntsu"]
FIND --> PAIR["Try forming pair (_increase_pair)"]
PAIR --> RUN_RECURSE3["_run(depth)"]
RUN_RECURSE3 --> PAIR_BACK["_decrease_pair"]
FIND --> TATSU["Try forming tatsu (_increase_tatsu)"]
TATSU --> RUN_RECURSE4["_run(depth)"]
RUN_RECURSE4 --> TATSU_BACK["_decrease_tatsu"]
```
--------------------------------
### Calculate hand value with estimate_hand_value
Source: https://deepwiki.com/MahjongRepository/mahjong/2-getting-started
Use the static `estimate_hand_value` method to calculate the value of a winning hand. Converts string notation to 136-tile arrays using `TilesConverter.string_to_136_array`, then prints han, fu, main cost, and yaku. Ron is the default win type.
```python
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.tile import TilesConverter
# Convert string notation to internal 136-tile format
tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444')
win_tile = TilesConverter.string_to_136_array(sou='4')[0]
# Calculate hand value (Ron by default)
result = HandCalculator.estimate_hand_value(tiles, win_tile)
print(result.han, result.fu)
print(result.cost['main'])
print(result.yaku)
```
--------------------------------
### Aotenjou class diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/4.3-score-calculation
Shows the inheritance relationship between ScoresCalculator and Aotenjou, where Aotenjou overrides calculate_scores to implement the unlimited scoring system.
```mermaid
classDiagram
class ScoresCalculator {
+calculate_scores(han, fu, config, is_yakuman) ScoresResult
}
class Aotenjou {
+calculate_scores(han, fu, config, is_yakuman) ScoresResult
}
ScoresCalculator <|-- Aotenjou : Inheritance
```
--------------------------------
### Mermaid class diagram: Yaku interface and yaku class inheritance
Source: https://deepwiki.com/MahjongRepository/mahjong/8.1-standard-yaku-%281-3-han%29
Mermaid class diagram mapping mahjong yaku concepts to Python classes. Shows the Yaku interface with yaku_id, han_open, han_closed, and is_condition_met, and inheritance from Yaku to Tanyao, Iipeiko, SanshokuDoukou, Sanankou, and Toitoi.
```mermaid
classDiagram
class Yaku {
<>
+yaku_id: int
+han_open: int
+han_closed: int
+is_condition_met(hand, *args) bool
}
class Tanyao {
+yaku_id: 13
}
class Iipeiko {
+yaku_id: 14
}
class SanshokuDoukou {
+yaku_id: 33
}
class Sanankou {
+yaku_id: 31
}
class Toitoi {
+yaku_id: 30
}
Yaku <|-- Tanyao
Yaku <|-- Iipeiko
Yaku <|-- SanshokuDoukou
Yaku <|-- Sanankou
Yaku <|-- Toitoi
```
--------------------------------
### Mermaid diagram: Hand calculation pipeline data flow
Source: https://deepwiki.com/MahjongRepository/mahjong/9-glossary
Mermaid flowchart showing how HandCalculator.estimate_hand_value orchestrates HandDivider, FuCalculator, ScoresCalculator, and Yaku to produce the final score and yaku list. The diagram maps natural language inputs (hand tiles, win tile, rules) to code entities and shows the flow of decompositions to yaku and fu calculations.
```mermaid
graph TD
subgraph "Natural Language Space"
Input["Hand Tiles + Win Tile"]
Config["Rules & Context (Wind, Tsumo/Ron)"]
Result["Final Score & Yaku List"]
end
subgraph "Code Entity Space"
HC["HandCalculator.estimate_hand_value"]
HD["HandDivider.divide_hand"]
FC["FuCalculator.calculate_fu"]
SC["ScoresCalculator.calculate_scores"]
YC["Yaku.is_condition_met"]
end
Input --> HC
Config --> HC
HC --> HD
HD -- "For each decomposition" --> YC
HD -- "For each decomposition" --> FC
YC --> SC
FC --> SC
SC --> Result
```
--------------------------------
### Entity Relationship Diagram for Rule Variants
Source: https://deepwiki.com/MahjongRepository/mahjong/5.2-rule-variants-and-customization
Mermaid class diagram showing the relationships between OptionalRules, HandConfig, ScoresCalculator, and HandCalculator. HandCalculator uses HandConfig, HandConfig contains OptionalRules, and ScoresCalculator reads options from HandConfig.
```mermaid
classDiagram
class OptionalRules {
+bool has_open_tanyao
+bool kiriage
+int kazoe_limit
+bool has_aka_dora
+bool renhou_as_yakuman
}
class HandConfig {
+OptionalRules options
+int tsumi_number
+int kyoutaku_number
}
class ScoresCalculator {
+calculate_scores(han, fu, config)
}
class HandCalculator {
+estimate_hand_value(tiles, win_tile, melds, dora, config)
}
HandCalculator ..> HandConfig : uses
HandConfig *-- OptionalRules : contains
ScoresCalculator ..> HandConfig : reads options
```
--------------------------------
### set_paarenchan_count: Scale Paarenchan Han Dynamically
Source: https://deepwiki.com/MahjongRepository/mahjong/4.4-yakuman-hands
Sets the han value for Paarenchan based on consecutive win count. Each count multiplies the base 13 han, so count=1 gives 13 han, count=2 gives 26 han, etc. Updates both han_open and han_closed to the same value.
```python
def set_paarenchan_count(self, count: int) -> None:
# count=1 -> 13 han, count=2 -> 26 han, etc.
self.han_open = 13 * count
self.han_closed = 13 * count
self.count = count
```
--------------------------------
### Chiitoitsu Shanten Calculation
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Calculates shanten for Chiitoitsu by counting pairs and unique tile kinds. The formula ensures four copies of the same tile do not count as two pairs.
```python
pairs = len([x for x in tiles_34 if x >= 2])
kinds = len([x for x in tiles_34 if x >= 1])
return 6 - pairs + (7 - kinds if kinds < 7 else 0)
```
--------------------------------
### Meld Class Structure Diagram
Source: https://deepwiki.com/MahjongRepository/mahjong/6-utilities-and-helpers
Mermaid diagram showing the Meld class attributes (type, tiles, opened, called_tile) and derived properties (tiles_34, __str__, __repr__). Use to understand the data flow within the Meld class.
```mermaid
graph LR
subgraph "Meld Class"
MTYPE["type: CHI, PON, KAN..."]
MTILES["tiles: tuple (136-format)"]
MOPEN["opened: bool"]
MCALL["called_tile: int"]
end
subgraph "Calculations"
T34["tiles_34 (cached_property)"]
STR["__str__() / __repr__()"]
end
MTILES --> T34
MTYPE --> STR
MTILES --> STR
```
--------------------------------
### Kokushi Musou Shanten Calculation
Source: https://deepwiki.com/MahjongRepository/mahjong/3.2-shanten-calculator
Calculates shanten for Kokushi Musou by checking the presence of the 13 terminal and honor tiles defined in `TERMINAL_AND_HONOR_INDICES`.
```python
completed_terminals = 0
terminals = 0
for i in TERMINAL_AND_HONOR_INDICES:
completed_terminals += tiles_34[i] >= 2
terminals += tiles_34[i] != 0
return 13 - terminals - (1 if completed_terminals else 0)
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.