Skip to content

Commit a558cac

Browse files
committed
fix(plugin25): proper winConditions
1 parent 22ace6e commit a558cac

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import sc.plugin2025.util.HuIConstants
1111
import sc.plugin2025.util.HuIWinReason
1212
import sc.shared.InvalidMoveException
1313
import sc.shared.WinCondition
14+
import sc.shared.WinReasonTie
1415
import kotlin.math.pow
1516
import kotlin.math.sqrt
1617

@@ -70,7 +71,16 @@ data class GameState @JvmOverloads constructor(
7071
get() = players.any { it.inGoal } && turn.mod(2) == 0 || turn / 2 >= HuIConstants.ROUND_LIMIT
7172

7273
override val winCondition: WinCondition?
73-
get() = players.filter { it.inGoal }.maxByNoEqual { -it.carrots }?.team?.let { WinCondition(it, HuIWinReason.DIFFERING_CARROTS) }
74+
get() {
75+
val goalies = players.filter { it.inGoal }
76+
return when(goalies.size) {
77+
0 -> null
78+
1 -> WinCondition(goalies.single().team, HuIWinReason.GOAL)
79+
else -> goalies.maxByNoEqual { -it.carrots }?.team?.let {
80+
WinCondition(it, HuIWinReason.DIFFERING_CARROTS)
81+
} ?: WinCondition(null, WinReasonTie)
82+
}
83+
}
7484

7585
val Hare.inGoal
7686
get() = position == board.size - 1

plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import sc.helpers.shouldSerializeTo
88
import sc.plugin2025.util.HuIWinReason
99
import sc.shared.InvalidMoveException
1010
import sc.shared.WinCondition
11+
import sc.shared.WinReasonTie
1112

1213
class GameStateTest: FunSpec({
1314
test("clone correctly") {
@@ -20,7 +21,7 @@ class GameStateTest: FunSpec({
2021
test("let lower carrots win on tie") {
2122
val state = GameState(Board(arrayOf(Field.GOAL)))
2223
state.isOver shouldBe true
23-
state.winCondition shouldBe null
24+
state.winCondition shouldBe WinCondition(null, WinReasonTie)
2425
state.players.first().carrots = 5
2526
state.winCondition shouldBe WinCondition(Team.ONE, HuIWinReason.DIFFERING_CARROTS)
2627
state.players.last().carrots = 4
@@ -70,7 +71,7 @@ class GameStateTest: FunSpec({
7071
state.performMoveDirectly(Advance(2))
7172
state.turn shouldBe 2
7273
state.isOver shouldBe true
73-
state.winCondition shouldBe null
74+
state.winCondition shouldBe WinCondition(null, WinReasonTie)
7475
}
7576
test("round limit") {
7677
state.turn = 59

sdk/src/main/framework/sc/shared/WinCondition.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@ data class WinCondition(
2020
override fun toString(): String =
2121
reason.getMessage((if(reason.isRegular) winner else winner?.opponent()).toString())
2222

23-
override fun equals(other: Any?): Boolean = other is WinCondition && other.winner == winner
23+
override fun equals(other: Any?): Boolean =
24+
other is WinCondition &&
25+
other.winner == winner &&
26+
other.reason == reason
2427
}

0 commit comments

Comments
 (0)