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

commonMain.eu.codlab.lorcana.math.Deck.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package eu.codlab.lorcana.math

data class Deck(
    val size: Long,
    val hand: Long
) {
    private var mutableCards = mutableListOf()
    var others = ExpectedCard(
        "others",
        originalAmount = size,
        originalMin = 0,
        originalMax = hand
    )
        private set

    val cards: List
        get() = mutableCards

    fun calculate() = eu.codlab.lorcana.math.calculate(
        size,
        hand,
        others.amount,
        cards
    )

    fun add(name: String): ExpectedCard {
        remove(name)

        return ExpectedCard(name, 0, 0, 0).also {
            mutableCards.add(it)

            updateRemainingCards()
        }
    }

    fun remove(name: String) {
        val index = mutableCards.indexOfFirst { it.name == name }
        if (index < 0) return

        mutableCards.removeAt(index)
    }

    fun update(name: String, amount: Long, min: Long, max: Long): Boolean {
        val holder = mutableCards.firstOrNull { it.name == name } ?: return false

        holder.update(amount, min, max)

        updateRemainingCards()

        println("amount -> $amount / $size")
        println("min -> $min / $hand")
        println("max -> $max / $size")
        println("update done \n\n")
        return amount <= size && min <= hand && max <= size
    }

    private fun updateRemainingCards() {
        val cardsAddedAmount = mutableCards.map { it.amount }.reduce { acc, l -> acc + l }
        val cardsMinAmount = mutableCards.map { it.min }.reduce { acc, l -> acc + l }

        println("cards added amount $cardsAddedAmount")
        println("cards min amount $cardsMinAmount")

        others.update(
            amount = size - cardsAddedAmount,
            min = 0,
            max = hand - cardsMinAmount
        )
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy