Skip to content

Commit 95e658c

Browse files
committed
feat(plugin): some utility checks
1 parent 2eb4897 commit 95e658c

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

plugin/src/main/kotlin/sc/plugin2023/Board.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,7 @@ class Board(fields: TwoDBoard<Field> = generateFields()): RectangularBoard<Field
8686
fun getOrEmpty(key: Coordinates?) = key?.let { getOrNull(it) } ?: Field()
8787

8888
override val entries: Set<Map.Entry<Coordinates, Field>>
89-
get() = filterFields { field, coordinates ->
90-
// TODO really? an anonymous object?
91-
object: Map.Entry<Coordinates, Field> {
92-
override val key = coordinates
93-
override val value = field
94-
}
95-
}.toSet()
89+
get() = filterFields { field, coordinates -> FieldPosition(coordinates, field) }.toSet()
9690

9791
override fun clone(): Board = Board(this)
9892

plugin/src/main/kotlin/sc/plugin2023/GameState.kt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,17 @@ data class GameState @JvmOverloads constructor(
5757
val currentPieces
5858
get() = board.filterValues { it.penguin == currentTeam }
5959

60-
override fun getPossibleMoves(): List<Move> {
61-
val pieces = currentPieces
62-
return if(pieces.size < PluginConstants.PENGUINS) {
63-
board.filterValues { it.fish == 1 }.map { Move(null, it.key) }
64-
} else {
65-
pieces.flatMap { (pos, _) -> board.possibleMovesFrom(pos) }
66-
}
67-
}
60+
val penguinsPlaced
61+
get() = currentPieces.size == PluginConstants.PENGUINS
62+
63+
override fun getPossibleMoves(): List<Move> =
64+
if(penguinsPlaced) {
65+
currentPieces.flatMap { (pos, _) -> board.possibleMovesFrom(pos) }
66+
} else {
67+
board.filterValues { it.fish == 1 }.map { Move(null, it.key) }
68+
}
69+
70+
fun canPlacePenguin(pos: Coordinates) = !penguinsPlaced && board[pos].fish == 1
6871

6972
fun immovable(team: Team? = null) =
7073
board.getPenguins()

sdk/src/main/server-api/sc/api/plugins/Coordinates.kt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@ data class Coordinates(
1515
override fun toString(): String = "[$x|$y]"
1616

1717
/** Addiere den [Vector] auf die [Coordinates] auf. */
18-
operator fun plus(vector: Vector): Coordinates {
19-
return Coordinates(x + vector.dx, y + vector.dy)
20-
}
18+
operator fun plus(vector: Vector): Coordinates =
19+
Coordinates(x + vector.dx, y + vector.dy)
2120
/** Berechne die Distanz zweier Koordinaten, als [Vector] */
22-
operator fun minus(other: Coordinates): Vector {
23-
return Vector(x - other.x, y - other.y)
24-
}
21+
operator fun minus(other: Coordinates): Vector =
22+
Vector(x - other.x, y - other.y)
2523
/** Ziehe die Distanz (als [Vector]) von der Koordinate ab. */
26-
operator fun minus(other: Vector): Coordinates {
27-
return Coordinates(x - other.dx, y - other.dy)
28-
}
24+
operator fun minus(other: Vector): Coordinates =
25+
Coordinates(x - other.dx, y - other.dy)
2926
/** Wandelt die [Coordinates] in einen entsprechenden [Vector]. */
3027
operator fun unaryPlus(): Vector = Vector(x, y)
3128

3229
/** Gibt ein Set der vier benachbarten Felder dieser Koordinaten zurück. */
3330
val neighbors: Collection<Coordinates>
3431
get() = Vector.cardinals.map { this + it }
3532

33+
// TODO separate interfaces Positioned and HexPositioned?
3634
val hexNeighbors: Collection<Coordinates>
3735
get() = Vector.DoubledHex.directions.map { this + it }
3836

sdk/src/main/server-api/sc/api/plugins/RectangularBoard.kt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package sc.api.plugins
22

3-
import com.thoughtworks.xstream.annotations.XStreamConverter
43
import com.thoughtworks.xstream.annotations.XStreamImplicit
5-
import com.thoughtworks.xstream.converters.collections.CollectionConverter
64
import sc.framework.PublicCloneable
75

86
typealias TwoDBoard<FIELD> = List<MutableList<FIELD>>
@@ -72,14 +70,15 @@ open class RectangularBoard<FIELD: IField<FIELD>>(
7270
override val entries: Set<Map.Entry<Coordinates, FIELD>>
7371
get() = gameField.flatMapIndexed { y, row ->
7472
row.mapIndexed { x, field ->
75-
// TODO really? an anonymous object?
76-
object: Map.Entry<Coordinates, FIELD> {
77-
override val key = Coordinates(x, y)
78-
override val value = field
79-
}
73+
FieldPosition(Coordinates(x, y), field)
8074
}
8175
}.toSet()
8276

77+
inner class FieldPosition(
78+
override val key: Coordinates,
79+
override val value: FIELD
80+
): Map.Entry<Coordinates, FIELD>
81+
8382
override val size: Int
8483
get() = gameField.sumOf { it.size }
8584

server/src/test/java/sc/server/network/ClientXmlReadTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public void clientSendPacketTest() throws IOException {
4848

4949
client.start();
5050
client.send(new ExamplePacket());
51+
stringInterface.close();
5152
String data = stringInterface.readData();
5253
assertEquals("<protocol>\n <example/>", data);
5354
}

0 commit comments

Comments
 (0)