@@ -149,9 +149,7 @@ object GameRuleLogic {
149
149
if (gameState.board.getField(move.destination).pieces.isNotEmpty() && pieceToDrag.type != = PieceType .BEETLE )
150
150
throw InvalidMoveException (" Only beetles are allowed to climb on other Pieces" )
151
151
152
- val boardWithoutPiece = Board (gameState.board.fields.map {
153
- if (it == move.start) Field (it).apply { pieces.pop() } else it
154
- })
152
+ val boardWithoutPiece = gameState.board.withoutPiece(move.start)
155
153
if (! isSwarmConnected(boardWithoutPiece))
156
154
throw InvalidMoveException (" Moving piece would disconnect swarm" )
157
155
@@ -164,14 +162,19 @@ object GameRuleLogic {
164
162
}
165
163
}
166
164
165
+ fun Board.withoutPiece (position : CubeCoordinates ): Board =
166
+ Board (fields.map {
167
+ if (it == position) it.clone().apply { pieces.pop() } else it
168
+ })
169
+
167
170
@Throws(InvalidMoveException ::class )
168
171
@JvmStatic
169
172
fun validateAntMove (board : Board , move : DragMove ) {
170
173
val visitedFields = mutableListOf (move.start)
171
174
var index = 0
172
175
do {
173
176
val currentField = visitedFields[index]
174
- val newFields = getAccessibleNeighboursExcept (board, currentField, move.start ).filterNot { it in visitedFields }
177
+ val newFields = getAccessibleNeighbours (board, currentField).filterNot { it in visitedFields }
175
178
if (move.destination in newFields)
176
179
return
177
180
visitedFields.addAll(newFields)
@@ -198,12 +201,10 @@ object GameRuleLogic {
198
201
199
202
@JvmStatic
200
203
fun getAccessibleNeighbours (board : Board , start : CubeCoordinates ) =
201
- getNeighbours(board, start).filter { neighbour ->
202
- neighbour.isEmpty && canMoveBetween(board, start, neighbour)
203
- }
204
+ getAccessibleNeighboursExcept(board, start, null )
204
205
205
206
@JvmStatic
206
- fun getAccessibleNeighboursExcept (board : Board , start : CubeCoordinates , except : CubeCoordinates ) =
207
+ fun getAccessibleNeighboursExcept (board : Board , start : CubeCoordinates , except : CubeCoordinates ? ) =
207
208
getNeighbours(board, start).filter { neighbour ->
208
209
neighbour.isEmpty && canMoveBetweenExcept(board, start, neighbour, except) && neighbour.coordinates != except
209
210
}
@@ -245,7 +246,7 @@ object GameRuleLogic {
245
246
paths.add(arrayOf(move.start))
246
247
do {
247
248
val currentPath = paths.removeFirst()
248
- val newFields = getAccessibleNeighboursExcept (board, currentPath.last(), move.start ).filterNot { it in currentPath }
249
+ val newFields = getAccessibleNeighbours (board, currentPath.last()).filterNot { it in currentPath }
249
250
if (currentPath.size < 3 )
250
251
paths.addAll(newFields.map { currentPath + it })
251
252
else if (move.destination in newFields)
@@ -277,12 +278,10 @@ object GameRuleLogic {
277
278
278
279
@JvmStatic
279
280
fun canMoveBetween (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates ): Boolean =
280
- sharedNeighboursOfTwoCoords(board, coords1, coords2).let { shared ->
281
- (shared.size == 1 || shared.any { it.isEmpty && ! it.isObstructed }) && shared.any { it.pieces.isNotEmpty() }
282
- }
281
+ canMoveBetweenExcept(board, coords1, coords2, null )
283
282
284
283
@JvmStatic
285
- fun canMoveBetweenExcept (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates , except : CubeCoordinates ): Boolean =
284
+ fun canMoveBetweenExcept (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates , except : CubeCoordinates ? ): Boolean =
286
285
sharedNeighboursOfTwoCoords(board, coords1, coords2).filterNot { it.pieces.size == 1 && except == it.coordinates }.let { shared ->
287
286
(shared.size == 1 || shared.any { it.isEmpty && ! it.isObstructed }) && shared.any { it.pieces.isNotEmpty() }
288
287
}
0 commit comments