Skip to content

Instantly share code, notes, and snippets.

@DanteAndroid
Last active July 6, 2023 14:15
Show Gist options
  • Save DanteAndroid/1f6efc9f8e192c8c9f6e097bfda52ab0 to your computer and use it in GitHub Desktop.
Save DanteAndroid/1f6efc9f8e192c8c9f6e097bfda52ab0 to your computer and use it in GitHub Desktop.
王荣吉占摸牌期望
main()
fun main() {
val testResult = mutableListOf<Int>()
repeat(10000) {
testResult.add(jiZhan())
}
val counts = testResult.groupingBy { it }.eachCount()
var expect = 0f
for ((number, count) in counts) {
val probability = count.toFloat() / testResult.size
expect += number * probability
}
println("Average is ${testResult.sum().toFloat() / testResult.size}, Expectation is $expect")
}
fun generateCards(containMNLM: Boolean = true): MutableList<Int> {
val cards = mutableListOf<Int>()
for (num in 1..13) {
// 每个点数都有3*4张+额外的2点和12点共4张
repeat(12) {
cards.add(num)
}
if (num == 2 || num == 12) {
cards.add(num)
cards.add(num)
}
}
if (containMNLM) {
// 木牛流马的点数是5
cards.add(5)
}
return cards
}
fun jiZhan(): Int {
val cards = generateCards().shuffled()
val resultCards = mutableListOf<Int>()
for (i in cards.indices) {
resultCards.add(cards[i])
val guessBig = makeGuess(cards[i])
if (guessBig && cards[i + 1] > cards[i]) {
continue
} else if (guessBig.not() && cards[i + 1] < cards[i]) {
continue
} else {
resultCards.add(cards[i+1])
// println("Wrong guess of $guessBig. Last card is ${cards[i]}, current card is ${cards[i + 1]}")
break
}
}
// println("Jizhan is over. Got ${resultCards.size} cards.")
return resultCards.size
}
// true表示猜大
fun makeGuess(currentCardNum: Int, threshold: Int = 7): Boolean {
return when (currentCardNum) {
in 1..threshold -> {
true
}
else -> {
false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment