All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.mahjongutils.models.hand.HandPattern.kt Maven / Gradle / Ivy

There is a newer version: 0.7.6
Show newest version
package mahjongutils.models.hand

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import mahjongutils.models.*

/**
 * 手牌形
 */
interface HandPattern : IHasFuro {
    /**
     * 手牌(包括门前与副露)
     */
    val tiles: List

    /**
     * 浮牌
     */
    val remaining: List
}

// 单纯是为了sealed
@Serializable
sealed interface CommonHandPattern : HandPattern

/**
 * 以标准形为目标的手牌
 */
interface IRegularHandPattern : HandPattern {
    /**
     * 目标面子组数(=手牌数/4)
     */
    val k: Int

    /**
     * 雀头
     */
    val jyantou: Tile?

    /**
     * 门前面子
     */
    val menzenMentsu: List

    /**
     * 搭子
     */
    val tatsu: List

    override val tiles: List
        get() = buildList {
            jyantou?.let {
                add(it)
                add(it)
            }
            for (mt in menzenMentsu) {
                addAll(mt.tiles)
            }
            for (fr in furo) {
                addAll(fr.asMentsu().tiles)
            }
            for (tt in tatsu) {
                add(tt.first)
                add(tt.second)
            }
            addAll(remaining)
        }

    /**
     * 面子(包括门前与副露)
     */
    val mentsu: List
        get() = menzenMentsu + furo.map { it.asMentsu() }

    /**
     * 暗刻
     */
    val anko: List
        get() = menzenMentsu.filter { it.type == MentsuType.Kotsu } +
                furo.filter { it.type == FuroType.Ankan }.map { it.asMentsu() }
}

/**
 * 以标准形为目标的手牌
 */
@Serializable
@SerialName("RegularHandPattern")
data class RegularHandPattern(
    override val k: Int,
    override val jyantou: Tile?,
    override val menzenMentsu: List,
    override val furo: List,
    override val tatsu: List,
    override val remaining: List,
) : IRegularHandPattern, CommonHandPattern

/**
 * 以七对子为目标的手牌
 */
interface IChitoiHandPattern : HandPattern {
    /**
     * 已有对子
     */
    val pairs: Set

    override val furo: List
        get() = emptyList()

    override val tiles: List
        get() = buildList {
            addAll(pairs)
            addAll(pairs)
            addAll(remaining)
        }
}

/**
 * 以七对子为目标的手牌
 */
@Serializable
@SerialName("ChitoiHandPattern")
data class ChitoiHandPattern(
    override val pairs: Set,
    override val remaining: List
) : IChitoiHandPattern, CommonHandPattern

/**
 * 以国士无双为目标的手牌
 */
interface IKokushiHandPattern : HandPattern {
    /**
     * 幺九牌
     */
    val yaochu: Set

    /**
     * 重复的幺九牌
     */
    val repeated: Tile?

    override val furo: List
        get() = emptyList()

    override val tiles: List
        get() = buildList {
            addAll(yaochu)
            repeated?.let { add(it) }
            addAll(remaining)
        }
}

/**
 * 以国士无双为目标的手牌
 */
@Serializable
@SerialName("KokushiHandPattern")
data class KokushiHandPattern(
    override val yaochu: Set,
    override val repeated: Tile?,
    override val remaining: List
) : IKokushiHandPattern, CommonHandPattern




© 2015 - 2025 Weber Informatics LLC | Privacy Policy