Skip to content

Commit

Permalink
refactor(plugin25): refactor possibleCardMoves and player cloning uti…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
xeruf committed Jul 3, 2024
1 parent 1f1d3e2 commit 975426a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
14 changes: 7 additions & 7 deletions plugin2025/src/main/kotlin/sc/plugin2025/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Move> = getSensibleMoves(currentPlayer)

Expand All @@ -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()
Expand All @@ -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<Advance>? {
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<Advance>? {
val state = clonePlayer(team) { it.advanceBy(distance) }
return state.nextCards(state.getHare(team))?.map { cards ->
Advance(distance, *cards)
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin2025/src/test/kotlin/sc/plugin2025/MoveTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 975426a

Please sign in to comment.