;================================================================
; mahjong/mahjong_hand 麻将游戏 手牌算法相关
; format_hand.erb 转换手牌格式
;================================================================


;;;预处理手牌代码
;;;- str hand_code 手牌代码
;;;@return 标准格式的手牌代码
@mj_format_hand_code(hand_code)
#FUNCTIONS
    #DIMS hand_code
    #DIM DYNAMIC group_count
    #DIMS DYNAMIC matches_list, 100
    #DIM DYNAMIC count
    #DIM DYNAMIC i
    #DIM DYNAMIC j
    #DIMS DYNAMIC match_res
    count = REGEXPMATCH(hand_code, "(\\d+[mpsz])", group_count, matches_list)
    hand_code '= ""
    FOR i, 0, count
        match_res '= matches_list:(i * group_count + 1)
        FOR j, 0, STRLENSU(match_res) - 1
            hand_code += SUBSTRINGU(match_res, j, 1) + SUBSTRINGU(match_res, STRLENSU(match_res) - 1)
        NEXT
    NEXT
    RETURNF hand_code


;;;将手牌代码转换为数量映射数组
;;;- str hand_code 手牌代码
;;;@return 数量映射数组(字符串形式)
@mj_map_hand_to_count(hand_code)
#FUNCTIONS
    #DIMS hand_code
    #DIM DYNAMIC i
    #DIM DYNAMIC tiles_count_map, 34
    SIF STRLENSU(hand_code) % 6 != 4 && STRLENSU(hand_code) % 6 != 2
        RETURNF 0
    hand_code '= REPLACE(hand_code, "0", "5")
    FOR i, 0, STRLENSU(hand_code) / 2
        tiles_count_map:(FINDELEMENT(MJ_ENUM_TILE_CODE, SUBSTRINGU(hand_code, i*2, 2), 0, , 1)) += 1
    NEXT
    RETURNF STRJOIN(tiles_count_map, "")
