Skip to content

Commit 371668e

Browse files
committed
fix(plugin): correct Piece comparison
Now only the individual blocks' coordinations and color are compared while identity transformations get ignored
1 parent b94e590 commit 371668e

File tree

5 files changed

+43
-11
lines changed

5 files changed

+43
-11
lines changed

plugin/src/shared/sc/plugin2021/Move.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ sealed class Move(val color: Color): IMove {
88

99
class SetMove(val piece: Piece): Move(piece.color) {
1010
override fun toString(): String = piece.toString()
11+
override fun equals(other: Any?): Boolean = piece == other
12+
override fun hashCode(): Int = piece.hashCode()
1113
}
1214

1315
class PassMove(color: Color): Move(color) {

plugin/src/shared/sc/plugin2021/Piece.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ class Piece(@XStreamAsAttribute val color: Color = Color.BLUE,
3737
override fun toString(): String =
3838
"$color Piece $kind:${rotation.value}${if (isFlipped) " (flipped)" else ""} [${position.x},${position.y}]"
3939

40-
override fun equals(other: Any?): Boolean {
41-
return other is Piece &&
42-
other.color == color &&
43-
other.kind == kind &&
44-
other.rotation == rotation &&
45-
other.isFlipped == isFlipped &&
46-
other.position == position
40+
override fun equals(other: Any?): Boolean = when(other) {
41+
is SetMove -> this == other.piece
42+
is Piece -> {
43+
this.color == other.color &&
44+
this.coordinates == other.coordinates
45+
}
46+
else -> false
4747
}
4848

4949
override fun hashCode(): Int {

plugin/src/shared/sc/plugin2021/util/SetHelpers.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package sc.plugin2021.util
22

3-
import sc.plugin2021.Coordinates
4-
import sc.plugin2021.FieldContent
5-
import sc.plugin2021.Rotation
6-
import sc.plugin2021.Vector
3+
import sc.plugin2021.*
4+
5+
/**
6+
* A Collection of methods callable on specific Sets or functions that take Sets as input.
7+
*/
78

89
/** Rotates the given shape based on the given rotation. */
910
fun Set<Coordinates>.rotate(rotation: Rotation): Set<Coordinates> = when(rotation) {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sc.plugin2021
2+
3+
import io.kotlintest.matchers.types.shouldNotBeSameInstanceAs
4+
import io.kotlintest.shouldBe
5+
import io.kotlintest.shouldNotBe
6+
import io.kotlintest.specs.StringSpec
7+
import sc.plugin2021.util.Constants
8+
9+
class ComparisonTest: StringSpec({
10+
"Coordinate comparison" {
11+
Coordinates(3, 2) shouldBe Coordinates(3, 2)
12+
Coordinates(3, 2) shouldNotBeSameInstanceAs Coordinates(3, 2)
13+
Coordinates(3, 2) shouldNotBe Coordinates(2, 3)
14+
Coordinates(3, 2) shouldNotBe Board()
15+
}
16+
"Piece comparison" {
17+
Rotation.values().map {
18+
SetMove(Piece(kind = PieceShape.TETRO_O, rotation = it, isFlipped = true, position = Coordinates(7, 12)))
19+
SetMove(Piece(kind = PieceShape.TETRO_O, rotation = it, isFlipped = false, position = Coordinates(7, 12)))
20+
}.toSet().size shouldBe 1
21+
}
22+
"SetMove comparison" {
23+
Rotation.values().map {
24+
SetMove(Piece(kind = PieceShape.TETRO_O, rotation = it, isFlipped = true, position = Coordinates(7, 12)))
25+
SetMove(Piece(kind = PieceShape.TETRO_O, rotation = it, isFlipped = false, position = Coordinates(7, 12)))
26+
}.toSet().size shouldBe 1
27+
}
28+
})

plugin/src/test/sc/plugin2021/PieceTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,5 @@ class PieceTest: StringSpec({
166166
converted shouldBe it
167167
}
168168
}
169+
169170
})

0 commit comments

Comments
 (0)