Skip to content

Commit 655c0d6

Browse files
committed
fix(plugin): add proper cloning of boards fields
1 parent 4c7a1a7 commit 655c0d6

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ data class Board(
124124
throw IndexOutOfBoundsException()
125125
x = f.coordinates.x + SHIFT
126126
y = f.coordinates.y + SHIFT
127-
gameField[x][y] = f
127+
gameField[x][y] = f.clone()
128128
}
129129
return gameField
130130
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class Field(
6161

6262
constructor(field: Field): this(field.x, field.y, field.z, field.pieces.toCollection(Stack()), field.isObstructed)
6363

64+
public override fun clone() = Field(this)
65+
6466
override fun equals(other: Any?): Boolean {
6567
if(this === other) return true
6668
if(javaClass != other?.javaClass)
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package sc.plugin2020
22

3+
import io.kotlintest.matchers.types.shouldNotBeSameInstanceAs
34
import io.kotlintest.shouldBe
45
import io.kotlintest.specs.StringSpec
56
import sc.framework.plugins.Player
@@ -8,16 +9,25 @@ import sc.shared.PlayerColor
89
class CloneTest: StringSpec({
910
"clone Player" {
1011
val player = Player(PlayerColor.RED, "aPlayer")
11-
player.clone() shouldBe player
12+
val clone = player.clone()
13+
clone shouldBe player
14+
clone shouldNotBeSameInstanceAs player
1215
}
1316
"clone Board" {
1417
val board = Board()
15-
board.clone() shouldBe board
18+
board.getField(0, 0, 0).pieces.add(Piece(PlayerColor.RED, PieceType.BEETLE))
19+
val clone = board.clone()
20+
clone shouldBe board
21+
clone shouldNotBeSameInstanceAs board
22+
clone.getField(0, 0, 0) shouldNotBeSameInstanceAs board.getField(0, 0, 0)
23+
// note that the individual pieces are immutable and don't need to be cloned, only the stack which holds them
24+
clone.getField(0, 0, 0).pieces shouldNotBeSameInstanceAs board.getField(0, 0, 0).pieces
1625
}
1726
"clone GameState" {
1827
val state = GameState(blue = Player(PlayerColor.BLUE, "aBluePlayer"), turn = 5)
1928
val clone = state.clone()
2029
clone shouldBe state
30+
clone shouldNotBeSameInstanceAs state
2131
clone.getDeployedPieces(PlayerColor.RED) shouldBe state.getDeployedPieces(PlayerColor.RED)
2232
}
2333
})

0 commit comments

Comments
 (0)