Skip to content

Commit a921144

Browse files
author
Xerus
committed
fix(plugin): add proper GameState & Board clone functions
1 parent 9afaee1 commit a921144

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ data class Board(
2929
}
3030

3131
constructor(fields: Collection<Field>): this(gameFieldFromFields(fields))
32-
33-
public override fun clone() = Board(fields)
32+
33+
/** Copy constructor to create a new deeply copied state from the given [board]. */
34+
constructor(board: Board): this(board.fields)
35+
36+
/** Creates a deep copy of this [Board]. */
37+
public override fun clone() = Board(this)
3438

3539
private fun generateObstructed() {
3640
val all = this.fields
@@ -87,12 +91,7 @@ data class Board(
8791
override fun equals(other: Any?): Boolean {
8892
if(this === other) return true
8993
if(javaClass != other?.javaClass) return false
90-
91-
other as Board
92-
93-
if(!gameField.contentDeepEquals(other.gameField)) return false
94-
95-
return true
94+
return gameField.contentDeepEquals((other as Board).gameField)
9695
}
9796

9897
override fun hashCode(): Int =

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ data class GameState @JvmOverloads constructor(
1919
override var turn: Int = 0,
2020
private val undeployedRedPieces: MutableList<Piece> = parsePiecesString(Constants.STARTING_PIECES, PlayerColor.RED),
2121
private val undeployedBluePieces: MutableList<Piece> = parsePiecesString(Constants.STARTING_PIECES, PlayerColor.BLUE)
22-
): TwoPlayerGameState<Player, IMove>(), Cloneable {
22+
): TwoPlayerGameState<Player, IMove>() {
2323

2424
@XStreamOmitField
2525
private var allPieces: Collection<Piece> = undeployedBluePieces + undeployedRedPieces + board.getPieces()
@@ -32,6 +32,12 @@ data class GameState @JvmOverloads constructor(
3232
return this
3333
}
3434

35+
/** Copy constructor to create a new deeply copied state from the given [state]. */
36+
constructor(state: GameState): this(state.red.clone(), state.blue.clone(), state.board.clone(), state.turn, ArrayList(state.undeployedRedPieces), ArrayList(state.undeployedBluePieces))
37+
38+
/** Creates a deep copy of this [GameState]. */
39+
public override fun clone() = GameState(this)
40+
3541
fun getUndeployedPieces(owner: PlayerColor): MutableList<Piece> {
3642
return when(owner) {
3743
PlayerColor.RED -> undeployedRedPieces
@@ -40,9 +46,9 @@ data class GameState @JvmOverloads constructor(
4046
}
4147

4248
fun getDeployedPieces(owner: PlayerColor): List<Piece> {
43-
val ownerPieces = allPieces.filterTo(ArrayList()) { it.owner == owner }
44-
getUndeployedPieces(owner).forEach { ownerPieces.remove(it) }
45-
return ownerPieces
49+
val ownedPieces = allPieces.filterTo(ArrayList()) { it.owner == owner }
50+
getUndeployedPieces(owner).forEach { ownedPieces.remove(it) }
51+
return ownedPieces
4652
}
4753

4854
fun addPlayer(player: Player) {

0 commit comments

Comments
 (0)