def check_pinfu(man, pin, sou, honors, player_wind, round_wind, win_tile_type,
                win_tile_value):
    calculator = HandCalculator()

    tiles = TilesConverter.string_to_136_array(man=man,
                                               pin=pin,
                                               sou=sou,
                                               honors=honors)
    print(tiles)
    win_tile = TilesConverter.string_to_136_array(
        **{win_tile_type: win_tile_value})[0]

    config = HandConfig(player_wind=player_wind, round_wind=round_wind)
    result = calculator.estimate_hand_value(tiles, win_tile, config=config)

    if result.yaku is not None:
        for yaku in result.yaku:
            if yaku.name == "Pinfu":
                cost = 1500 if config.is_dealer else 1000
                return [
                    json.dumps({
                        'isPinfu': True,
                        'cost': cost
                    }).encode("utf-8")
                ]

    return [json.dumps({'isPinfu': False, 'cost': 0}).encode("utf-8")]
Exemplo n.º 2
0
async def read_item(winning_tile: str,
                    man_tile: str = None,
                    pin_tile: str = None,
                    sou_tile: str = None):
    calculator = HandCalculator()
    # アガリ形(man=マンズ, pin=ピンズ, sou=ソーズ, honors=字牌)
    tiles = TilesConverter.string_to_136_array(man=man_tile,
                                               pin=pin_tile,
                                               sou=sou_tile)

    # アガリ牌(ソーズの5)
    win_tile = TilesConverter.string_to_136_array(sou=winning_tile)[0]

    # 鳴き(チー:CHI, ポン:PON, カン:KAN(True:ミンカン,False:アンカン), カカン:CHANKAN, ヌキドラ:NUKI)
    melds = None

    # ドラ(なし)
    dora_indicators = None

    # オプション(なし)
    config = HandConfig(is_tsumo=True)

    result = calculator.estimate_hand_value(tiles, win_tile, melds,
                                            dora_indicators, config)

    return {
        "result_main": result.cost['main'],
        "result_additional": result.cost['additional'],
        "yaku": result.yaku
    }
Exemplo n.º 3
0
def main():
    hand = random.choice(questions)
    round_wind, player_wind = get_random_kaze_set()
    tiles = TilesConverter.string_to_136_array(
        man=hand.get_man(), pin=hand.get_pin(), sou=hand.get_sou())
    win_tile = TilesConverter.string_to_136_array(
        **hand.get_win_tile())[0]
    conf = {
        'is_tsumo': get_is_or_not(),
        'is_riichi': get_is_or_not(),
        'player_wind': player_wind,
        'round_wind': round_wind
    }
    config = HandConfig(**conf)
    result = calculator.estimate_hand_value(tiles, win_tile, config=config)

    question_caption = '\n'
    if config.is_tsumo:
        question_caption += f"ツモ:{hand.get_win_tile_figure()} "
    else:
        question_caption += f"ロン:{hand.get_win_tile_figure()} "

    if config.is_riichi:
        question_caption += 'リーチ有 '
    else:
        question_caption += 'リーチ無 '
    question_caption += f"場風: {DISPLAY_WINDS_JP[config.round_wind]} 自風: {DISPLAY_WINDS_JP[config.player_wind]}"

    print(hand.get_figure())
    print(question_caption)
    if config.is_tsumo and config.player_wind == EAST:
        child_answer = int(input('子の支払う点数: '))
        if child_answer == result.cost['main']:
            print('正解!!')
        else:
            print(f"不正解!! 正解は {result.cost['main']} オール")
    elif config.is_tsumo and config.player_wind != EAST:
        parent_answer = int(input('親の支払う点数: '))
        child_answer = int(input('子の支払う点数: '))
        if parent_answer == result.cost['main'] and child_answer == result.cost['additional']:
            print('正解!!')
        else:
            print(
                f"不正解!! 正解は 親: {result.cost['main']}, 子: {result.cost['additional']}")
    else:
        answer = int(input('放銃者の支払う点数: '))
        if answer == result.cost['main']:
            print('正解!!')
        else:
            print(f"不正解!! 正解は {result.cost['main']}")
