Skip to content

Commit

Permalink
番符计算结果切换为ULong
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttkkl committed Jan 6, 2024
1 parent f594052 commit fc7ac44
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 120 deletions.
66 changes: 66 additions & 0 deletions mahjong-utils/src/commonMain/kotlin/mahjongutils/hanhu/Models.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package mahjongutils.hanhu

import kotlinx.serialization.Serializable


interface Point {
/**
* 荣和点数
*/
val ron: ULong

/**
* 自摸总点数
*/
val tsumoTotal: ULong
}

/**
* 亲家(庄家)和牌点数
*/
@Serializable
data class ParentPoint(
override val ron: ULong,
/**
* 自摸各家点数
*/
val tsumo: ULong
) : Point {
override val tsumoTotal: ULong
get() = tsumo * 3uL

companion object {
val Mangan = ParentPoint(12000uL, 4000uL)
val Haneman = ParentPoint(18000uL, 6000uL)
val Baiman = ParentPoint(24000uL, 8000uL)
val Sanbaiman = ParentPoint(36000uL, 12000uL)
val Yakuman = ParentPoint(48000uL, 16000uL)
}
}

/**
* 子家(闲家)和牌点数
*/
@Serializable
data class ChildPoint(
override val ron: ULong,
/**
* 自摸亲家(庄家)点数
*/
val tsumoParent: ULong,
/**
* 自摸子家(闲家)点数
*/
val tsumoChild: ULong
) : Point {
override val tsumoTotal: ULong
get() = tsumoParent + tsumoChild * 2uL

companion object {
val Mangan = ChildPoint(8000uL, 4000uL, 2000uL)
val Haneman = ChildPoint(12000uL, 6000uL, 3000uL)
val Baiman = ChildPoint(16000uL, 8000uL, 4000uL)
val Sanbaiman = ChildPoint(24000uL, 12000uL, 6000uL)
val Yakuman = ChildPoint(32000uL, 16000uL, 8000uL)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package mahjongutils.hanhu

import kotlinx.serialization.Serializable

private fun ceil100(x: Int): Int {
return if (x % 100 > 0) {
x + (100 - x % 100)
private fun ceil100(x: ULong): ULong {
return if (x % 100uL > 0uL) {
x + (100uL - x % 100uL)
} else {
x
}
Expand All @@ -16,87 +14,26 @@ private fun ceil100(x: Int): Int {
// 底符20+门清荣和10+暗杠32*3+单骑2+连风雀头4=132
//private const val MAX_HU = 140

interface Point {
/**
* 荣和点数
*/
val ron: Int

/**
* 自摸总点数
*/
val tsumoTotal: Int
}

/**
* 亲家(庄家)和牌点数
*/
@Serializable
data class ParentPoint(
override val ron: Int,
/**
* 自摸各家点数
*/
val tsumo: Int
) : Point {
override val tsumoTotal: Int
get() = tsumo * 3

companion object {
val Mangan = ParentPoint(12000, 4000)
val Haneman = ParentPoint(18000, 6000)
val Baiman = ParentPoint(24000, 8000)
val Sanbaiman = ParentPoint(36000, 12000)
val Yakuman = ParentPoint(48000, 16000)
}
}

/**
* 子家(闲家)和牌点数
*/
@Serializable
data class ChildPoint(
override val ron: Int,
/**
* 自摸亲家(庄家)点数
*/
val tsumoParent: Int,
/**
* 自摸子家(闲家)点数
*/
val tsumoChild: Int
) : Point {
override val tsumoTotal: Int
get() = tsumoParent + tsumoChild * 2

companion object {
val Mangan = ChildPoint(8000, 4000, 2000)
val Haneman = ChildPoint(12000, 6000, 3000)
val Baiman = ChildPoint(16000, 8000, 4000)
val Sanbaiman = ChildPoint(24000, 12000, 6000)
val Yakuman = ChildPoint(32000, 16000, 8000)
}
}

private fun calcParentPoint(han: Int, hu: Int, aotenjou: Boolean = false): ParentPoint {
var a = hu * (1 shl (han + 2))
if (a > 2000 && !aotenjou) {
a = 2000
var a = hu.toULong() * (1uL shl (han + 2))
if (a > 2000uL && !aotenjou) {
a = 2000uL
}

val ron = ceil100(6 * a)
val tsumo = ceil100(2 * a)
val ron = ceil100(6uL * a)
val tsumo = ceil100(2uL * a)
return ParentPoint(ron, tsumo)
}

private fun calcChildPoint(han: Int, hu: Int, aotenjou: Boolean = false): ChildPoint {
var a = hu * (1 shl (han + 2))
if (a > 2000 && !aotenjou) {
a = 2000
var a = hu.toULong() * (1uL shl (han + 2))
if (a > 2000uL && !aotenjou) {
a = 2000uL
}

val ron = ceil100(4 * a)
val tsumoParent = ceil100(2 * a)
val ron = ceil100(4uL * a)
val tsumoParent = ceil100(2uL * a)
val tsumoChild = ceil100(a)
return ChildPoint(ron, tsumoParent, tsumoChild)
}
Expand Down
16 changes: 8 additions & 8 deletions mahjong-utils/src/commonMain/kotlin/mahjongutils/hora/Hora.kt
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,11 @@ data class Hora internal constructor(
@EncodeDefault
val parentPoint: ParentPoint = run {
if (han == 0) {
ParentPoint(0, 0)
ParentPoint(0uL, 0uL)
} else {
val raw = if (hasYakuman) {
val oneTimeYakuman = getParentPointByHanHu(13, hu, options.hanHuOptions)
val times = han / 13
val times = (han / 13).toULong()
ParentPoint(oneTimeYakuman.ron * times, oneTimeYakuman.tsumo * times)
} else {
getParentPointByHanHu(
Expand All @@ -131,9 +131,9 @@ data class Hora internal constructor(
}

if (tsumo) {
ParentPoint(0, raw.tsumo)
ParentPoint(0uL, raw.tsumo)
} else {
ParentPoint(raw.ron, 0)
ParentPoint(raw.ron, 0uL)
}
}
}
Expand All @@ -144,11 +144,11 @@ data class Hora internal constructor(
@EncodeDefault
val childPoint: ChildPoint = run {
if (han == 0) {
ChildPoint(0, 0, 0)
ChildPoint(0uL, 0uL, 0uL)
} else {
val raw = if (hasYakuman) {
val oneTimeYakuman = getChildPointByHanHu(13, hu, options.hanHuOptions)
val times = han / 13
val times = (han / 13).toULong()
ChildPoint(
oneTimeYakuman.ron * times,
oneTimeYakuman.tsumoParent * times,
Expand All @@ -161,9 +161,9 @@ data class Hora internal constructor(
}

if (tsumo) {
ChildPoint(0, raw.tsumoParent, raw.tsumoChild)
ChildPoint(0uL, raw.tsumoParent, raw.tsumoChild)
} else {
ChildPoint(raw.ron, 0, 0)
ChildPoint(raw.ron, 0uL, 0uL)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import kotlin.test.assertFailsWith
class TestPointByHanHu {
@Test
fun testGetParentPointByHanHu() {
assertEquals(ParentPoint(2900, 1000), getParentPointByHanHu(2, 30))
assertEquals(ParentPoint(2900uL, 1000uL), getParentPointByHanHu(2, 30))
assertEquals(ParentPoint.Mangan, getParentPointByHanHu(3, 70))
assertEquals(ParentPoint(9600, 3200), getParentPointByHanHu(4, 25))
assertEquals(ParentPoint(9600uL, 3200uL), getParentPointByHanHu(4, 25))
assertEquals(ParentPoint.Mangan, getParentPointByHanHu(4, 40))
assertEquals(ParentPoint.Mangan, getParentPointByHanHu(5, 60))
assertEquals(ParentPoint.Haneman, getParentPointByHanHu(6, 30))
Expand Down Expand Up @@ -39,9 +39,9 @@ class TestPointByHanHu {

@Test
fun testGetChildPointByHanHu() {
assertEquals(ChildPoint(2000, 1000, 500), getChildPointByHanHu(2, 30))
assertEquals(ChildPoint(2000uL, 1000uL, 500uL), getChildPointByHanHu(2, 30))
assertEquals(ChildPoint.Mangan, getChildPointByHanHu(3, 70))
assertEquals(ChildPoint(6400, 3200, 1600), getChildPointByHanHu(4, 25))
assertEquals(ChildPoint(6400uL, 3200uL, 1600uL), getChildPointByHanHu(4, 25))
assertEquals(ChildPoint.Mangan, getChildPointByHanHu(4, 40))
assertEquals(ChildPoint.Mangan, getChildPointByHanHu(5, 60))
assertEquals(ChildPoint.Haneman, getChildPointByHanHu(6, 30))
Expand Down Expand Up @@ -98,12 +98,12 @@ class TestPointByHanHu {
@Test
fun testAotenjou() {
assertEquals(
ChildPoint(15400, 7700, 3900),
ChildPoint(15400uL, 7700uL, 3900uL),
getChildPointByHanHu(5, 30, HanHuOptions.Default.copy(aotenjou = true))
)

assertEquals(
ParentPoint(23100, 7700),
ParentPoint(23100uL, 7700uL),
getParentPointByHanHu(5, 30, HanHuOptions.Default.copy(aotenjou = true))
)
}
Expand Down
Loading

0 comments on commit fc7ac44

Please sign in to comment.