@@ -36,9 +36,7 @@ object GameRuleLogic {
36
36
if (Constants .VALIDATE_MOVE )
37
37
validateSetMove(gameState, move)
38
38
39
- move.piece.coordinates.forEach {
40
- gameState.board[it] = + move.color
41
- }
39
+ performSetMove(gameState.board, move)
42
40
gameState.undeployedPieceShapes.getValue(move.color).remove(move.piece.kind)
43
41
gameState.deployedPieces?.getValue(move.color).add(move.piece)
44
42
@@ -67,20 +65,9 @@ object GameRuleLogic {
67
65
// Check if piece has already been placed
68
66
gameState.undeployedPieceShapes.getValue(move.color).find { it == move.piece.kind }
69
67
? : throw InvalidMoveException (" Piece #${move.piece.kind} has already been placed before" , move)
68
+
69
+ validateSetMove(gameState.board, move)
70
70
71
- move.piece.coordinates.forEach {
72
- try {
73
- gameState.board[it]
74
- } catch (e: ArrayIndexOutOfBoundsException ) {
75
- throw InvalidMoveException (" Field $it is out of bounds" , move)
76
- }
77
- // Checks if a part of the piece is obstructed
78
- if (isObstructed(gameState.board, it))
79
- throw InvalidMoveException (" Field $it already belongs to ${gameState.board[it].content} " , move)
80
- // Checks if a part of the piece would border on another piece of same color
81
- if (bordersOnColor(gameState.board, it, move.color))
82
- throw InvalidMoveException (" Field $it already borders on ${move.color} " , move)
83
- }
84
71
if (isFirstMove(gameState)) {
85
72
// Check if it's the requested shape
86
73
if (move.piece.kind != gameState.startPiece)
@@ -95,6 +82,32 @@ object GameRuleLogic {
95
82
}
96
83
}
97
84
85
+ /* * Validates a SetMove on a board. */
86
+ @JvmStatic
87
+ fun validateSetMove (board : Board , move : SetMove ) {
88
+ move.piece.coordinates.forEach {
89
+ try {
90
+ board[it]
91
+ } catch (e: ArrayIndexOutOfBoundsException ) {
92
+ throw InvalidMoveException (" Field $it is out of bounds" , move)
93
+ }
94
+ // Checks if a part of the piece is obstructed
95
+ if (isObstructed(board, it))
96
+ throw InvalidMoveException (" Field $it already belongs to ${board[it].content} " , move)
97
+ // Checks if a part of the piece would border on another piece of same color
98
+ if (bordersOnColor(board, it, move.color))
99
+ throw InvalidMoveException (" Field $it already borders on ${move.color} " , move)
100
+ }
101
+ }
102
+
103
+ /* * Places the given Piece on the given board. */
104
+ @JvmStatic
105
+ fun performSetMove (board : Board , move : SetMove ) {
106
+ move.piece.coordinates.forEach {
107
+ board[it] = + move.color
108
+ }
109
+ }
110
+
98
111
/* * Returns true if [move] is valid, false otherwise. */
99
112
@JvmStatic
100
113
fun isValidSetMove (gameState : GameState , move : SetMove ) =
0 commit comments