Exemplo n.º 4
0
 def _string_to_34_tile(self, sou='', pin='', man='', honors=''):
     item = TilesConverter.string_to_136_array(sou=sou,
                                               pin=pin,
                                               man=man,
                                               honors=honors)
     item[0] //= 4
     return item[0]
Exemplo n.º 5
0
    def main(self):
        # read config
        handconfig = self._read_config()

        # ライブラリに渡す用の前処理
        dora_idx = self._piname2index(self.dora_pi)
        win_idx = self._piname2index(self.win_pi)
        man, pin, sou, honors = self._get_list_pinum()
        pi_idx = TilesConverter.string_to_136_array(man=man,
                                                    pin=pin,
                                                    sou=sou,
                                                    honors=honors)
        # 計算
        calculator = HandCalculator()
        melds = None
        result = calculator.estimate_hand_value(pi_idx,
                                                win_idx,
                                                melds=melds,
                                                dora_indicators=[dora_idx],
                                                config=handconfig)
        yaku = result.yaku
        han = result.han
        hu = result.fu
        parent_point = result.cost['main']
        child_point = result.cost['additional']

        return yaku, han, hu, parent_point, child_point
Exemplo n.º 6
0
def string_to_34_tile(sou="", pin="", man="", honors=""):
    item = TilesConverter.string_to_136_array(sou=sou,
                                              pin=pin,
                                              man=man,
                                              honors=honors,
                                              has_aka_dora=True)
    item[0] //= 4
    return item[0]
Exemplo n.º 7
0
    def test_convert_string_to_136_array(self):
        tiles = TilesConverter.string_to_136_array(sou="19",
                                                   pin="19",
                                                   man="19",
                                                   honors="1234567")

        self.assertEqual(
            [0, 32, 36, 68, 72, 104, 108, 112, 116, 120, 124, 128, 132], tiles)
Exemplo n.º 8
0
def meld_4_tiles(boxes, opened=True):
    man, pin, sou, honors = to_136_string(boxes[:4])
    if man is None:
        return None
    # print('man', man, 'pin', pin, 'sou', sou, 'honors', honors)
    tiles = tc.string_to_136_array(man=man, pin=pin, sou=sou, honors=honors)
    # print(tc.to_one_line_string(tiles))
    return Meld(meld_type=Meld.KAN, tiles=tiles, opened=opened)
Exemplo n.º 9
0
    def _piname2index(self, piname: str):
        # モデルで使用した牌の名前を一つ受け取り、136配列のインデックスで返す
        if piname in self.list_man:
            man = int(re.sub(r'\D', '', piname))
            pi = TilesConverter.string_to_136_array(man=[man])
        elif piname in self.list_pin:
            pin = int(re.sub(r'\D', '', piname))
            pi = TilesConverter.string_to_136_array(pin=[pin])
        elif piname in self.list_sou:
            sou = int(re.sub(r'\D', '', piname))
            pi = TilesConverter.string_to_136_array(sou=[sou])
        elif piname in self.list_honors:
            idx = self.dict_honors[piname]
            pi = [idx]
        else:
            raise ValueError('Not supported pi name: {}'.format(piname))

        return pi[0]
Exemplo n.º 10
0
def to_136_array(obj=None,
                 man=None,
                 pin=None,
                 sou=None,
                 honors=None,
                 has_aka_dora=True):
    if obj == None:
        return TilesConverter.string_to_136_array(man=man,
                                                  pin=pin,
                                                  sou=sou,
                                                  honors=honors,
                                                  has_aka_dora=has_aka_dora)
    else:
        return TilesConverter.string_to_136_array(man=obj["man"],
                                                  pin=obj["pin"],
                                                  sou=obj["sou"],
                                                  honors=obj["honors"],
                                                  has_aka_dora=has_aka_dora)
