Skip to content

Commit b27f1e1

Browse files
committed
refactor(plugin): move shape validation into it's own function
refactor(plugin): rearrange color validation
1 parent 9e9b867 commit b27f1e1

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,14 @@ object GameRuleLogic {
6262
/** Checks if the given [move] is able to be performed for the given [gameState]. */
6363
@JvmStatic
6464
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
6970
validateSetMove(gameState.board, move)
7071

7172
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)
7573
// Check if it is placed correctly in a corner
7674
if (move.piece.coordinates.none { isOnCorner(it)})
7775
throw InvalidMoveException("The Piece isn't located in a corner", move)
@@ -82,7 +80,19 @@ object GameRuleLogic {
8280
}
8381
}
8482

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]. */
8696
@JvmStatic
8797
fun validateSetMove(board: Board, move: SetMove) {
8898
move.piece.coordinates.forEach {

plugin/src/shared/sc/plugin2021/util/SetHelpers.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,4 @@ fun printShapes(vararg shapes: Set<Coordinates>, dimension: Vector = Vector(4, 5
9696

9797
/** Filters all moves, returning only those who pass the validation functions. */
9898
fun Set<SetMove>.filterValidMoves(gameState: GameState): Set<SetMove> =
99-
filter {
100-
try {
101-
GameRuleLogic.validateMoveColor(gameState, it)
102-
GameRuleLogic.validateSetMove(gameState, it)
103-
true
104-
} catch(e: InvalidMoveException) {
105-
false
106-
}
107-
}.toSet()
99+
filter { GameRuleLogic.isValidSetMove(gameState, it) }.toSet()

0 commit comments

Comments
 (0)