Python 3 Script to Build Mahjong Game in Command Line Using Mahjong Library
pip install mahjong
<span class="kn">from</span> <span class="nn">mahjong.hand_calculating.hand</span> <span class="kn">import</span> <span class="n">HandCalculator</span><span class="kn">from</span> <span class="nn">mahjong.tile</span> <span class="kn">import</span> <span class="n">TilesConverter</span><span class="kn">from</span> <span class="nn">mahjong.hand_calculating.hand_config</span> <span class="kn">import</span> <span class="n">HandConfig</span><span class="kn">from</span> <span class="nn">mahjong.meld</span> <span class="kn">import</span> <span class="n">Meld</span> <span class="n">calculator</span> <span class="o">=</span> <span class="n">HandCalculator</span><span class="p">()</span> <span class="c1">
1 401300[Tanyao]{'fu': 30, 'reason': 'base'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 2, 'reason': 'open_pon'}
<span class="n">result</span> <span class="o">=</span> <span class="n">calculator</span><span class="o">.</span><span class="n">estimate_hand_value</span><span class="p">(</span><span class="n">tiles</span><span class="p">,</span> <span class="n">win_tile</span><span class="p">,</span> <span class="n">config</span><span class="o">=</span><span class="n">HandConfig</span><span class="p">(</span><span class="n">is_tsumo</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span> <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">han</span><span class="p">,</span> <span class="n">result</span><span class="o">.</span><span class="n">fu</span><span class="p">)</span><span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">cost</span><span class="p">[</span><span class="s1">'main'</span><span class="p">],</span> <span class="n">result</span><span class="o">.</span><span class="n">cost</span><span class="p">[</span><span class="s1">'additional'</span><span class="p">])</span><span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">yaku</span><span class="p">)</span><span class="k">for</span> <span class="n">fu_item</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">fu_details</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">fu_item</span><span class="p">)</span>
4 404000 2000[Menzen Tsumo, Tanyao, San Ankou]{'fu': 20, 'reason': 'base'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 2, 'reason': 'tsumo'}
<span class="n">melds</span> <span class="o">=</span> <span class="p">[</span><span class="n">Meld</span><span class="p">(</span><span class="n">meld_type</span><span class="o">=</span><span class="n">Meld</span><span class="o">.</span><span class="n">PON</span><span class="p">,</span> <span class="n">tiles</span><span class="o">=</span><span class="n">TilesConverter</span><span class="o">.</span><span class="n">string_to_136_array</span><span class="p">(</span><span class="n">man</span><span class="o">=</span><span class="s1">'444'</span><span class="p">))]</span> <span class="n">result</span> <span class="o">=</span> <span class="n">calculator</span><span class="o">.</span><span class="n">estimate_hand_value</span><span class="p">(</span><span class="n">tiles</span><span class="p">,</span> <span class="n">win_tile</span><span class="p">,</span> <span class="n">melds</span><span class="o">=</span><span class="n">melds</span><span class="p">,</span> <span class="n">config</span><span class="o">=</span><span class="n">HandConfig</span><span class="p">(</span><span class="n">options</span><span class="o">=</span><span class="n">OptionalRules</span><span class="p">(</span><span class="n">has_open_tanyao</span><span class="o">=</span><span class="kc">True</span><span class="p">)))</span> <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">han</span><span class="p">,</span> <span class="n">result</span><span class="o">.</span><span class="n">fu</span><span class="p">)</span><span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">cost</span><span class="p">[</span><span class="s1">'main'</span><span class="p">])</span><span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="o">.</span><span class="n">yaku</span><span class="p">)</span><span class="k">for</span> <span class="n">fu_item</span> <span class="ow">in</span> <span class="n">result</span><span class="o">.</span><span class="n">fu_details</span><span class="p">:</span> <span class="nb">print</span><span class="p">(</span><span class="n">fu_item</span><span class="p">)</span>
1 301000[Tanyao]{'fu': 20, 'reason': 'base'}{'fu': 4, 'reason': 'closed_pon'}{'fu': 2, 'reason': 'open_pon'}{'fu': 2, 'reason': 'open_pon'}
<span class="kn">from</span> <span class="nn">mahjong.shanten</span> <span class="kn">import</span> <span class="n">Shanten</span> <span class="n">shanten</span> <span class="o">=</span> <span class="n">Shanten</span><span class="p">()</span><span class="n">tiles</span> <span class="o">=</span> <span class="n">TilesConverter</span><span class="o">.</span><span class="n">string_to_34_array</span><span class="p">(</span><span class="n">man</span><span class="o">=</span><span class="s1">'13569'</span><span class="p">,</span> <span class="n">pin</span><span class="o">=</span><span class="s1">'123459'</span><span class="p">,</span> <span class="n">sou</span><span class="o"
>=</span><span class="s1">'443'</span><span class="p">)</span><span class="n">result</span> <span class="o">=</span> <span class="n">shanten</span><span class="o">.</span><span class="n">calculate_shanten</span><span class="p">(</span><span class="n">tiles</span><span class="p">)</span> <span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>