Exemplo n.º 11
0
 def _string_to_open_34_set(self, sou='', pin='', man='', honors=''):
     open_set = TilesConverter.string_to_136_array(sou=sou,
                                                   pin=pin,
                                                   man=man,
                                                   honors=honors)
     open_set[0] //= 4
     open_set[1] //= 4
     open_set[2] //= 4
     return open_set
Exemplo n.º 12
0
    def naku(self, naki_kbn, haistrs):
        """
        なきの場合、対応するMeldオブジェクトを返却する
        :param naki_kbn:
        :param haistrs:
        :return:
        """

        # ポン
        if naki_kbn == 'N':
            meld_type = Meld.PON

        # チー
        elif naki_kbn == 'C':
            meld_type = Meld.CHI

        # カン
        elif naki_kbn == 'K':
            meld_type = Meld.KAN

        man = ""
        pin = ""
        sou = ""
        honors = ""

        for haistr in haistrs:
            buff = buff = haistr
            if buff == '東':
                honors = self.add_hainum_to_tehai(honors, '1')
            elif buff == '南':
                honors = self.add_hainum_to_tehai(honors, '2')
            elif buff == '西':
                honors = self.add_hainum_to_tehai(honors, '3')
            elif buff == '北':
                honors = self.add_hainum_to_tehai(honors, '4')
            elif buff == '白':
                honors = self.add_hainum_to_tehai(honors, '5')
            elif buff == '発':
                honors = self.add_hainum_to_tehai(honors, '6')
            elif buff == '中':
                honors = self.add_hainum_to_tehai(honors, '7')
            elif buff[1] == 'm':
                man = self.add_hainum_to_tehai(man, buff[0])
            elif buff[1] == 'p':
                pin = self.add_hainum_to_tehai(pin, buff[0])
            elif buff[1] == 's':
                sou = self.add_hainum_to_tehai(pin, buff[0])
            else:
                print('[error]不正な文字列が入力されました。')

        tiles = TilesConverter.string_to_136_array(man=man,
                                                   pin=pin,
                                                   sou=sou,
                                                   honors=honors)
        return Meld(meld_type=meld_type, tiles=tiles)
Exemplo n.º 13
0
async def test(item: Item):
    calculator = HandCalculator()
    # アガリ形(man=マンズ, pin=ピンズ, sou=ソーズ, honors=字牌)
    tiles = TilesConverter.string_to_136_array(man=item.man_tile,
                                               pin=item.pin_tile,
                                               sou=item.sou_tile)

    winning_tile = {'man': '', 'pin': '', 'sou': ''}
    winning_tile['man'] = None if item.winning_tile[
        'man'] is None else item.winning_tile['man']
    winning_tile['pin'] = None if item.winning_tile[
        'pin'] is None else item.winning_tile['pin']
    winning_tile['sou'] = None if item.winning_tile[
        'sou'] is None else item.winning_tile['sou']
    print(winning_tile['sou'])
    win_tile = TilesConverter.string_to_136_array(man=winning_tile['man'],
                                                  pin=winning_tile['pin'],
                                                  sou=winning_tile['sou'])[0]

    # 鳴き(チー:CHI, ポン:PON, カン:KAN(True:ミンカン,False:アンカン), カカン:CHANKAN, ヌキドラ:NUKI)
    melds = item.melds if not None else None

    # ドラ(なし)
    dora_indicators = item.dora_indicators if not None else None

    # オプション(なし)
    config = HandConfig(is_tsumo=item.is_tsumo,
                        is_riichi=item.is_riichi) if not None else None

    result = calculator.estimate_hand_value(tiles, win_tile, melds,
                                            dora_indicators, config)

    return {
        "result_main": result.cost['main'],
        "result_additional": result.cost['additional'],
        "yaku": result.yaku
    }
