Skip to content

Commit 975426a

Browse files
committed
refactor(plugin25): refactor possibleCardMoves and player cloning utility
1 parent 1f1d3e2 commit 975426a

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,9 @@ data class GameState @JvmOverloads constructor(
7878
override fun clone(): GameState =
7979
copy(players = players.clone())
8080

81-
fun cloneCurrentPlayer(transform: (Hare) -> Unit) =
82-
copy(players = players.map { if(it.team == currentTeam) it.clone().apply(transform) else it })
81+
/** Create a copy of this state with the player matching the given team adjusted. */
82+
fun clonePlayer(team: Team = currentTeam, transform: (Hare) -> Unit = {}) =
83+
copy(players = players.map { if(it.team == team) it.clone().apply(transform) else it })
8384

8485
override fun getSensibleMoves(): List<Move> = getSensibleMoves(currentPlayer)
8586

@@ -89,7 +90,7 @@ data class GameState @JvmOverloads constructor(
8990
return (1..calculateMoveableFields(player.carrots).coerceAtMost(board.size - player.position)).flatMap { distance ->
9091
if(checkAdvance(distance, player) != null)
9192
return@flatMap emptyList()
92-
return@flatMap possibleCardMoves(distance, player) ?: listOf(Advance(distance))
93+
return@flatMap possibleCardMoves(distance, player.team) ?: listOf(Advance(distance))
9394
} + listOfNotNull(
9495
FallBack.takeIf { nextFallBack(player) != null },
9596
*possibleExchangeCarrotMoves(player).toTypedArray()
@@ -98,10 +99,9 @@ data class GameState @JvmOverloads constructor(
9899

99100
/** Possible Advances including buying/playing of cards.
100101
* @return null if target field is neither market nor hare, empty list if no possibilities, otherwise possible Moves */
101-
fun possibleCardMoves(distance: Int, player: Hare = currentPlayer): List<Advance>? {
102-
val state =
103-
copy(players = players.map { if(it.team == player.team) it.clone().apply { advanceBy(distance) } else it })
104-
return state.nextCards(state.getHare(player.team))?.map { cards ->
102+
fun possibleCardMoves(distance: Int, team: Team = currentTeam): List<Advance>? {
103+
val state = clonePlayer(team) { it.advanceBy(distance) }
104+
return state.nextCards(state.getHare(team))?.map { cards ->
105105
Advance(distance, *cards)
106106
}
107107
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ class MoveTest: WordSpec({
9696
Advance(2, Card.FALL_BACK).perform(state.clone()) shouldBe HuIMoveMistake.FIELD_OCCUPIED
9797
state.currentPlayer.position++
9898
Advance(2, Card.FALL_BACK).perform(state.clone()) shouldBe HuIMoveMistake.MUST_BUY_ONE_CARD
99-
state.cloneCurrentPlayer { it.position = 1 }.nextCards() shouldBe Card.values().map { listOf(it) }
100-
state.cloneCurrentPlayer { it.position = 3 }.nextCards() shouldBe Card.values().map { listOf(Card.FALL_BACK, it) }
99+
state.clonePlayer { it.position = 1 }.nextCards() shouldBe Card.values().map { listOf(it) }
100+
state.clonePlayer { it.position = 3 }.nextCards() shouldBe Card.values().map { listOf(Card.FALL_BACK, it) }
101101

102102
state.performMoveDirectly(Advance(2, Card.FALL_BACK, Card.EAT_SALAD))
103103
state.turn shouldBe 2

0 commit comments

Comments
 (0)