Skip to content

Commit

Permalink
fix(plugin25): proper winConditions
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Jul 3, 2024
1 parent 22ace6e commit a558cac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
12 changes: 11 additions & 1 deletion plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
5 changes: 4 additions & 1 deletion sdk/src/main/framework/sc/shared/WinCondition.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit a558cac

Please sign in to comment.