Skip to content

Commit a1174b3

Browse files
author
Xerus
committed
refactor(gamerules): clean up code duplication
1 parent 75a714a commit a1174b3

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

plugin/src/shared/sc/plugin2020/util/GameRuleLogic.kt

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,7 @@ object GameRuleLogic {
149149
if (gameState.board.getField(move.destination).pieces.isNotEmpty() && pieceToDrag.type !== PieceType.BEETLE)
150150
throw InvalidMoveException("Only beetles are allowed to climb on other Pieces")
151151

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)
155153
if (!isSwarmConnected(boardWithoutPiece))
156154
throw InvalidMoveException("Moving piece would disconnect swarm")
157155

@@ -164,14 +162,19 @@ object GameRuleLogic {
164162
}
165163
}
166164

165+
fun Board.withoutPiece(position: CubeCoordinates): Board =
166+
Board(fields.map {
167+
if (it == position) it.clone().apply { pieces.pop() } else it
168+
})
169+
167170
@Throws(InvalidMoveException::class)
168171
@JvmStatic
169172
fun validateAntMove(board: Board, move: DragMove) {
170173
val visitedFields = mutableListOf(move.start)
171174
var index = 0
172175
do {
173176
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 }
175178
if (move.destination in newFields)
176179
return
177180
visitedFields.addAll(newFields)
@@ -198,12 +201,10 @@ object GameRuleLogic {
198201

199202
@JvmStatic
200203
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)
204205

205206
@JvmStatic
206-
fun getAccessibleNeighboursExcept(board: Board, start: CubeCoordinates, except: CubeCoordinates) =
207+
fun getAccessibleNeighboursExcept(board: Board, start: CubeCoordinates, except: CubeCoordinates?) =
207208
getNeighbours(board, start).filter { neighbour ->
208209
neighbour.isEmpty && canMoveBetweenExcept(board, start, neighbour, except) && neighbour.coordinates != except
209210
}
@@ -245,7 +246,7 @@ object GameRuleLogic {
245246
paths.add(arrayOf(move.start))
246247
do {
247248
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 }
249250
if (currentPath.size < 3)
250251
paths.addAll(newFields.map { currentPath + it })
251252
else if (move.destination in newFields)
@@ -277,12 +278,10 @@ object GameRuleLogic {
277278

278279
@JvmStatic
279280
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)
283282

284283
@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 =
286285
sharedNeighboursOfTwoCoords(board, coords1, coords2).filterNot { it.pieces.size == 1 && except == it.coordinates }.let { shared ->
287286
(shared.size == 1 || shared.any { it.isEmpty && !it.isObstructed }) && shared.any { it.pieces.isNotEmpty() }
288287
}

0 commit comments

Comments
 (0)