Skip to content

Commit af633d3

Browse files
committed
feat(plugin): add getAllMoves for GameState
1 parent 6d4f1e3 commit af633d3

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

.dev/scopes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ testclient
1212
player
1313
server
1414

15+
plugin
1516
plugin23
1617
plugin24
1718
gamerules

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ data class GameState @JvmOverloads constructor(
6666
board.filterValues { it.fish == 1 }.map { Move(null, it.key) }
6767
}
6868

69+
override fun getAllMoves(): Iterator<IMove> =
70+
getSensibleMoves().iterator()
71+
6972
fun canPlacePenguin(pos: Coordinates) = !penguinsPlaced && board[pos].fish == 1
7073

7174
fun immovable(team: Team? = null) =

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ data class GameState @JvmOverloads constructor(
132132
override fun getSensibleMoves(): List<IMove> =
133133
getPossibleMoves(currentShip.coal.coerceAtMost(1)).ifEmpty { getPossibleMoves() }
134134

135+
/** TODO Incomplete */
136+
override fun getAllMoves(): Iterator<IMove> =
137+
getPossibleMoves().iterator()
138+
135139
// TODO this should be a Stream
136140
/** Possible simple Moves (accelerate+turn+move) using at most the given coal amount. */
137141
fun getPossibleMoves(maxCoal: Int = currentShip.coal): List<IMove> =

plugin/src/test/kotlin/sc/plugin2024/BoardTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ class BoardTest: FunSpec({
133133
}
134134

135135
test("return false when rotation mismatch") {
136-
val board = Board(listOf(Segment(CubeDirection.RIGHT, CubeCoordinates.ORIGIN, arrayOf(arrayOf(Field.WATER, Field.WATER, Field.PASSENGER(CubeDirection.LEFT))))))
136+
val miniBoard = Board(listOf(Segment(CubeDirection.RIGHT, CubeCoordinates.ORIGIN, arrayOf(arrayOf(Field.WATER, Field.WATER, Field.PASSENGER(CubeDirection.LEFT))))))
137137
val ship = Ship(CubeCoordinates.ORIGIN, Team.ONE)
138-
board.pickupPassenger(CubeCoordinates.ORIGIN) shouldBe null
139-
board.pickupPassenger(ship) shouldBe false
140-
board[CubeCoordinates.ORIGIN + CubeDirection.LEFT.vector].shouldBeInstanceOf<Field.PASSENGER>()
141-
board.pickupPassenger(CubeCoordinates.ORIGIN + CubeDirection.LEFT.vector * 2) shouldBe Field.PASSENGER(CubeDirection.LEFT)
138+
miniBoard.pickupPassenger(CubeCoordinates.ORIGIN) shouldBe null
139+
miniBoard.pickupPassenger(ship) shouldBe false
140+
miniBoard[CubeCoordinates.ORIGIN + CubeDirection.LEFT.vector].shouldBeInstanceOf<Field.PASSENGER>()
141+
miniBoard.pickupPassenger(CubeCoordinates.ORIGIN + CubeDirection.LEFT.vector * 2) shouldBe Field.PASSENGER(CubeDirection.LEFT)
142142
}
143143
}
144144

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import sc.protocol.room.RoomMessage
2828
* Zusaetzlich zu den eigentlichen Informationen koennen bestimmte
2929
* Teilinformationen abgefragt werden.
3030
*/
31-
interface IGameState : RoomMessage, PublicCloneable<IGameState> {
31+
interface IGameState: RoomMessage, PublicCloneable<IGameState> {
3232
/** Aktuelle Zugzahl */
3333
val turn: Int
34-
34+
3535
/** Aktuelle Rundenzahl */
3636
val round: Int
3737

@@ -44,8 +44,18 @@ interface IGameState : RoomMessage, PublicCloneable<IGameState> {
4444
/** Gibt Punktzahlen des Teams entsprechend der [ScoreDefinition] zurück. */
4545
fun getPointsForTeam(team: ITeam): IntArray
4646

47-
/** Die möglichen Züge des aktuellen Teams in der aktuellen Situation.
47+
/** Mögliche Züge des aktuellen Teams in der aktuellen Situation.
4848
* Bei manchen Spielen wird aufgrund der unüberschaubaren Zahl möglicher Züge
4949
* nur ein Ausschnitt zurückgegeben. */
50-
fun getSensibleMoves(): List<IMove>
50+
fun getSensibleMoves(): List<IMove> {
51+
val possibleMoves = getAllMoves()
52+
val someMoves: MutableList<IMove> = ArrayList()
53+
while(possibleMoves.hasNext() && someMoves.size < 100)
54+
someMoves.add(possibleMoves.next())
55+
return someMoves
56+
}
57+
58+
/** Eine Abfolge aller möglichen Züge des aktuellen Teams,
59+
* nur soweit berechnet wie nötig. */
60+
fun getAllMoves(): Iterator<IMove>
5161
}

server/src/test/java/sc/server/plugins/TestGameState.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ data class TestGameState(
1616
@Transient
1717
override val isOver = false
1818

19-
override fun getPointsForTeam(team: ITeam): IntArray = intArrayOf(currentTeam.index, turn)
19+
override fun getPointsForTeam(team: ITeam): IntArray =
20+
intArrayOf(currentTeam.index, turn)
2021

21-
override fun getSensibleMoves(): List<IMove> = throw NotImplementedError("TestGame has no possible Moves")
22+
override fun getAllMoves(): Iterator<IMove> =
23+
throw NotImplementedError("TestGame has no possible Moves")
2224

2325
override val round get() = turn / 2
2426

0 commit comments

Comments
 (0)