Skip to content

Commit 2334d42

Browse files
committed
feat(plugin): randomise and check starting piece shape
1 parent 0327d8d commit 2334d42

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

plugin/src/shared/sc/plugin2021/GameState.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class GameState @JvmOverloads constructor(
1414
override var second: Player = Player(Team.TWO),
1515
override var lastMove: Move? = null,
1616
startTurn: Int = 1,
17-
val startColor: Color = Color.BLUE
17+
val startColor: Color = Color.BLUE,
18+
@XStreamAsAttribute val startPiece: PieceShape = GameRuleLogic.getRandomPentomino()
1819
): TwoPlayerGameState<Player>(Team.ONE) {
1920

2021
@XStreamAsAttribute

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ object GameRuleLogic {
7575
throw InvalidMoveException("Field $it already borders on ${move.color}", move)
7676
}
7777
if (gameState.deployedPieces[move.color].isNullOrEmpty()) {
78-
// TODO: Check if the piece is the predetermined pentomino
79-
if (move.piece.coordinates.size < 5)
80-
throw InvalidMoveException("Piece ${move.piece.kind} is not a pentomino", move)
78+
if (move.piece.kind != gameState.startPiece)
79+
throw InvalidMoveException("Expected the predetermined staring piece, ${gameState.startPiece}", move)
8180
// Check if it is placed correctly in a corner
8281
if (move.piece.coordinates.none { isOnCorner(it)})
8382
// TODO: Add expected move to exception
@@ -123,4 +122,12 @@ object GameRuleLogic {
123122
Coordinates(Constants.BOARD_SIZE - 1, 0),
124123
Coordinates(Constants.BOARD_SIZE - 1, Constants.BOARD_SIZE - 1),
125124
Coordinates(0, Constants.BOARD_SIZE - 1)).contains(position)
125+
126+
/** Returns a random pentomino which is not the `x` one (Used to get a valid starting piece). */
127+
@JvmStatic
128+
fun getRandomPentomino() =
129+
PieceShape.values()
130+
.slice(9 until Constants.TOTAL_PIECE_SHAPES)
131+
.filter{ it.size == 5 && it != PieceShape.PENTO_X }
132+
.random()
126133
}

plugin/src/test/sc/plugin2021/GameRuleLogicTest.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import sc.shared.InvalidMoveException
1212

1313
class GameRuleLogicTest: StringSpec({
1414
"Color validation works correctly" {
15-
val gameState = GameState()
15+
val gameState = GameState(startPiece = PieceShape.PENTO_U)
1616

1717
assertThrows<InvalidMoveException> {
1818
val invalidMove = SetMove(Piece(Color.RED))
@@ -47,7 +47,8 @@ class GameRuleLogicTest: StringSpec({
4747
// PENTO_S is: # # #
4848
// : # #
4949
val invalidPieces = listOf(
50-
Piece(Color.BLUE, PieceShape.TETRO_O),
50+
Piece(Color.GREEN, PieceShape.TETRO_O),
51+
Piece(Color.BLUE, PieceShape.PENTO_S),
5152
Piece(Color.YELLOW, PieceShape.PENTO_S, Rotation.RIGHT, position = Coordinates(Constants.BOARD_SIZE - 4, 0)),
5253
Piece(Color.RED, PieceShape.PENTO_S, position = Coordinates(13, 5)),
5354
Piece(Color.GREEN, PieceShape.PENTO_S, Rotation.LEFT, position = Coordinates(Constants.BOARD_SIZE - 2, 0)),
@@ -61,7 +62,7 @@ class GameRuleLogicTest: StringSpec({
6162
)
6263

6364
assertDoesNotThrow {
64-
val gameState = GameState()
65+
val gameState = GameState(startPiece = PieceShape.PENTO_S)
6566
GameRuleLogic.performMove(gameState, SetMove(validPieces.first()))
6667
assertThrows<InvalidMoveException> {
6768
invalidPieces.forEach {
@@ -70,11 +71,8 @@ class GameRuleLogicTest: StringSpec({
7071
}
7172
}
7273
}
73-
validPieces.forEach{
74-
it.coordinates.print(Vector(5, 5)).also{println()}
75-
}
7674
assertDoesNotThrow {
77-
val gameState = GameState()
75+
val gameState = GameState(startPiece = PieceShape.PENTO_S)
7876
validPieces.forEach {
7977
GameRuleLogic.performMove(gameState, SetMove(it))
8078
gameState.turn++

plugin/src/test/sc/plugin2021/GameStateTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class GameStateTest: StringSpec({
4545
gameState.currentColor shouldBe Color.YELLOW
4646
}
4747
"Pieces can only be placed once" {
48-
val gameState = GameState()
48+
val gameState = GameState(startPiece = PieceShape.PENTO_I)
4949
val move = SetMove(
5050
Piece(Color.BLUE, PieceShape.PENTO_I, Rotation.RIGHT, true))
5151

0 commit comments

Comments
 (0)