@@ -8,12 +8,12 @@ import kotlin.math.abs
8
8
9
9
object GameRuleLogic {
10
10
11
- /* * @return all (up to 6) neighbouring fields of [coords]. */
11
+ /* * @return all (up to 6) neighboring fields of [coords]. */
12
12
@JvmStatic
13
- fun getNeighbours (board : Board , coords : CubeCoordinates ): List <Field > =
13
+ fun getNeighbors (board : Board , coords : CubeCoordinates ): List <Field > =
14
14
Direction .values().mapNotNull {
15
15
try {
16
- getNeighbourInDirection (board, coords, it)
16
+ getNeighborInDirection (board, coords, it)
17
17
} catch (ex: IndexOutOfBoundsException ) {
18
18
null
19
19
}
@@ -22,7 +22,7 @@ object GameRuleLogic {
22
22
/* * Gets the [Field] adjacent to [coords] in [direction].
23
23
* @throws IndexOutOfBoundsException if there is no field in that direction. */
24
24
@JvmStatic
25
- fun getNeighbourInDirection (board : Board , coords : CubeCoordinates , direction : Direction ): Field {
25
+ fun getNeighborInDirection (board : Board , coords : CubeCoordinates , direction : Direction ): Field {
26
26
return board.getField(CubeCoordinates (coords.x + direction.shift(1 ).x, coords.y + direction.shift(1 ).y, coords.z + direction.shift(1 ).z))
27
27
}
28
28
@@ -51,16 +51,16 @@ object GameRuleLogic {
51
51
}
52
52
53
53
/* * Whether the Bee is completely blocked.
54
- * @return true iff [freeBeeNeighbours ] returns 0 */
54
+ * @return true iff [freeBeeNeighbors ] returns 0 */
55
55
@JvmStatic
56
56
fun isBeeBlocked (board : Board , color : PlayerColor ): Boolean =
57
- freeBeeNeighbours (board, color) == 0
57
+ freeBeeNeighbors (board, color) == 0
58
58
59
59
/* * @return number of free (empty & not obstructed) fields around the Bee - -1 if no Bee has been placed. */
60
60
@JvmStatic
61
- fun freeBeeNeighbours (board : Board , color : PlayerColor ): Int =
61
+ fun freeBeeNeighbors (board : Board , color : PlayerColor ): Int =
62
62
board.fields.find { it.pieces.contains(Piece (color, PieceType .BEE )) }
63
- ?.let { getNeighbours (board, it) }?.count { field -> field.isEmpty }
63
+ ?.let { getNeighbors (board, it) }?.count { field -> field.isEmpty }
64
64
? : - 1
65
65
66
66
/* * @return true iff the given [coords] are within the Board size. */
@@ -108,7 +108,7 @@ object GameRuleLogic {
108
108
if (ownedFields.isEmpty()) {
109
109
val otherPlayerFields = gameState.board.fields.filter { it.owner == gameState.otherPlayerColor }
110
110
if (otherPlayerFields.isNotEmpty()) {
111
- if (move.destination !in otherPlayerFields.flatMap { getNeighbours (gameState.board, it.coordinates) })
111
+ if (move.destination !in otherPlayerFields.flatMap { getNeighbors (gameState.board, it.coordinates) })
112
112
throw InvalidMoveException (" Your first piece has to touch the piece of the other player" , move)
113
113
}
114
114
} else {
@@ -118,11 +118,11 @@ object GameRuleLogic {
118
118
if (gameState.round >= 3 && ! gameState.hasPlayerPlacedBee() && move.piece.type != PieceType .BEE )
119
119
throw InvalidMoveException (" The bee must be placed in fourth round latest" , move)
120
120
121
- val destinationNeighbours = getNeighbours (gameState.board, move.destination)
122
- if (! destinationNeighbours .any { it.owner == gameState.currentPlayerColor })
121
+ val destinationNeighbors = getNeighbors (gameState.board, move.destination)
122
+ if (! destinationNeighbors .any { it.owner == gameState.currentPlayerColor })
123
123
throw InvalidMoveException (" A newly placed piece must touch an own piece" , move)
124
124
125
- if (destinationNeighbours .any { it.owner == gameState.otherPlayerColor })
125
+ if (destinationNeighbors .any { it.owner == gameState.otherPlayerColor })
126
126
throw InvalidMoveException (" A newly placed piece must not touch a piece of the other player" , move)
127
127
}
128
128
}
@@ -174,7 +174,7 @@ object GameRuleLogic {
174
174
var index = 0
175
175
do {
176
176
val currentField = visitedFields[index]
177
- val newFields = getAccessibleNeighbours (board, currentField).filterNot { it in visitedFields }
177
+ val newFields = getAccessibleNeighbors (board, currentField).filterNot { it in visitedFields }
178
178
if (move.destination in newFields)
179
179
return
180
180
visitedFields.addAll(newFields)
@@ -189,24 +189,24 @@ object GameRuleLogic {
189
189
var index = 0
190
190
do {
191
191
val currentField = visitedFields[index]
192
- val occupiedNeighbours = getNeighbours (board, currentField.coordinates)
192
+ val occupiedNeighbors = getNeighbors (board, currentField.coordinates)
193
193
.filterTo(ArrayList ()) { it.pieces.isNotEmpty() }
194
- occupiedNeighbours .removeAll(visitedFields)
195
- visitedFields.addAll(occupiedNeighbours )
194
+ occupiedNeighbors .removeAll(visitedFields)
195
+ visitedFields.addAll(occupiedNeighbors )
196
196
if (visitedFields.sumBy { it.pieces.size } == totalPieces)
197
197
return true
198
198
} while (++ index < visitedFields.size)
199
199
return false
200
200
}
201
201
202
202
@JvmStatic
203
- fun getAccessibleNeighbours (board : Board , start : CubeCoordinates ) =
204
- getAccessibleNeighboursExcept (board, start, null )
203
+ fun getAccessibleNeighbors (board : Board , start : CubeCoordinates ) =
204
+ getAccessibleNeighborsExcept (board, start, null )
205
205
206
206
@JvmStatic
207
- fun getAccessibleNeighboursExcept (board : Board , start : CubeCoordinates , except : CubeCoordinates ? ) =
208
- getNeighbours (board, start).filter { neighbour ->
209
- neighbour .isEmpty && canMoveBetweenExcept(board, start, neighbour , except) && neighbour .coordinates != except
207
+ fun getAccessibleNeighborsExcept (board : Board , start : CubeCoordinates , except : CubeCoordinates ? ) =
208
+ getNeighbors (board, start).filter { neighbor ->
209
+ neighbor .isEmpty && canMoveBetweenExcept(board, start, neighbor , except) && neighbor .coordinates != except
210
210
}
211
211
212
212
@Throws(InvalidMoveException ::class )
@@ -221,7 +221,7 @@ object GameRuleLogic {
221
221
@JvmStatic
222
222
fun validateBeetleMove (board : Board , move : DragMove ) {
223
223
validateDestinationNextToStart(move)
224
- if ((sharedNeighboursOfTwoCoords (board, move.start, move.destination) + board.getField(move.destination) + board.getField(move.start)).all { it.pieces.isEmpty() })
224
+ if ((sharedNeighborsOfTwoCoords (board, move.start, move.destination) + board.getField(move.destination) + board.getField(move.start)).all { it.pieces.isEmpty() })
225
225
throw InvalidMoveException (" Beetle has to move along swarm" , move)
226
226
}
227
227
@@ -231,7 +231,7 @@ object GameRuleLogic {
231
231
if (! twoFieldsOnOneStraight(move.start, move.destination)) {
232
232
throw InvalidMoveException (" Grasshopper can only move in straight lines" , move)
233
233
}
234
- if (isNeighbour (move.start, move.destination)) {
234
+ if (isNeighbor (move.start, move.destination)) {
235
235
throw InvalidMoveException (" Grasshopper has to jump over at least one piece" , move)
236
236
}
237
237
if (getLineBetweenCoords(board, move.start, move.destination).any { ! it.hasOwner }) {
@@ -246,7 +246,7 @@ object GameRuleLogic {
246
246
paths.add(arrayOf(move.start))
247
247
do {
248
248
val currentPath = paths.removeFirst()
249
- val newFields = getAccessibleNeighbours (board, currentPath.last()).filterNot { it in currentPath }
249
+ val newFields = getAccessibleNeighbors (board, currentPath.last()).filterNot { it in currentPath }
250
250
if (currentPath.size < 3 )
251
251
paths.addAll(newFields.map { currentPath + it })
252
252
else if (move.destination in newFields)
@@ -282,32 +282,32 @@ object GameRuleLogic {
282
282
283
283
@JvmStatic
284
284
fun canMoveBetweenExcept (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates , except : CubeCoordinates ? ): Boolean =
285
- sharedNeighboursOfTwoCoords (board, coords1, coords2).filterNot { it.pieces.size == 1 && except == it.coordinates }.let { shared ->
285
+ sharedNeighborsOfTwoCoords (board, coords1, coords2).filterNot { it.pieces.size == 1 && except == it.coordinates }.let { shared ->
286
286
(shared.size == 1 || shared.any { it.isEmpty && ! it.isObstructed }) && shared.any { it.pieces.isNotEmpty() }
287
287
}
288
288
289
- /* * Checks that start and destination of this [DragMove] are neighbours .
290
- * @throws InvalidMoveException if start and destination are not neighbours */
289
+ /* * Checks that start and destination of this [DragMove] are neighbors .
290
+ * @throws InvalidMoveException if start and destination are not neighbors */
291
291
@Throws(InvalidMoveException ::class )
292
292
@JvmStatic
293
293
fun validateDestinationNextToStart (move : DragMove ) {
294
- if (! this .isNeighbour (move.start, move.destination))
294
+ if (! this .isNeighbor (move.start, move.destination))
295
295
throw InvalidMoveException (" Destination field is not next to start field" , move)
296
296
}
297
297
298
298
@JvmStatic
299
- fun isNeighbour (start : CubeCoordinates , destination : CubeCoordinates ): Boolean =
299
+ fun isNeighbor (start : CubeCoordinates , destination : CubeCoordinates ): Boolean =
300
300
start.distanceTo(destination) == 1
301
301
302
302
/* * @return true iff the two fields have an axis in common. */
303
303
@JvmStatic
304
304
fun twoFieldsOnOneStraight (coords1 : CubeCoordinates , coords2 : CubeCoordinates ): Boolean =
305
305
coords1.x == coords2.x || coords1.y == coords2.y || coords1.z == coords2.z
306
306
307
- /* * @return the Fields (up to 2) that are neighbours of both fields. */
307
+ /* * @return the Fields (up to 2) that are neighbors of both fields. */
308
308
@JvmStatic
309
- fun sharedNeighboursOfTwoCoords (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates ): Collection <Field > =
310
- getNeighbours (board, coords1).intersect(getNeighbours (board, coords2))
309
+ fun sharedNeighborsOfTwoCoords (board : Board , coords1 : CubeCoordinates , coords2 : CubeCoordinates ): Collection <Field > =
310
+ getNeighbors (board, coords1).intersect(getNeighbors (board, coords2))
311
311
312
312
/* * @return true iff the current Player has to place the Bee in this round. */
313
313
@JvmStatic
@@ -340,8 +340,8 @@ object GameRuleLogic {
340
340
341
341
return gameState.board.getFieldsOwnedBy(gameState.currentPlayerColor).flatMap { startField ->
342
342
when (startField.topPiece?.type) {
343
- PieceType .BEE -> this .getAccessibleNeighbours (gameState.board, startField)
344
- PieceType .BEETLE -> this .getNeighbours (gameState.board, startField)
343
+ PieceType .BEE -> this .getAccessibleNeighbors (gameState.board, startField)
344
+ PieceType .BEETLE -> this .getNeighbors (gameState.board, startField)
345
345
else -> this .getEmptyFieldsConnectedToSwarm(gameState.board)
346
346
}.mapNotNull { destination: CubeCoordinates ->
347
347
val move = DragMove (startField, destination)
@@ -358,16 +358,16 @@ object GameRuleLogic {
358
358
@JvmStatic
359
359
fun getEmptyFieldsConnectedToSwarm (board : Board ): Set <CubeCoordinates > =
360
360
board.fields.filter { it.hasOwner }
361
- .flatMap { this .getNeighbours (board, it).filter { it.isEmpty } }
361
+ .flatMap { this .getNeighbors (board, it).filter { it.isEmpty } }
362
362
.toSet()
363
363
364
364
@JvmStatic
365
365
fun getPossibleSetMoveDestinations (board : Board , owner : PlayerColor ): Collection <CubeCoordinates > =
366
366
board.getFieldsOwnedBy(owner)
367
367
.asSequence()
368
- .flatMap { this .getNeighbours (board, it).asSequence() }
368
+ .flatMap { this .getNeighbors (board, it).asSequence() }
369
369
.filter { it.isEmpty }
370
- .filter { this .getNeighbours (board, it).all { it.owner != owner.opponent() } }
370
+ .filter { this .getNeighbors (board, it).all { it.owner != owner.opponent() } }
371
371
.toSet()
372
372
373
373
@JvmStatic
@@ -380,7 +380,7 @@ object GameRuleLogic {
380
380
gameState.board.fields.filter { it.isEmpty }
381
381
} else {
382
382
gameState.board.getFieldsOwnedBy(gameState.otherPlayerColor)
383
- .flatMap { getNeighbours (gameState.board, it).filter { it.isEmpty } }
383
+ .flatMap { getNeighbors (gameState.board, it).filter { it.isEmpty } }
384
384
}
385
385
} else {
386
386
this .getPossibleSetMoveDestinations(gameState.board, gameState.currentPlayerColor)
0 commit comments