diff --git a/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt b/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt index 24319fe0e..fb05c0de1 100644 --- a/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt +++ b/plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt @@ -78,8 +78,9 @@ data class GameState @JvmOverloads constructor( override fun clone(): GameState = copy(players = players.clone()) - fun cloneCurrentPlayer(transform: (Hare) -> Unit) = - copy(players = players.map { if(it.team == currentTeam) it.clone().apply(transform) else it }) + /** Create a copy of this state with the player matching the given team adjusted. */ + fun clonePlayer(team: Team = currentTeam, transform: (Hare) -> Unit = {}) = + copy(players = players.map { if(it.team == team) it.clone().apply(transform) else it }) override fun getSensibleMoves(): List = getSensibleMoves(currentPlayer) @@ -89,7 +90,7 @@ data class GameState @JvmOverloads constructor( return (1..calculateMoveableFields(player.carrots).coerceAtMost(board.size - player.position)).flatMap { distance -> if(checkAdvance(distance, player) != null) return@flatMap emptyList() - return@flatMap possibleCardMoves(distance, player) ?: listOf(Advance(distance)) + return@flatMap possibleCardMoves(distance, player.team) ?: listOf(Advance(distance)) } + listOfNotNull( FallBack.takeIf { nextFallBack(player) != null }, *possibleExchangeCarrotMoves(player).toTypedArray() @@ -98,10 +99,9 @@ data class GameState @JvmOverloads constructor( /** Possible Advances including buying/playing of cards. * @return null if target field is neither market nor hare, empty list if no possibilities, otherwise possible Moves */ - fun possibleCardMoves(distance: Int, player: Hare = currentPlayer): List? { - val state = - copy(players = players.map { if(it.team == player.team) it.clone().apply { advanceBy(distance) } else it }) - return state.nextCards(state.getHare(player.team))?.map { cards -> + fun possibleCardMoves(distance: Int, team: Team = currentTeam): List? { + val state = clonePlayer(team) { it.advanceBy(distance) } + return state.nextCards(state.getHare(team))?.map { cards -> Advance(distance, *cards) } } diff --git a/plugin2025/src/test/kotlin/sc/plugin2025/MoveTest.kt b/plugin2025/src/test/kotlin/sc/plugin2025/MoveTest.kt index 4f1e44fe9..75e29fa12 100644 --- a/plugin2025/src/test/kotlin/sc/plugin2025/MoveTest.kt +++ b/plugin2025/src/test/kotlin/sc/plugin2025/MoveTest.kt @@ -96,8 +96,8 @@ class MoveTest: WordSpec({ Advance(2, Card.FALL_BACK).perform(state.clone()) shouldBe HuIMoveMistake.FIELD_OCCUPIED state.currentPlayer.position++ Advance(2, Card.FALL_BACK).perform(state.clone()) shouldBe HuIMoveMistake.MUST_BUY_ONE_CARD - state.cloneCurrentPlayer { it.position = 1 }.nextCards() shouldBe Card.values().map { listOf(it) } - state.cloneCurrentPlayer { it.position = 3 }.nextCards() shouldBe Card.values().map { listOf(Card.FALL_BACK, it) } + state.clonePlayer { it.position = 1 }.nextCards() shouldBe Card.values().map { listOf(it) } + state.clonePlayer { it.position = 3 }.nextCards() shouldBe Card.values().map { listOf(Card.FALL_BACK, it) } state.performMoveDirectly(Advance(2, Card.FALL_BACK, Card.EAT_SALAD)) state.turn shouldBe 2