From a558cacd02213b3471d2d2821433b183697dab3a Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Wed, 3 Jul 2024 21:53:36 +0300 Subject: [PATCH] fix(plugin25): proper winConditions --- .../src/main/kotlin/sc/plugin2025/GameState.kt | 12 +++++++++++- .../src/test/kotlin/sc/plugin2025/GameStateTest.kt | 5 +++-- sdk/src/main/framework/sc/shared/WinCondition.kt | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt b/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt index fb05c0de1..cda0242fd 100644 --- a/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt +++ b/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt @@ -11,6 +11,7 @@ import sc.plugin2025.util.HuIConstants import sc.plugin2025.util.HuIWinReason import sc.shared.InvalidMoveException import sc.shared.WinCondition +import sc.shared.WinReasonTie import kotlin.math.pow import kotlin.math.sqrt @@ -70,7 +71,16 @@ data class GameState @JvmOverloads constructor( get() = players.any { it.inGoal } && turn.mod(2) == 0 || turn / 2 >= HuIConstants.ROUND_LIMIT override val winCondition: WinCondition? - get() = players.filter { it.inGoal }.maxByNoEqual { -it.carrots }?.team?.let { WinCondition(it, HuIWinReason.DIFFERING_CARROTS) } + get() { + val goalies = players.filter { it.inGoal } + return when(goalies.size) { + 0 -> null + 1 -> WinCondition(goalies.single().team, HuIWinReason.GOAL) + else -> goalies.maxByNoEqual { -it.carrots }?.team?.let { + WinCondition(it, HuIWinReason.DIFFERING_CARROTS) + } ?: WinCondition(null, WinReasonTie) + } + } val Hare.inGoal get() = position == board.size - 1 diff --git a/plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt b/plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt index ea03c074d..64bda54b5 100644 --- a/plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt +++ b/plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt @@ -8,6 +8,7 @@ import sc.helpers.shouldSerializeTo import sc.plugin2025.util.HuIWinReason import sc.shared.InvalidMoveException import sc.shared.WinCondition +import sc.shared.WinReasonTie class GameStateTest: FunSpec({ test("clone correctly") { @@ -20,7 +21,7 @@ class GameStateTest: FunSpec({ test("let lower carrots win on tie") { val state = GameState(Board(arrayOf(Field.GOAL))) state.isOver shouldBe true - state.winCondition shouldBe null + state.winCondition shouldBe WinCondition(null, WinReasonTie) state.players.first().carrots = 5 state.winCondition shouldBe WinCondition(Team.ONE, HuIWinReason.DIFFERING_CARROTS) state.players.last().carrots = 4 @@ -70,7 +71,7 @@ class GameStateTest: FunSpec({ state.performMoveDirectly(Advance(2)) state.turn shouldBe 2 state.isOver shouldBe true - state.winCondition shouldBe null + state.winCondition shouldBe WinCondition(null, WinReasonTie) } test("round limit") { state.turn = 59 diff --git a/sdk/src/main/framework/sc/shared/WinCondition.kt b/sdk/src/main/framework/sc/shared/WinCondition.kt index 0a6da4819..4e056bd79 100644 --- a/sdk/src/main/framework/sc/shared/WinCondition.kt +++ b/sdk/src/main/framework/sc/shared/WinCondition.kt @@ -20,5 +20,8 @@ data class WinCondition( override fun toString(): String = reason.getMessage((if(reason.isRegular) winner else winner?.opponent()).toString()) - override fun equals(other: Any?): Boolean = other is WinCondition && other.winner == winner + override fun equals(other: Any?): Boolean = + other is WinCondition && + other.winner == winner && + other.reason == reason } \ No newline at end of file