Example #1
0
def maj_cal(data):
    """{
        "tehai":{
            "man":"111",
            "pin":"222",
            "sou":"333",
            "honors":"11166"
        },
        "agari":{
            "man":"1",
            "pin":"",
            "sou":"",
            "honors":""
        },
        "wind":{
            "round":"1",
            "player":"1",
        },
        "dora":
            {"man":"",
            "pin":"1",
            "sou":"",
            "honors":""
            },
        "kan":[
            {"man":"",
            "pin":"",
            "sou":"",
            "honors":"",
            "open":""
            }
        ],
        "pon":[
            {"man":"",
            "pin":"",
            "sou":"",
            "honors":""
            }
        ],
        "chi":[
            {"man":"",
            "pin":"",
            "sou":"",
            "honors":""
            }
        ],
        "option":[]
    }"""
    print(data)
    has_aka_dora = False
    for d_i, d in data.items():
        if d_i != "option" and has_aka_dora == False:
            if type(d) == type({}):
                for i, v in d.items():
                    if i != "open" and "r" in v:
                        has_aka_dora = True
                        break
            elif type(d) == type([]):
                for d2 in d:
                    for i, v in d2.items():
                        # print("v",v)
                        if i != "open" and "r" in v:
                            has_aka_dora = True
                            break

    handconfig = HandConfig(
        options=OptionalRules(has_open_tanyao=True, has_aka_dora=has_aka_dora))
    melds = []
    dora_indicators = []
    yaku_opened = False
    if "wind" in data.keys():
        handconfig.round_wind = WINDS[int(data["wind"]["round"]) - 1]
        handconfig.player_wind = WINDS[int(data["wind"]["player"]) - 1]
        handconfig.is_dealer = handconfig.player_wind == WINDS[0]

    if "kan" in data.keys() and len(data["kan"]) > 0:
        for kan in data["kan"]:
            opened = (kan["open"] in {"1", 1})
            melds.append(
                Meld(meld_type=Meld.KAN,
                     tiles=to_136_array(obj=kan),
                     opened=opened))
            if opened:
                yaku_opened = opened

    if "pon" in data.keys() and len(data["pon"]) > 0:
        for pon in data["pon"]:
            melds.append(Meld(meld_type=Meld.PON, tiles=to_136_array(obj=pon)))
            yaku_opened = True

    if "chi" in data.keys() and len(data["chi"]) > 0:
        for chi in data["chi"]:
            #配列内をソート
            for chi_key in chi.keys():
                if chi[chi_key] != "":
                    r_flag = False
                    if "r" in chi[chi_key]:
                        r_flag = True
                        chi[chi_key] = chi[chi_key].replace("r", "5")

                    tmp = chi[chi_key]
                    tmp = sorted([tmp[0], tmp[1], tmp[2]])
                    chi[chi_key] = "".join(tmp)
                    if r_flag:
                        chi[chi_key] = chi[chi_key].replace("5", "r")
                    print(chi[chi_key])

            melds.append(Meld(meld_type=Meld.CHI, tiles=to_136_array(obj=chi)))
            yaku_opened = True

    if "dora" in data.keys():
        dora_indicators = to_136_array(obj=data["dora"])

    if "option" in data.keys():
        for i in data["option"]:
            if i in ["tsumo", "ツモ", "つも"]:
                handconfig.is_tsumo = True
            elif i in ["riichi", "リーチ", "りーち"]:
                handconfig.is_riichi = True
            elif i in ["rinshan", "嶺上", "りんしゃん", "リンシャン"]:
                handconfig.is_rinshan = True
            elif i in ["ippatsu", "一発", "いっぱつ", "イッパツ"]:
                handconfig.is_ippatsu = True
            elif i in ["chankan", "槍槓", "ちゃんかん", "チャンカン"]:
                handconfig.is_chankan = True
            elif i in ["haitei", "海底", "はいてい", "ハイテイ"]:
                handconfig.is_haitei = True
            elif i in ["houtei", "河底", "ほうてい", "ホウテイ"]:
                handconfig.is_houtei = True
            elif i in ["daburu_riichi", "だぶりー", "ダブリー", "だぶるりーち", "ダブルリーチ"]:
                handconfig.is_daburu_riichi = True
            elif i in ["nagashi_mangan", "流しマンガン", "ナガシマンガン", "ながしまんがん"]:
                handconfig.is_nagashi_mangan = True
            elif i in ["tenhou", "テンホウ", "てんほう"]:
                handconfig.is_tenhou = True
            elif i in ["renhou", "レンホウ", "れんほう"]:
                handconfig.is_renhou = True
            elif i in ["chiihou", "ちいほう", "チイホウ"]:
                handconfig.is_chiihou = True

    import json
    #print(json.dumps(data["tehai"], indent=4))
    #print(json.dumps(data["agari"], indent=4))

    tiles = to_136_array(data["tehai"], has_aka_dora=has_aka_dora)
    win_tile = to_136_array(data["agari"], has_aka_dora=has_aka_dora)[0]

    if len(dora_indicators) == []: dora_indicators = None
    if len(melds) == 0: melds = None
    result = calculator.estimate_hand_value(tiles,
                                            win_tile,
                                            melds=melds,
                                            dora_indicators=dora_indicators,
                                            config=handconfig)
    return print_hand_result(result, opened=yaku_opened)
