From cbda82827932cd288576ba0320c03615cba9dab7 Mon Sep 17 00:00:00 2001 From: xerus2000 <27jf@pm.me> Date: Thu, 4 Feb 2021 11:08:55 +0100 Subject: [PATCH] fix(plugin): clone GameState validColors --- plugin/src/shared/sc/plugin2021/GameState.kt | 17 +++++++++++------ plugin/src/test/sc/plugin2021/GameStateTest.kt | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugin/src/shared/sc/plugin2021/GameState.kt b/plugin/src/shared/sc/plugin2021/GameState.kt index 40d84bb46..ce487fcb1 100644 --- a/plugin/src/shared/sc/plugin2021/GameState.kt +++ b/plugin/src/shared/sc/plugin2021/GameState.kt @@ -31,17 +31,18 @@ class GameState @JvmOverloads constructor( /** Speichert für jede Farbe, die alle Steine gelegt hat, ob das Monomino zuletzt gelegt wurde. */ @XStreamAsAttribute val lastMoveMono: HashMap = HashMap(), - blueShapes: LinkedHashSet = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)), - yellowShapes: LinkedHashSet = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)), - redShapes: LinkedHashSet = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)), - greenShapes: LinkedHashSet = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)), + blueShapes: LinkedHashSet = PieceShape.values().toLinkedHashSet(), + yellowShapes: LinkedHashSet = PieceShape.values().toLinkedHashSet(), + redShapes: LinkedHashSet = PieceShape.values().toLinkedHashSet(), + greenShapes: LinkedHashSet = PieceShape.values().toLinkedHashSet(), + validColors: LinkedHashSet = Color.values().toLinkedHashSet() ): TwoPlayerGameState(Team.ONE) { companion object { val logger = LoggerFactory.getLogger(GameState::class.java) } - constructor(other: GameState): this(other.first, other.second, other.startPiece, other.board.clone(), other.turn, other.lastMove, HashMap(other.lastMoveMono), LinkedHashSet(other.blueShapes), LinkedHashSet(other.yellowShapes), LinkedHashSet(other.redShapes), LinkedHashSet(other.greenShapes)) + constructor(other: GameState): this(other.first, other.second, other.startPiece, other.board.clone(), other.turn, other.lastMove, HashMap(other.lastMoveMono), LinkedHashSet(other.blueShapes), LinkedHashSet(other.yellowShapes), LinkedHashSet(other.redShapes), LinkedHashSet(other.greenShapes), LinkedHashSet(other.validColors)) private val blueShapes = blueShapes private val yellowShapes = yellowShapes @@ -90,7 +91,7 @@ class GameState @JvmOverloads constructor( get() = orderedColors[turn % Constants.COLORS] /** Liste der Farben, die noch im Spiel sind. */ - private val validColors = Color.values().toCollection(LinkedHashSet(Color.values().size)) + private val validColors = validColors /** Beendet das Spiel, indem alle Farben entfernt werden. */ internal fun clearValidColors() = validColors.clear() @@ -162,6 +163,7 @@ class GameState @JvmOverloads constructor( && yellowShapes == other.yellowShapes && redShapes == other.redShapes && greenShapes == other.greenShapes + && validColors == other.validColors ) } @@ -177,7 +179,10 @@ class GameState @JvmOverloads constructor( result = 31 * result + yellowShapes.hashCode() result = 31 * result + redShapes.hashCode() result = 31 * result + greenShapes.hashCode() + result = 31 * result + validColors.hashCode() return result } } + +fun Array.toLinkedHashSet() = toCollection(LinkedHashSet(size)) \ No newline at end of file diff --git a/plugin/src/test/sc/plugin2021/GameStateTest.kt b/plugin/src/test/sc/plugin2021/GameStateTest.kt index 88e9a058f..0c71ace93 100644 --- a/plugin/src/test/sc/plugin2021/GameStateTest.kt +++ b/plugin/src/test/sc/plugin2021/GameStateTest.kt @@ -74,18 +74,28 @@ class GameStateTest: WordSpec({ "preserve equality" { cloned shouldBe state } - "not be equal when lastMoveMono changes" { + "not equal original when lastMoveMono changed" { cloned shouldBe state cloned.lastMoveMono[Color.RED] = true cloned shouldNotBe state } - "not be equal when undeployedPieces changes" { + "not equal original when undeployedPieces changed" { cloned shouldBe state GameRuleLogic.isFirstMove(cloned) shouldBe true cloned.undeployedPieceShapes().remove(cloned.undeployedPieceShapes().first()) GameRuleLogic.isFirstMove(cloned) shouldBe false cloned shouldNotBe state } + "respect validColors" { + state.removeColor(Color.BLUE) + val newClone = state.clone() + newClone shouldBe state + cloned shouldNotBe state + newClone.removeColor(Color.BLUE) + newClone shouldBe state + newClone.removeColor(Color.RED) + newClone shouldNotBe state + } val otherState = GameState(lastMove = SetMove(Piece(Color.GREEN, 0))) "preserve inequality" { otherState shouldNotBe state