Skip to content

Commit 59c4e60

Browse files
committed
feat(plugin): make Vector and Move comparable
1 parent 1e163c2 commit 59c4e60

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

plugin/src/main/sc/plugin2022/Coordinates.kt

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,18 @@ package sc.plugin2022
33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55
import sc.plugin2022.util.Constants
6-
import kotlin.math.min
6+
import kotlin.math.abs
7+
import kotlin.math.sqrt
78

89
/** Eine 2D Koordinate der Form (x, y). */
910
@XStreamAlias(value = "coordinates")
1011
data class Coordinates(
1112
@XStreamAsAttribute val x: Int,
12-
@XStreamAsAttribute val y: Int) {
13+
@XStreamAsAttribute val y: Int,
14+
) {
1315

1416
override fun toString(): String = "[$x|$y]"
15-
17+
1618
/** Addiere den [Vector] auf die [Coordinates] auf. */
1719
operator fun plus(vector: Vector): Coordinates {
1820
return Coordinates(x + vector.dx, y + vector.dy)
@@ -27,7 +29,7 @@ data class Coordinates(
2729
}
2830
/** Wandelt die [Coordinates] in einen entsprechenden [Vector]. */
2931
operator fun unaryPlus(): Vector = Vector(x, y)
30-
32+
3133
/** Gibt ein Set der vier benachbarten Felder dieser Koordinaten zurück. */
3234
val neighbors: Set<Coordinates>
3335
get() = Vector.cardinals.mapTo(HashSet()) { this + it }
@@ -48,27 +50,31 @@ data class Coordinates(
4850
* @property dx die Differenz in x-Richtung
4951
* @property dy die Differenz in y-Richtung
5052
*/
51-
data class Vector(val dx: Int, val dy: Int) {
53+
data class Vector(val dx: Int, val dy: Int): Comparable<Vector> {
5254
/** Die Fläche des Rechtecks, dessen Diagonale der Vector ist. */
53-
val area: Int = dx * dy
54-
55-
/** Verändert die Länge des Vectors um den gegebenen Faktor, ohne seine Richtung zu ändern. */
56-
operator fun times(scalar: Int): Vector {
57-
return Vector(scalar * dx, scalar * dy)
58-
}
59-
55+
val area: Int
56+
get() = abs(dx * dy)
57+
58+
private val comparableLength: Int
59+
get() = dx * dx + dy * dy
60+
61+
val length: Double
62+
get() = sqrt(comparableLength.toDouble())
63+
64+
/** Verändert die Länge des Vektors um den gegebenen Faktor, ohne seine Richtung zu ändern. */
65+
operator fun times(scalar: Int): Vector =
66+
Vector(scalar * dx, scalar * dy)
67+
6068
/**
61-
* Vergleicht die beiden Vektoren. Der Rückgabewert ist
62-
* - positiv, wenn beide Größen dieses Vektors kleiner sind als die des anderen.
63-
* - null, wenn beide Vektoren gleich groß sind.
64-
* - negativ, wenn mindestens eine Größe dieses Vektors größer als die des anderen ist.
69+
* Vergleicht die Länge dieses Vektors mit einem anderen.
70+
* @return groesser Null, wenn dieser laenger ist
6571
*/
66-
operator fun compareTo(other: Vector): Int =
67-
min(other.dx - dx, other.dy - dy)
68-
72+
override operator fun compareTo(other: Vector): Int =
73+
comparableLength - other.comparableLength
74+
6975
/** Konvertiert den Vektor zu entsprechenden [Coordinates]. */
7076
operator fun unaryPlus(): Coordinates = Coordinates(dx, dy)
71-
77+
7278
companion object {
7379
/** Die vier Vektoren in diagonaler Richtung. */
7480
val diagonals: Array<Vector> = arrayOf(

plugin/src/main/sc/plugin2022/Move.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,15 @@ data class Move(
88
val start: Coordinates,
99
/** Zielposition des Zugs. */
1010
val destination: Coordinates,
11-
): IMove {
12-
val delta
11+
): IMove, Comparable<Move> {
12+
val delta: Vector
1313
get() = destination - start
1414

1515
override fun toString(): String = "Zug von $start zu $destination"
1616

17+
/** Compares the Moves based on their length ([delta]). */
18+
override fun compareTo(other: Move) = delta.compareTo(other.delta)
19+
1720
companion object {
1821
/** @return a [Move] if the target is on the board, or null. */
1922
@JvmStatic

0 commit comments

Comments
 (0)