;================================================================
; mahjong/mahjong_hand 麻将游戏 手牌算法相关
; get_hand_info.erb 获取手牌信息
;================================================================


;;;根据座位获取手牌代码
;;;- (option)int seat 座位（默认为玩家座位）
;;;@return 手牌代码
@mj_get_hand_code(seat=-1)
#FUNCTIONS
    #DIM seat
    #DIM DYNAMIC idx
    #DIMS DYNAMIC hand_code
    SIF seat < 0 || seat >= MJ_PLAYER_COUNT
        seat = MJ_PLAYER_SEAT
    FOR idx, 0, mj_count_hand(seat)
        hand_code += mj_to_code(MJ_HAND:seat:idx)
    NEXT
    RETURNF hand_code


;;;获取某家的手牌数量
;;;- int seat 座次
;;;@return 手牌数量
@mj_count_hand(seat)
#FUNCTION
    #DIM seat
    #DIM DYNAMIC idx
    #DIM DYNAMIC hand_count
    FOR idx, 0, VARSIZE("MJ_HAND", 1)
        SIF mj_to_code(MJ_HAND:seat:idx) != ""
            hand_count += 1
    NEXT
    RETURNF hand_count


;;;获取给定的一副手牌的麻将牌数量
;;;- str hand_str 手牌字符串
;;;@return 手牌数量
@mj_count_hand_from(hand_str)
#FUNCTION
    #DIMS hand_str
    #DIM DYNAMIC i
    #DIM DYNAMIC hand_count
    SIF STRLENSU(hand_str) < 34
        hand_str '= mj_map_hand_to_count(hand_str)
    FOR i, 0, 34
        hand_count += TOINT(SUBSTRINGU(hand_str, i, 1))
    NEXT
    RETURNF hand_count


;;;获取对子数量
;;;- str hand_str 手牌字符串
;;;@return 对子数量
@mj_count_toitsu(hand_str)
#FUNCTION
    #DIMS hand_str
    #DIMS DYNAMIC count_map_str
    #DIM DYNAMIC i
    #DIM DYNAMIC toitsu_count
    SIF STRLENSU(hand_str) < 34
        hand_str '= mj_map_hand_to_count(hand_str)
    FOR i, 0, 34
        toitsu_count += TOINT(SUBSTRINGU(hand_str, i, 1)) / 2  ; 这个写法兼容龙七对（因为 4 / 2 = 2）
    NEXT
    RETURNF toitsu_count


;;;获取幺九牌数量
;;;- str hand_str 手牌字符串
;;;@return 幺九牌数量
@mj_count_yaochuu(hand_str)
#FUNCTION
    #DIMS hand_str
    #DIM CONST YaochuuIndex, 13 = 0, 8, 9, 17, 18, 26, 27, 28, 29, 30, 31, 32, 33
    #DIMS DYNAMIC count_map_str
    #DIM DYNAMIC i
    #DIM DYNAMIC yaochuu_count
    SIF STRLENSU(hand_str) < 34
        hand_str '= mj_map_hand_to_count(hand_str)
    FOR i, 0, VARSIZE("YaochuuIndex")
        yaochuu_count += TOINT(SUBSTRINGU(hand_str, YaochuuIndex:i, 1)) > 0
    NEXT
    RETURNF yaochuu_count


;;;获取所有可能的雀头的位置
;;;- str count_map_str 手牌数量映射数组
;;;@return 雀头可能存在的位置(字符串形式)
@mj_get_jantou(count_map_str)
#FUNCTIONS
    #DIMS count_map_str
    #DIM DYNAMIC i
    #DIMS DYNAMIC jantou
    FOR i, 0, STRLENSU(count_map_str)
        SIF TOINT(SUBSTRINGU(count_map_str, i, 1)) >= 2
            jantou += (jantou == "" ? "" # ",") + TOSTR(i)
    NEXT
    RETURNF jantou
