Skip to content

Commit 8a42642

Browse files
committed
feat(plugin): add perform / validate move on boards
A limitted variant working without the extensive knowledge of gameStates
1 parent 9c1120c commit 8a42642

File tree

1 file changed

+29
-16
lines changed

1 file changed

+29
-16
lines changed

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

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ object GameRuleLogic {
3636
if (Constants.VALIDATE_MOVE)
3737
validateSetMove(gameState, move)
3838

39-
move.piece.coordinates.forEach {
40-
gameState.board[it] = +move.color
41-
}
39+
performSetMove(gameState.board, move)
4240
gameState.undeployedPieceShapes.getValue(move.color).remove(move.piece.kind)
4341
gameState.deployedPieces?.getValue(move.color).add(move.piece)
4442

@@ -67,20 +65,9 @@ object GameRuleLogic {
6765
// Check if piece has already been placed
6866
gameState.undeployedPieceShapes.getValue(move.color).find { it == move.piece.kind }
6967
?: throw InvalidMoveException("Piece #${move.piece.kind} has already been placed before", move)
68+
69+
validateSetMove(gameState.board, move)
7070

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-
}
8471
if (isFirstMove(gameState)) {
8572
// Check if it's the requested shape
8673
if (move.piece.kind != gameState.startPiece)
@@ -95,6 +82,32 @@ object GameRuleLogic {
9582
}
9683
}
9784

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+
98111
/** Returns true if [move] is valid, false otherwise. */
99112
@JvmStatic
100113
fun isValidSetMove(gameState: GameState, move: SetMove) =

0 commit comments

Comments
 (0)