Skip to content

Commit

Permalink
feat(plugin25): implement WinCondition
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Jun 26, 2024
1 parent e7f8d43 commit 6adbccb
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
5 changes: 5 additions & 0 deletions plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import sc.api.plugins.*
import sc.plugin2025.GameRuleLogic.calculateCarrots
import sc.plugin2025.GameRuleLogic.calculateMoveableFields
import sc.plugin2025.util.HuIConstants
import sc.plugin2025.util.HuIWinReason
import sc.shared.InvalidMoveException
import sc.shared.WinCondition
import kotlin.math.pow
import kotlin.math.sqrt

Expand Down Expand Up @@ -66,6 +68,9 @@ data class GameState @JvmOverloads constructor(
override val isOver: Boolean
get() = players.any { it.inGoal } && turn.mod(2) == 0 || round >= HuIConstants.ROUND_LIMIT

override val winCondition: WinCondition?
get() = players.singleOrNull { it.inGoal }?.team?.let { WinCondition(it, HuIWinReason.GOAL) }

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

Expand Down
4 changes: 4 additions & 0 deletions plugin2025/src/test/kotlin/sc/plugin2025/GameStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.*
import sc.api.plugins.Team
import sc.helpers.shouldSerializeTo
import sc.plugin2025.util.HuIWinReason
import sc.shared.InvalidMoveException
import sc.shared.WinCondition

class GameStateTest: FunSpec({
test("clone correctly") {
Expand Down Expand Up @@ -48,6 +50,7 @@ class GameStateTest: FunSpec({
state.performMoveDirectly(EatSalad)
state.turn shouldBe 2
state.isOver shouldBe true
state.winCondition shouldBe WinCondition(Team.TWO, HuIWinReason.GOAL)
}
test("tie") {
state.currentPlayer.run {
Expand All @@ -58,6 +61,7 @@ class GameStateTest: FunSpec({
state.performMoveDirectly(Advance(2))
state.turn shouldBe 2
state.isOver shouldBe true
state.winCondition shouldBe null
}
}
test("produce nice XML") {
Expand Down
3 changes: 1 addition & 2 deletions sdk/src/main/server-api/sc/api/plugins/IGameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ interface IGameState: RoomMessage, PublicCloneable<IGameState> {
val isOver: Boolean

/** Falls es einen klaren Sieger anhand der Spielregeln unabhängig von der Punktzahl gibt.
* Wenn dieser Wert nicht null ist, sollte [isOver] true zurückgeben. */
* Wenn dieser Wert nicht null ist, muss [isOver] true zurückgeben. */
val winCondition: WinCondition?
get() = null

/** Gibt Punktzahlen des Teams passend zur ScoreDefinition des aktuellen Spielplugins zurück. */
fun getPointsForTeam(team: ITeam): IntArray
Expand Down

0 comments on commit 6adbccb

Please sign in to comment.