Exemplo n.º 14
0
def meld_3_tiles(boxes, meld_type=None):
    man, pin, sou, honors = to_136_string(boxes[:3])
    if man is None:
        return None
    # print('man', man, 'pin', pin, 'sou', sou, 'honors', honors)
    tiles = tc.string_to_136_array(man=man, pin=pin, sou=sou, honors=honors)
    if meld_type is None:
        if boxes_is_pon(boxes):
            meld_type = Meld.PON
        elif boxes_is_chi(boxes):
            meld_type = Meld.CHI
        else:
            print("error: neither pon nor chi({})".format(boxes))
            return None
    # print(tc.to_one_line_string(tiles))
    return Meld(meld_type=meld_type, tiles=tiles)
Exemplo n.º 15
0
    def test_kokushi_musou_multiple_yakuman(self):
        hand_calculator = HandCalculator()

        # kokushi test

        tiles = TilesConverter.string_to_136_array(sou='19',
                                                   pin='19',
                                                   man='19',
                                                   honors='12345677')
        win_tile = TilesConverter.string_to_136_array(honors='1')[0]

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 1)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 13)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=True,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=True)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        # double kokushi test

        tiles = TilesConverter.string_to_136_array(sou='19',
                                                   pin='19',
                                                   man='19',
                                                   honors='12345677')
        win_tile = TilesConverter.string_to_136_array(honors='7')[0]

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 1)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=True,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 39)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=True)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 39)
Exemplo n.º 16
0
 def _string_to_136_array(self, sou='', pin='', man='', honors=''):
     return TilesConverter.string_to_136_array(sou=sou, pin=pin, man=man, honors=honors)
Exemplo n.º 17
0
def to_tiles(obj):
    return TilesConverter.string_to_136_array(man=obj['man'],
                                              pin=obj['pin'],
                                              sou=obj['sou'],
                                              honors=obj['honors'])
Exemplo n.º 18
0
def boxes_to_tiles(boxes, kan=None):
    man, pin, sou, honors = to_136_string(boxes, kan)
    if man is None:
        return None
    # print('man', man, 'pin', pin, 'sou', sou, 'honors', honors)
    return tc.string_to_136_array(man=man, pin=pin, sou=sou, honors=honors)
Exemplo n.º 19
0
from mahjong.tile import TilesConverter
from mahjong.hand_calculating.hand import HandCalculator

hc = HandCalculator()
tc = TilesConverter()

tiles = tc.string_to_136_array('22444', '333567', '444')
result = hc.estimate_hand_value(tiles, tiles[0], None, None, None)
print(result)
Exemplo n.º 20
0
    def __exit__(self):
        pass


check = ClassCheckMahjongtiles()
check.CheckManzu()
check.CheckPinzu()
check.CheckSozu()
check.CheckZihai()

Manzu, Sozu, Pinzu, Zihai = check.OutputHandConbination()

tiles = TilesConverter.string_to_136_array(man=Manzu,
                                           pin=Pinzu,
                                           sou=Sozu,
                                           honors=Zihai,
                                           has_aka_dora=False)

win_tile = TilesConverter.string_to_136_array(man='2')[0]

melds = None

dora_indicators = None

