from mahjong.hand_calculating.hand import HandCalculator
from mahjong.meld import Meld
from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules
from mahjong.shanten import Shanten
from mahjong.tile import TilesConverter

calculator = HandCalculator()


# useful helper
def print_hand_result(hand_result):
    print(hand_result.han, hand_result.fu)
    print(hand_result.cost['main'])
    print(hand_result.yaku)
    for fu_item in hand_result.fu_details:
        print(fu_item)
    print('')


####################################################################
# Tanyao hand by ron                                               #
####################################################################


# we had to use all 14 tiles in that array
tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444')
win_tile = TilesConverter.string_to_136_array(sou='4')[0]

result = calculator.estimate_hand_value(tiles, win_tile)
print_hand_result(result)


####################################################################
# Tanyao hand by tsumo                                             #
####################################################################


result = calculator.estimate_hand_value(tiles, win_tile, config=HandConfig(is_tsumo=True))
print_hand_result(result)


####################################################################
# Add open set to hand                                             #
####################################################################


melds = [Meld(meld_type=Meld.PON, tiles=TilesConverter.string_to_136_array(man='444'))]

result = calculator.estimate_hand_value(tiles, win_tile, melds=melds, config=HandConfig(options=OptionalRules(has_open_tanyao=True)))
print_hand_result(result)


####################################################################
# Shanten calculation                                              #
####################################################################


shanten = Shanten()
tiles = TilesConverter.string_to_34_array(man='13569', pin='123459', sou='443')
result = shanten.calculate_shanten(tiles)

print(result)

####################################################################
# Kazoe as a sanbaiman                                             #
####################################################################


tiles = TilesConverter.string_to_136_array(man='22244466677788')
win_tile = TilesConverter.string_to_136_array(man='7')[0]
melds = [
    Meld(Meld.KAN, TilesConverter.string_to_136_array(man='2222'), False)
]

dora_indicators = [
    TilesConverter.string_to_136_array(man='1')[0],
    TilesConverter.string_to_136_array(man='1')[0],
    TilesConverter.string_to_136_array(man='1')[0],
    TilesConverter.string_to_136_array(man='1')[0],
]

config = HandConfig(is_riichi=True, options=OptionalRules(kazoe=HandConfig.KAZOE_SANBAIMAN))
result = calculator.estimate_hand_value(tiles, win_tile, melds, dora_indicators, config)
print_hand_result(result)


####################################################################
# Change the cost of yaku                                          #
####################################################################


config = HandConfig(is_renhou=True)
# renhou as an yakuman - old style
config.yaku.renhou.han_closed = 13

tiles = TilesConverter.string_to_136_array(man='22444', pin='333567', sou='444')
win_tile = TilesConverter.string_to_136_array(sou='4')[0]

result = calculator.estimate_hand_value(tiles, win_tile, config=config)
print_hand_result(result)

[((252, 268), 'mahjong.hand_calculating.hand.HandCalculator', 'HandCalculator', ([], {}), '()\n', (266, 268), False, 'from mahjong.hand_calculating.hand import HandCalculator\n'), ((772, 844), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""22444"""', 'pin': '"""333567"""', 'sou': '"""444"""'}), "(man='22444', pin='333567', sou='444')\n", (806, 844), False, 'from mahjong.tile import TilesConverter\n'), ((1996, 2005), 'mahjong.shanten.Shanten', 'Shanten', ([], {}), '()\n', (2003, 2005), False, 'from mahjong.shanten import Shanten\n'), ((2014, 2085), 'mahjong.tile.TilesConverter.string_to_34_array', 'TilesConverter.string_to_34_array', ([], {'man': '"""13569"""', 'pin': '"""123459"""', 'sou': '"""443"""'}), "(man='13569', pin='123459', sou='443')\n", (2047, 2085), False, 'from mahjong.tile import TilesConverter\n'), ((2361, 2417), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""22244466677788"""'}), "(man='22244466677788')\n", (2395, 2417), False, 'from mahjong.tile import TilesConverter\n'), ((3222, 3248), 'mahjong.hand_calculating.hand_config.HandConfig', 'HandConfig', ([], {'is_renhou': '(True)'}), '(is_renhou=True)\n', (3232, 3248), False, 'from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules\n'), ((3328, 3400), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""22444"""', 'pin': '"""333567"""', 'sou': '"""444"""'}), "(man='22444', pin='333567', sou='444')\n", (3362, 3400), False, 'from mahjong.tile import TilesConverter\n'), ((856, 899), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'sou': '"""4"""'}), "(sou='4')\n", (890, 899), False, 'from mahjong.tile import TilesConverter\n'), ((2429, 2472), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""7"""'}), "(man='7')\n", (2463, 2472), False, 'from mahjong.tile import TilesConverter\n'), ((3412, 3455), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'sou': '"""4"""'}), "(sou='4')\n", (3446, 3455), False, 'from mahjong.tile import TilesConverter\n'), ((1262, 1287), 'mahjong.hand_calculating.hand_config.HandConfig', 'HandConfig', ([], {'is_tsumo': '(True)'}), '(is_tsumo=True)\n', (1272, 1287), False, 'from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules\n'), ((2505, 2551), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""2222"""'}), "(man='2222')\n", (2539, 2551), False, 'from mahjong.tile import TilesConverter\n'), ((2587, 2630), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""1"""'}), "(man='1')\n", (2621, 2630), False, 'from mahjong.tile import TilesConverter\n'), ((2639, 2682), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""1"""'}), "(man='1')\n", (2673, 2682), False, 'from mahjong.tile import TilesConverter\n'), ((2691, 2734), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""1"""'}), "(man='1')\n", (2725, 2734), False, 'from mahjong.tile import TilesConverter\n'), ((2743, 2786), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""1"""'}), "(man='1')\n", (2777, 2786), False, 'from mahjong.tile import TilesConverter\n'), ((2838, 2885), 'mahjong.hand_calculating.hand_config.OptionalRules', 'OptionalRules', ([], {'kazoe': 'HandConfig.KAZOE_SANBAIMAN'}), '(kazoe=HandConfig.KAZOE_SANBAIMAN)\n', (2851, 2885), False, 'from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules\n'), ((1566, 1611), 'mahjong.tile.TilesConverter.string_to_136_array', 'TilesConverter.string_to_136_array', ([], {'man': '"""444"""'}), "(man='444')\n", (1600, 1611), False, 'from mahjong.tile import TilesConverter\n'), ((1711, 1746), 'mahjong.hand_calculating.hand_config.OptionalRules', 'OptionalRules', ([], {'has_open_tanyao': '(True)'}), '(has_open_tanyao=True)\n', (1724, 1746), False, 'from mahjong.hand_calculating.hand_config import HandConfig, OptionalRules\n')]