Skip to content

Commit 70693a6

Browse files
committed
fix(plugin): check whether Move target contains own piece
1 parent 7380c9c commit 70693a6

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,24 @@ data class Board(
2020
operator fun get(x: Int, y: Int) =
2121
get(Coordinates(x, y))
2222

23-
/** Moves a piece according to [Move].
23+
/** Moves a piece according to [Move] after verification.
2424
* @throws InvalidMoveException if something is wrong with the Move.
2525
* @return the moved [Piece], null if it turned into an amber. */
2626
@Throws(InvalidMoveException::class)
2727
fun movePiece(move: Move): Piece? =
2828
(piecePositions[move.start] ?: throw InvalidMoveException(MoveMistake.START_EMPTY, move)).let { piece ->
29+
if(!move.destination.isValid)
30+
throw InvalidMoveException(MoveMistake.OUT_OF_BOUNDS, move)
2931
if (move.delta !in piece.possibleMoves)
3032
throw InvalidMoveException(MoveMistake.INVALID_MOVEMENT, move)
31-
piecePositions[move.destination]?.let { piece.capture(it) }
33+
piecePositions[move.destination]?.let {
34+
if(it.team == piece.team)
35+
throw InvalidMoveException(MoveMistake.DESTINATION_BLOCKED, move)
36+
piece.capture(it)
37+
}
3238
piecePositions.remove(move.start)
3339
if (piece.isAmber || (piece.type.isLight && move.destination.y == piece.team.opponent().startLine)) {
40+
// TODO how to indicate double amber?
3441
piecePositions.remove(move.destination)
3542
null
3643
} else {
@@ -54,7 +61,8 @@ data class Board(
5461
* in rotational symmetry. */
5562
@JvmStatic
5663
fun generatePiecePositions() =
57-
(PieceType.values() + PieceType.values()).let { pieces ->
64+
PieceType.values().let { types ->
65+
val pieces = types + types
5866
pieces.shuffle()
5967
pieces.withIndex().flatMap { (index, type) ->
6068
Team.values().map { team ->

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ data class GameState @JvmOverloads constructor(
3838

3939
val possibleMoves
4040
get() = currentPieces.flatMap { (pos, piece) ->
41-
piece.possibleMoves.mapNotNull {
42-
Move.create(pos, it)
41+
piece.possibleMoves.mapNotNull { delta ->
42+
Move.create(pos, delta)?.takeIf { board[it.destination]?.team != piece.team }
4343
}
4444
}
4545

plugin/src/main/sc/plugin2022/util/MoveMistake.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import sc.shared.IMoveMistake
1111
enum class MoveMistake(override val message: String): IMoveMistake {
1212
WRONG_COLOR("Die Farbe des Zuges ist nicht an der Reihe"),
1313
START_EMPTY("Das Startfeld des Zuges ist leer"),
14-
INVALID_MOVEMENT("%s kann sich nicht um %s bewegen"),
14+
DESTINATION_BLOCKED("Kann nicht auf ein Feld der eigenen Farbe ziehen"),
15+
OUT_OF_BOUNDS("Das Zielfeld liegt außerhalb des Spielfelds"),
16+
INVALID_MOVEMENT("%s kann sich nicht um %s bewegen"), // TODO useful message formatting
1517
INVALID_MOVE("Dieser Zug ist nicht möglich"),
1618
INVALID_FORMAT("Der Zug konnte nicht erkannt werden");
1719
override fun toString() = message

0 commit comments

Comments
 (0)