Skip to content

Commit 502d01b

Browse files
author
Xerus
committed
fix(plugin): clone lastMove & expand GameState clone test
fixes #235
1 parent 127d2f6 commit 502d01b

File tree

3 files changed

+11
-8
lines changed

3 files changed

+11
-8
lines changed

plugin/src/shared/sc/plugin2020/GameState.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class GameState @JvmOverloads constructor(
1616
override var board: Board = Board(),
1717
turn: Int = 0,
1818
private val undeployedRedPieces: MutableList<Piece> = parsePiecesString(Constants.STARTING_PIECES, PlayerColor.RED),
19-
private val undeployedBluePieces: MutableList<Piece> = parsePiecesString(Constants.STARTING_PIECES, PlayerColor.BLUE)
19+
private val undeployedBluePieces: MutableList<Piece> = parsePiecesString(Constants.STARTING_PIECES, PlayerColor.BLUE),
20+
override var lastMove: Move? = null
2021
): TwoPlayerGameState<Player>() {
2122

2223
@XStreamOmitField
@@ -33,8 +34,6 @@ class GameState @JvmOverloads constructor(
3334
currentPlayerColor = currentPlayerFromTurn()
3435
}
3536

36-
override var lastMove: Move? = null
37-
3837
val gameStats: Array<IntArray>
3938
get() = PlayerColor.values().map { getPlayerStats(it) }.toTypedArray()
4039

@@ -44,7 +43,7 @@ class GameState @JvmOverloads constructor(
4443
}
4544

4645
/** Copy constructor to create a new deeply copied state from the given [state]. */
47-
constructor(state: GameState): this(state.red.clone(), state.blue.clone(), state.board.clone(), state.turn, ArrayList(state.undeployedRedPieces), ArrayList(state.undeployedBluePieces))
46+
constructor(state: GameState): this(state.red.clone(), state.blue.clone(), state.board.clone(), state.turn, ArrayList(state.undeployedRedPieces), ArrayList(state.undeployedBluePieces), state.lastMove)
4847

4948
/** Creates a deep copy of this [GameState]. */
5049
public override fun clone() = GameState(this)

plugin/src/test/sc/plugin2020/CloneTest.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@ class CloneTest: StringSpec({
2929
clone shouldBe state
3030
clone shouldNotBeSameInstanceAs state
3131
clone.getDeployedPieces(PlayerColor.RED) shouldBe state.getDeployedPieces(PlayerColor.RED)
32+
clone.currentPlayerColor shouldBe state.currentPlayerColor
33+
clone.currentPlayer shouldNotBeSameInstanceAs state.currentPlayer
34+
clone.lastMove shouldBe state.lastMove
35+
clone.board shouldBe state.board
3236
}
3337
})

plugin/src/test/sc/plugin2020/GamePlayTest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -859,10 +859,6 @@ class GamePlayTest {
859859
val xstream = Configuration.xStream
860860
val xml = """
861861
|<state startPlayerColor="RED" currentPlayerColor="BLUE" turn="3">
862-
| <lastMove class="setmove">
863-
| <piece owner="BLUE" type="GRASSHOPPER"/>
864-
| <destination x="-2" y="4" z="-2"/>
865-
| </lastMove>
866862
| <red color="RED" displayName=""/>
867863
| <blue color="BLUE" displayName="aBluePlayer"/>
868864
| <board>
@@ -1038,6 +1034,10 @@ class GamePlayTest {
10381034
| <piece owner="BLUE" type="ANT"/>
10391035
| <piece owner="BLUE" type="ANT"/>
10401036
| </undeployedBluePieces>
1037+
| <lastMove class="setmove">
1038+
| <piece owner="BLUE" type="GRASSHOPPER"/>
1039+
| <destination x="-2" y="4" z="-2"/>
1040+
| </lastMove>
10411041
|</state>""".trimMargin()
10421042
assertEquals(xml, xstream.toXML(state))
10431043
val fromXml = xstream.fromXML(xml) as GameState

0 commit comments

Comments
 (0)