Skip to content

Commit 7380c9c

Browse files
committed
fix(plugin): tweak Gamestate and Board
All tests pass already :)
1 parent 2f6c625 commit 7380c9c

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import sc.plugin2022.util.Constants.boardrange
88
import sc.plugin2022.util.MoveMistake
99
import sc.shared.InvalidMoveException
1010

11-
/** Das Spielbrett besteht aus 8x8 Feldern. */
11+
/** Das Spielbrett besteht aus 8x8 Feldern mit anfänglich 8 Figuren pro Spieler. */
1212
@XStreamAlias(value = "board")
1313
data class Board(
14-
private val board: MutableMap<Coordinates, Piece>,
15-
): IBoard, Map<Coordinates, Piece> by board {
14+
private val piecePositions: MutableMap<Coordinates, Piece>,
15+
): IBoard, Map<Coordinates, Piece> by piecePositions {
1616

17-
constructor(): this(generateBoard())
17+
constructor(): this(generatePiecePositions())
1818

1919
/** Gibt das Feld an den gegebenen Koordinaten zurück. */
2020
operator fun get(x: Int, y: Int) =
@@ -25,19 +25,19 @@ data class Board(
2525
* @return the moved [Piece], null if it turned into an amber. */
2626
@Throws(InvalidMoveException::class)
2727
fun movePiece(move: Move): Piece? =
28-
board[move.start]?.let { piece ->
28+
(piecePositions[move.start] ?: throw InvalidMoveException(MoveMistake.START_EMPTY, move)).let { piece ->
2929
if (move.delta !in piece.possibleMoves)
3030
throw InvalidMoveException(MoveMistake.INVALID_MOVEMENT, move)
31-
board[move.destination]?.let { piece.capture(it) }
32-
board.remove(move.start)
31+
piecePositions[move.destination]?.let { piece.capture(it) }
32+
piecePositions.remove(move.start)
3333
if (piece.isAmber || (piece.type.isLight && move.destination.y == piece.team.opponent().startLine)) {
34-
board.remove(move.destination)
34+
piecePositions.remove(move.destination)
3535
null
3636
} else {
37-
board[move.destination] = piece
37+
piecePositions[move.destination] = piece
3838
piece
3939
}
40-
} ?: throw InvalidMoveException(MoveMistake.START_EMPTY, move)
40+
}
4141

4242
override fun toString() =
4343
boardrange.joinToString("\n") { y ->
@@ -46,11 +46,14 @@ data class Board(
4646
}
4747
}
4848

49-
override fun clone() = Board(HashMap(board))
49+
override fun clone() = Board(HashMap(piecePositions))
5050

5151
companion object {
52+
/** Generates a random new board with two pieces per type
53+
* for each player arranged randomly on their starting line
54+
* in rotational symmetry. */
5255
@JvmStatic
53-
fun generateBoard() =
56+
fun generatePiecePositions() =
5457
(PieceType.values() + PieceType.values()).let { pieces ->
5558
pieces.shuffle()
5659
pieces.withIndex().flatMap { (index, type) ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import java.util.EnumMap
1515
* mit deren Hilfe der nächste Zug berechnet werden kann.
1616
*/
1717
@XStreamAlias(value = "state")
18-
class GameState @JvmOverloads constructor(
18+
data class GameState @JvmOverloads constructor(
1919
/** Das aktuelle Spielfeld. */
2020
override val board: Board = Board(),
2121
/** Die Anzahl an bereits getätigten Zügen. */
@@ -48,7 +48,7 @@ class GameState @JvmOverloads constructor(
4848

4949
/** Berechne die Punkteanzahl für das gegebene Team. */
5050
override fun getPointsForTeam(team: ITeam): Int =
51-
ambers[team]!!
51+
ambers[team] ?: 0
5252

5353
override fun clone() = GameState(this)
5454

0 commit comments

Comments
 (0)