config = HandConfig(is_tsumo=False,
                    is_ippatsu=False,
                    is_rinshan=False,
                    is_chankan=False,
                    is_haitei=False,
                    is_houtei=False,
Exemplo n.º 21
0
        print(str_dict['error'])
    else:
        print(str_dict['fu_details'])
        print(str_dict['yaku'])
        print(str_dict['cost'])

    print('')


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

# we had to use all 14 tiles in that array
tiles = TilesConverter.string_to_136_array(man='112233',
                                           pin='667788',
                                           sou='44')
win_tile = TilesConverter.string_to_136_array(sou='4')[0]
config = HandConfig()
# config.is_dealer = True
config.is_tsumo = True
result = calculator.estimate_hand_value(tiles, win_tile, config=config)
print_hand_result(result, locale='Chinese')

####################################################################
# Bug: Yakuman and Non-yakuman Yakus should not add together       #
####################################################################

config = HandConfig(is_renhou=True)
# renhou as an yakuman - old style
config.yaku.renhou.han_closed = 13
Exemplo n.º 22
0
 def test_convert_string_with_aka_dora_as_zero_to_136_array(self):
     tiles = TilesConverter.string_to_136_array(man='22444', pin='333067', sou='444', has_aka_dora=True)
     self.assertTrue(FIVE_RED_PIN in tiles)
Exemplo n.º 23
0
 def _string_to_136_tile(self, sou="", pin="", man="", honors=""):
     return TilesConverter.string_to_136_array(sou=sou, pin=pin, man=man, honors=honors)[0]
Exemplo n.º 24
0
 def test_convert_string_with_aka_dora_to_136_array(self):
     tiles = TilesConverter.string_to_136_array(man="22444",
                                                pin="333r67",
                                                sou="444",
                                                has_aka_dora=True)
     self.assertTrue(FIVE_RED_PIN in tiles)
Exemplo n.º 25
0
    def yaku(self, player, is_tsumo, agari_hai=None, chankan=False):
        # 手牌
        tile_strs = [""] * 4
        win_tile_strs = [""] * 4

        for hai in player.tehai.hais:
            tile_strs[hai.kind] += "r" if hai.red else str(hai.num)

        for furo in player.tehai.furos:
            for hai in furo.hais:
                tile_strs[hai.kind] += "r" if hai.red else str(hai.num)

            if furo.kind == FuroKind.ANKAN or furo.kind == FuroKind.MINKAN or furo.kind == FuroKind.KAKAN:
                tile_strs[furo.hais[0].kind] = tile_strs[
                    furo.hais[0].kind][:-1]

        if is_tsumo:
            hai = player.tehai.tsumo_hai
            tile_strs[hai.kind] += "r" if hai.red else str(hai.num)
            win_tile_strs[hai.kind] += "r" if hai.red else str(hai.num)
        else:
            tile_strs[agari_hai.kind] += "r" if agari_hai.red else str(
                agari_hai.num)
            win_tile_strs[agari_hai.kind] += "r" if agari_hai.red else str(
                agari_hai.num)

        tiles = TilesConverter.string_to_136_array(tile_strs[0], tile_strs[1],
                                                   tile_strs[2], tile_strs[3],
                                                   True)
        win_tile = TilesConverter.string_to_136_array(win_tile_strs[0],
                                                      win_tile_strs[1],
                                                      win_tile_strs[2],
                                                      win_tile_strs[3],
                                                      True)[0]

        # 副露
        FURO_MELD = {
            FuroKind.PON: Meld.PON,
            FuroKind.CHI: Meld.CHI,
            FuroKind.ANKAN: Meld.KAN,
            FuroKind.MINKAN: Meld.KAN,
            FuroKind.KAKAN: Meld.CHANKAN
        }

        melds = []

        for furo in player.tehai.furos:
            furo_strs = [""] * 4

            for hai in furo.hais:
                furo_strs[hai.kind] += "r" if hai.red else str(hai.num)

            meld_tiles = TilesConverter.string_to_136_array(
                furo_strs[0], furo_strs[1], furo_strs[2], furo_strs[3], True)
            melds.append(
                Meld(FURO_MELD[furo.kind], meld_tiles,
                     furo.kind != FuroKind.ANKAN))

        # ドラ
        dora_indicators = []

        for hai in self.dora_hais:
            dora_strs = [""] * 4
            dora_strs[hai.kind] += "r" if hai.red else str(hai.num)

            dora_tile = TilesConverter.string_to_136_array(
                dora_strs[0], dora_strs[1], dora_strs[2], dora_strs[3],
                True)[0]
            dora_indicators.append(dora_tile)

        if player.richi:
            for hai in self.uradora_hais:
                dora_strs = [""] * 4
                dora_strs[hai.kind] += "r" if hai.red else str(hai.num)

                dora_tile = TilesConverter.string_to_136_array(
                    dora_strs[0], dora_strs[1], dora_strs[2], dora_strs[3],
                    True)[0]
                dora_indicators.append(dora_tile)

        # 設定
        KAZE_WIND = [EAST, SOUTH, WEST, NORTH]

        config = HandConfig(
            is_tsumo=is_tsumo,
            is_riichi=player.richi,
            is_ippatsu=player.ippatsu,
            is_rinshan=player.rinshan,
            is_chankan=chankan,
            is_haitei=agari_hai is None and self.yama.remain == 0,
            is_houtei=agari_hai is not None and self.yama.remain == 0,
            is_daburu_riichi=len(player.kawa.hais) > 0
            and player.kawa.hais[0].richi,
            is_tenhou=agari_hai is None and self.jikaze(player) == 0
            and len(player.kawa.hais) == 0,
            is_renhou=agari_hai is not None and len(player.kawa.hais) == 0,
            is_chiihou=agari_hai is None and self.jikaze(player) != 0
            and len(player.kawa.hais) == 0,
            player_wind=KAZE_WIND[self.jikaze(player)],
            round_wind=KAZE_WIND[self.bakaze],
            options=OptionalRules(has_aka_dora=True))

        return self.calculator.estimate_hand_value(tiles, win_tile, melds,
                                                   dora_indicators, config)
Exemplo n.º 26
0
from mahjong.hand_calculating.hand import HandCalculator
from mahjong.tile import TilesConverter
from mahjong.hand_calculating.hand_config import HandConfig
from mahjong.meld import Meld

calculator = HandCalculator()

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

result = calculator.estimate_hand_value(tiles, win_tile)

print(result.han, result.fu)
print(result.cost['main'])
print(result.yaku)
for fu_item in result.fu_details:
    print(fu_item)
Exemplo n.º 27
0
    def test_kokushi_musou_multiple_yakuman(self):
        hand_calculator = HandCalculator()

        # kokushi test

        tiles = TilesConverter.string_to_136_array(sou="19",
                                                   pin="19",
                                                   man="19",
                                                   honors="12345677")
        win_tile = TilesConverter.string_to_136_array(honors="1")[0]

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 1)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 13)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=True,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=True)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertTrue(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertFalse(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        # double kokushi test

        tiles = TilesConverter.string_to_136_array(sou="19",
                                                   pin="19",
                                                   man="19",
                                                   honors="12345677")
        win_tile = TilesConverter.string_to_136_array(honors="7")[0]

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 1)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 26)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=True,
                                 is_chiihou=False)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 39)

        hand_config = HandConfig(is_tsumo=True,
                                 is_tenhou=False,
                                 is_chiihou=True)

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 39)

        hand_config = HandConfig(is_tsumo=False,
                                 is_renhou=True,
                                 options=OptionalRules(renhou_as_yakuman=True))

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 2)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.renhou_yakuman in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 39)

        hand_config = HandConfig(
            is_tsumo=False,
            is_renhou=True,
            is_riichi=True,
            is_open_riichi=True,
            options=OptionalRules(renhou_as_yakuman=True,
                                  has_sashikomi_yakuman=True),
        )

        hand_calculation = hand_calculator.estimate_hand_value(
            tiles, win_tile, config=hand_config)

        self.assertIsNone(hand_calculation.error)
        self.assertEqual(len(hand_calculation.yaku), 3)
        self.assertFalse(hand_config.yaku.kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.daburu_kokushi in hand_calculation.yaku)
        self.assertTrue(
            hand_config.yaku.renhou_yakuman in hand_calculation.yaku)
        self.assertTrue(hand_config.yaku.sashikomi in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.tenhou in hand_calculation.yaku)
        self.assertFalse(hand_config.yaku.chiihou in hand_calculation.yaku)
        self.assertEqual(hand_calculation.han, 52)
Exemplo n.º 28
0
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)
Exemplo n.º 29
0
def string_to_136_array(sou="", pin="", man="", honors=""):
    return TilesConverter.string_to_136_array(sou=sou, pin=pin, man=man, honors=honors, has_aka_dora=True)