@@ -62,16 +62,14 @@ object GameRuleLogic {
62
62
/* * Checks if the given [move] is able to be performed for the given [gameState]. */
63
63
@JvmStatic
64
64
fun validateSetMove (gameState : GameState , move : SetMove ) {
65
- // Check if piece has already been placed
66
- gameState.undeployedPieceShapes.getValue(move.color).find { it == move.piece.kind }
67
- ? : throw InvalidMoveException (" Piece #${move.piece.kind} has already been placed before" , move)
68
-
65
+ // Check whether the color's move is currently active
66
+ validateMoveColor(gameState, move)
67
+ // Check whether the shape is valid
68
+ validateShape(gameState, move.piece.kind, move.color)
69
+ // Check whether the piece can be placed
69
70
validateSetMove(gameState.board, move)
70
71
71
72
if (isFirstMove(gameState)) {
72
- // Check if it's the requested shape
73
- if (move.piece.kind != gameState.startPiece)
74
- throw InvalidMoveException (" Expected the predetermined staring piece, ${gameState.startPiece} " , move)
75
73
// Check if it is placed correctly in a corner
76
74
if (move.piece.coordinates.none { isOnCorner(it)})
77
75
throw InvalidMoveException (" The Piece isn't located in a corner" , move)
@@ -82,7 +80,19 @@ object GameRuleLogic {
82
80
}
83
81
}
84
82
85
- /* * Validates a SetMove on a board. */
83
+ /* * Validates the [PieceShape] of a [SetMove] depending on the current [GameState]. */
84
+ @JvmStatic
85
+ fun validateShape (gameState : GameState , shape : PieceShape , color : Color = gameState.currentColor) {
86
+ if (isFirstMove(gameState)) {
87
+ if (shape != gameState.startPiece)
88
+ throw InvalidMoveException (" $shape is not the requested first shape, ${gameState.startPiece} " )
89
+ } else {
90
+ if (! gameState.undeployedPieceShapes.getValue(color).contains(shape))
91
+ throw InvalidMoveException (" Piece ${shape} has already been placed before" )
92
+ }
93
+ }
94
+
95
+ /* * Validates a [SetMove] on a [board]. */
86
96
@JvmStatic
87
97
fun validateSetMove (board : Board , move : SetMove ) {
88
98
move.piece.coordinates.forEach {
0 commit comments