Skip to content

Commit

Permalink
feat(plugin): add getAllMoves for GameState
Browse files Browse the repository at this point in the history
  • Loading branch information
xeruf committed Aug 21, 2023
1 parent 6d4f1e3 commit 1f397e8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 9 deletions.
1 change: 1 addition & 0 deletions .dev/scopes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ testclient
player
server

plugin
plugin23
plugin24
gamerules
Expand Down
3 changes: 3 additions & 0 deletions plugin/src/main/kotlin/sc/plugin2023/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ data class GameState @JvmOverloads constructor(
board.filterValues { it.fish == 1 }.map { Move(null, it.key) }
}

override fun getAllMoves(): Iterator<IMove> =
getSensibleMoves().iterator()

fun canPlacePenguin(pos: Coordinates) = !penguinsPlaced && board[pos].fish == 1

fun immovable(team: Team? = null) =
Expand Down
4 changes: 4 additions & 0 deletions plugin/src/main/kotlin/sc/plugin2024/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ data class GameState @JvmOverloads constructor(
override fun getSensibleMoves(): List<IMove> =
getPossibleMoves(currentShip.coal.coerceAtMost(1)).ifEmpty { getPossibleMoves() }

/** TODO Incomplete */
override fun getAllMoves(): Iterator<IMove> =
getPossibleMoves().iterator()

// TODO this should be a Stream
/** Possible simple Moves (accelerate+turn+move) using at most the given coal amount. */
fun getPossibleMoves(maxCoal: Int = currentShip.coal): List<IMove> =
Expand Down
10 changes: 5 additions & 5 deletions plugin/src/test/kotlin/sc/plugin2024/BoardTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ class BoardTest: FunSpec({
}

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

Expand Down
18 changes: 14 additions & 4 deletions sdk/src/main/server-api/sc/api/plugins/IGameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ import sc.protocol.room.RoomMessage
* Zusaetzlich zu den eigentlichen Informationen koennen bestimmte
* Teilinformationen abgefragt werden.
*/
interface IGameState : RoomMessage, PublicCloneable<IGameState> {
interface IGameState: RoomMessage, PublicCloneable<IGameState> {
/** Aktuelle Zugzahl */
val turn: Int

/** Aktuelle Rundenzahl */
val round: Int

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

/** Die möglichen Züge des aktuellen Teams in der aktuellen Situation.
/** Mögliche Züge des aktuellen Teams in der aktuellen Situation.
* Bei manchen Spielen wird aufgrund der unüberschaubaren Zahl möglicher Züge
* nur ein Ausschnitt zurückgegeben. */
fun getSensibleMoves(): List<IMove>
fun getSensibleMoves(): List<IMove> {
val possibleMoves = getAllMoves()
val someMoves: MutableList<IMove> = ArrayList()
while(possibleMoves.hasNext() && someMoves.size < 100)
someMoves.add(possibleMoves.next())
return someMoves
}

/** Eine Abfolge aller möglichen Züge des aktuellen Teams,
* nur soweit berechnet wie nötig. */
fun getAllMoves(): Iterator<IMove>
}

0 comments on commit 1f397e8

Please sign in to comment.