Example #2
0
def maj_cal(te_hai):
    #te_hai = input("てはい>>") #p:1 s:1 m:1 j:東南西北撥白中 a:p1 b:東 k:東 tumo ron kan:1111
    tehai_list = {"man":None,"pin":None,"sou":None,"honors":None}
    agarihai_list = {"man":None,"pin":None,"sou":None,"honors":None}
    handconfig = HandConfig(options=OptionalRules(has_open_tanyao=True,has_aka_dora=True))
    melds = []
    dora_indicators = []
    for i in te_hai.split():
        if ":" in i:
            tmp = i.split(":")

            if tmp[0] == "p": #ピンズの手牌設定 p:123456789r
                tehai_list["pin"] = tmp[1]
            elif tmp[0] == "s": #ソウズの手牌設定 s:123456789r
                tehai_list["sou"] = tmp[1]
            elif tmp[0] == "m": #マンズの手牌設定 m:123456789r
                tehai_list["man"] = tmp[1]
            elif tmp[0] == "j": #字牌の手牌設定 j:123456789r
                tehai_list["honors"] = to_honors(tmp[1])

            elif tmp[0] == "a": #上がり牌 a:p1
                if tmp[1][0] == "p":
                    agarihai_list["pin"] = tmp[1][1]
                elif tmp[1][0] == "s":
                    agarihai_list["sou"] = tmp[1][1]
                elif tmp[1][0] == "m":
                    agarihai_list["man"] = tmp[1][1]
                elif tmp[1][0] == "j":
                    agarihai_list["honors"] = to_honors(tmp[1][1])
            
            elif tmp[0] == "b": #場風 j:東
                handconfig.round_wind=WINDS[int(to_honors(tmp[1]))-1]

            elif tmp[0] == "k": #自風 k:東
                handconfig.player_wind=WINDS[int(to_honors(tmp[1]))-1]

            elif tmp[0] == "kan": #カン kan:s1111n nのありなしで鳴きを判断
                if tmp[1][0] == "p": kan_tiles = to_136_array(pin=tmp[1][1:5])
                elif tmp[1][0] == "s": kan_tiles = to_136_array(sou=tmp[1][1:5])
                elif tmp[1][0] == "m": kan_tiles = to_136_array(man=tmp[1][1:5])
                elif tmp[1][0] == "j": kan_tiles = to_136_array(honors=to_honors(tmp[1][1:5]))
                if tmp[1][-1] == "n":
                    melds.append(Meld(meld_type=Meld.KAN, tiles=kan_tiles, opened=True))
                else:
                    melds.append(Meld(meld_type=Meld.KAN, tiles=kan_tiles, opened=False))

            # elif tmp[0] == "chankan": #チャンカン
            #     if tmp[1][0] == "p": chankan_tiles = to_136_array(pin=tmp[1][1:5])
            #     elif tmp[1][0] == "s": chankan_tiles = to_136_array(sou=tmp[1][1:5])
            #     elif tmp[1][0] == "m": chankan_tiles = to_136_array(man=tmp[1][1:5])
            #     elif tmp[1][0] == "j": chankan_tiles = to_136_array(honors=to_honors(tmp[1][1:5]))
            #     melds.append(Meld(meld_type=Meld.CHANKAN, tiles=chankan_tiles))

            elif tmp[0] == "pon": #ポン pon:s111
                if tmp[1][0] == "p": pon_tiles = to_136_array(pin=tmp[1][1:4])
                elif tmp[1][0] == "s": pon_tiles = to_136_array(sou=tmp[1][1:4])
                elif tmp[1][0] == "m": pon_tiles = to_136_array(man=tmp[1][1:4])
                elif tmp[1][0] == "j": pon_tiles = to_136_array(honors=to_honors(tmp[1][1:4]))
                melds.append(Meld(meld_type=Meld.PON, tiles=pon_tiles))

            elif tmp[0] == "chi": #チー chi:s111
                if tmp[1][0] == "p": chi_tiles = to_136_array(pin=tmp[1][1:4])
                elif tmp[1][0] == "s": chi_tiles = to_136_array(sou=tmp[1][1:4])
                elif tmp[1][0] == "m": chi_tiles = to_136_array(man=tmp[1][1:4])
                elif tmp[1][0] == "j": chi_tiles = to_136_array(honors=to_honors(tmp[1][1:4]))
                melds.append(Meld(meld_type=Meld.CHI, tiles=chi_tiles))

            elif tmp[0] == "dora": #ドラ dora:s1s1s1
                for dora_i in range(0,len(tmp[1]),2):
                    if tmp[1][dora_i] == "p": dora_tiles = to_136_array(pin=tmp[1][dora_i+1])
                    elif tmp[1][dora_i] == "s": dora_tiles = to_136_array(sou=tmp[1][dora_i+1])
                    elif tmp[1][dora_i] == "m": dora_tiles = to_136_array(man=tmp[1][dora_i+1])
                    elif tmp[1][dora_i] == "j": dora_tiles = to_136_array(honors=to_honors(tmp[1][dora_i+1]))
                    dora_indicators.append(dora_tiles[0])

                
        else:
            if i in ["tumo", "ツモ", "つも"]:
                handconfig.is_tsumo =True
            elif i in ["riichi", "リーチ", "りーち"]:
                handconfig.is_riichi =True
            elif i in ["rinshan", "嶺上", "りんしゃん", "リンシャン"]:
                handconfig.is_rinshan=True
            elif i in ["ippatsu", "一発", "いっぱつ", "イッパツ"]:
                handconfig.is_ippatsu=True
            elif i in ["chankan", "槍槓", "ちゃんかん", "チャンカン"]:
                handconfig.is_chankan=True
            elif i in ["haitei", "海底", "はいてい", "ハイテイ"]:
                handconfig.is_haitei=True
            elif i in ["houtei", "河底", "ほうてい", "ホウテイ"]:
                handconfig.is_houtei=True
            elif i in ["だぶりー", "ダブリー", "だぶるりーち", "ダブルリーチ"]:
                handconfig.is_daburu_riichi=True
            elif i in ["流しマンガン", "ナガシマンガン", "ながしまんがん"]:
                handconfig.is_nagashi_mangan=True
            elif i in ["テンホウ", "てんほう"]:
                handconfig.is_tenhou=True
            elif i in ["レンホウ", "れんほう"]:
                handconfig.is_renhou=True
            elif i in ["ちいほう", "チイホウ"]:
                handconfig.is_chiihou=True

    import json
    print(json.dumps(tehai_list, indent=4))
    print(json.dumps(agarihai_list, indent=4))

    tiles = to_136_array(tehai_list, has_aka_dora=True)
    win_tile = to_136_array(agarihai_list, has_aka_dora=True)[0]

    if len(dora_indicators) == []: dora_indicators = None
    if len(melds) == 0: melds = None
    result = calculator.estimate_hand_value(tiles, win_tile, melds=melds, dora_indicators=dora_indicators, config=handconfig)
    return print_hand_result(result)