@@ -20,17 +20,24 @@ data class Board(
20
20
operator fun get (x : Int , y : Int ) =
21
21
get(Coordinates (x, y))
22
22
23
- /* * Moves a piece according to [Move].
23
+ /* * Moves a piece according to [Move] after verification .
24
24
* @throws InvalidMoveException if something is wrong with the Move.
25
25
* @return the moved [Piece], null if it turned into an amber. */
26
26
@Throws(InvalidMoveException ::class )
27
27
fun movePiece (move : Move ): Piece ? =
28
28
(piecePositions[move.start] ? : throw InvalidMoveException (MoveMistake .START_EMPTY , move)).let { piece ->
29
+ if (! move.destination.isValid)
30
+ throw InvalidMoveException (MoveMistake .OUT_OF_BOUNDS , move)
29
31
if (move.delta !in piece.possibleMoves)
30
32
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
+ }
32
38
piecePositions.remove(move.start)
33
39
if (piece.isAmber || (piece.type.isLight && move.destination.y == piece.team.opponent().startLine)) {
40
+ // TODO how to indicate double amber?
34
41
piecePositions.remove(move.destination)
35
42
null
36
43
} else {
@@ -54,7 +61,8 @@ data class Board(
54
61
* in rotational symmetry. */
55
62
@JvmStatic
56
63
fun generatePiecePositions () =
57
- (PieceType .values() + PieceType .values()).let { pieces ->
64
+ PieceType .values().let { types ->
65
+ val pieces = types + types
58
66
pieces.shuffle()
59
67
pieces.withIndex().flatMap { (index, type) ->
60
68
Team .values().map { team ->
0 commit comments