Skip to content

Commit

Permalink
fix(plugin): clone GameState validColors
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Feb 4, 2021
1 parent 010f077 commit cbda828
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
17 changes: 11 additions & 6 deletions plugin/src/shared/sc/plugin2021/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color, Boolean> = HashMap(),
blueShapes: LinkedHashSet<PieceShape> = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)),
yellowShapes: LinkedHashSet<PieceShape> = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)),
redShapes: LinkedHashSet<PieceShape> = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)),
greenShapes: LinkedHashSet<PieceShape> = PieceShape.values().toCollection(LinkedHashSet(PieceShape.values().size)),
blueShapes: LinkedHashSet<PieceShape> = PieceShape.values().toLinkedHashSet(),
yellowShapes: LinkedHashSet<PieceShape> = PieceShape.values().toLinkedHashSet(),
redShapes: LinkedHashSet<PieceShape> = PieceShape.values().toLinkedHashSet(),
greenShapes: LinkedHashSet<PieceShape> = PieceShape.values().toLinkedHashSet(),
validColors: LinkedHashSet<Color> = Color.values().toLinkedHashSet()
): TwoPlayerGameState<Player>(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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -162,6 +163,7 @@ class GameState @JvmOverloads constructor(
&& yellowShapes == other.yellowShapes
&& redShapes == other.redShapes
&& greenShapes == other.greenShapes
&& validColors == other.validColors
)
}

Expand All @@ -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 <T> Array<T>.toLinkedHashSet() = toCollection(LinkedHashSet(size))
14 changes: 12 additions & 2 deletions plugin/src/test/sc/plugin2021/GameStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cbda828

Please sign in to comment.