diff --git a/plugin/src/shared/sc/plugin2021/Color.kt b/plugin/src/shared/sc/plugin2021/Color.kt index 5fe28554a..99f538090 100644 --- a/plugin/src/shared/sc/plugin2021/Color.kt +++ b/plugin/src/shared/sc/plugin2021/Color.kt @@ -3,11 +3,11 @@ package sc.plugin2021 import com.thoughtworks.xstream.annotations.XStreamAlias @XStreamAlias(value = "color") -enum class Color(val team: Team) { - BLUE (Team.ONE), - YELLOW(Team.TWO), - RED (Team.ONE), - GREEN (Team.TWO) { init { +enum class Color() { + BLUE, + YELLOW, + RED, + GREEN { init { BLUE.next = YELLOW YELLOW.next = RED RED.next = GREEN @@ -16,7 +16,13 @@ enum class Color(val team: Team) { lateinit var next: Color private set - + + val team: Team + get() = when(this) { + BLUE, RED -> Team.ONE + YELLOW, GREEN -> Team.TWO + } + operator fun unaryPlus(): FieldContent = when(this) { BLUE -> FieldContent.BLUE YELLOW -> FieldContent.YELLOW diff --git a/plugin/src/shared/sc/plugin2021/GameState.kt b/plugin/src/shared/sc/plugin2021/GameState.kt index 43806ba9e..62ef27491 100644 --- a/plugin/src/shared/sc/plugin2021/GameState.kt +++ b/plugin/src/shared/sc/plugin2021/GameState.kt @@ -28,12 +28,19 @@ class GameState @JvmOverloads constructor( @XStreamAsAttribute override val board: Board = Board() - - @XStreamAsAttribute - val undeployedPieceShapes: Map> = Color.values().map { - it to PieceShape.values().toMutableSet() - }.toMap() - + + fun undeployedPieceShapes(color: Color): MutableSet = when (color) { + Color.BLUE -> blueShapes + Color.YELLOW -> yellowShapes + Color.RED -> redShapes + Color.GREEN -> greenShapes + } + + private val blueShapes: MutableSet = PieceShape.values().toMutableSet() + private val yellowShapes: MutableSet = PieceShape.values().toMutableSet() + private val redShapes: MutableSet = PieceShape.values().toMutableSet() + private val greenShapes: MutableSet = PieceShape.values().toMutableSet() + @XStreamOmitField val deployedPieces: Map> = Color.values().map { it to mutableListOf() @@ -43,12 +50,8 @@ class GameState @JvmOverloads constructor( val lastMoveMono: MutableMap = mutableMapOf() override val currentTeam - get() = when(currentColor) { - Color.BLUE, Color.RED -> Team.ONE - Color.YELLOW, Color.GREEN -> Team.TWO - } -// get() = currentColor.team - + get() = currentColor.team + override val currentPlayer get() = getPlayer(currentTeam)!! @@ -118,7 +121,7 @@ class GameState @JvmOverloads constructor( (team as Team).colors.map { getPointsForColor(it) }.sum() private fun getPointsForColor(color: Color): Int { - val pieces = undeployedPieceShapes[color] ?: return 0 + val pieces = undeployedPieceShapes(color) val lastMono = lastMoveMono[color] ?: false return GameRuleLogic.getPointsFromUndeployed(pieces, lastMono) } @@ -149,16 +152,23 @@ class GameState @JvmOverloads constructor( turn == other.turn && currentTeam == other.currentTeam } - + override fun hashCode(): Int { var result = first.hashCode() result = 31 * result + second.hashCode() result = 31 * result + (lastMove?.hashCode() ?: 0) + result = 31 * result + startColor.hashCode() + result = 31 * result + startPiece.hashCode() result = 31 * result + board.hashCode() - result = 31 * result + undeployedPieceShapes.hashCode() - result = 31 * result + currentTeam.hashCode() + result = 31 * result + blueShapes.hashCode() + result = 31 * result + yellowShapes.hashCode() + result = 31 * result + redShapes.hashCode() + result = 31 * result + greenShapes.hashCode() + result = 31 * result + lastMoveMono.hashCode() + result = 31 * result + orderedColors.hashCode() + result = 31 * result + currentColorIndex result = 31 * result + turn + result = 31 * result + round return result } - } diff --git a/plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt b/plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt index 6460cb731..708e0b36f 100644 --- a/plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt +++ b/plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt @@ -77,11 +77,11 @@ object GameRuleLogic { validateSetMove(gameState, move) performSetMove(gameState.board, move) - gameState.undeployedPieceShapes.getValue(move.color).remove(move.piece.kind) + gameState.undeployedPieceShapes(move.color).remove(move.piece.kind) gameState.deployedPieces?.getValue(move.color).add(move.piece) // If it was the last piece for this color, remove it from the turn queue - if (gameState.undeployedPieceShapes.getValue(move.color).isEmpty()) + if (gameState.undeployedPieceShapes(move.color).isEmpty()) gameState.lastMoveMono += move.color to (move.piece.kind == PieceShape.MONO) gameState.tryAdvance() @@ -94,7 +94,7 @@ object GameRuleLogic { if (shape != gameState.startPiece) throw InvalidMoveException("$shape is not the requested first shape, ${gameState.startPiece}") } else { - if (!gameState.undeployedPieceShapes.getValue(color).contains(shape)) + if (!gameState.undeployedPieceShapes(color).contains(shape)) throw InvalidMoveException("Piece ${shape} has already been placed before") } } @@ -184,7 +184,7 @@ object GameRuleLogic { @JvmStatic fun isFirstMove(gameState: GameState) = - gameState.undeployedPieceShapes.getValue(gameState.currentColor).size == Constants.TOTAL_PIECE_SHAPES + gameState.undeployedPieceShapes(gameState.currentColor).size == Constants.TOTAL_PIECE_SHAPES /** Returns a random pentomino which is not the `x` one (Used to get a valid starting piece). */ @JvmStatic @@ -255,7 +255,7 @@ object GameRuleLogic { @JvmStatic fun streamAllPossibleMoves(gameState: GameState) = sequence { val color = gameState.currentColor - gameState.undeployedPieceShapes.getValue(color).map { + gameState.undeployedPieceShapes(color).map { val area = it.coordinates.area() for (y in 0 until Constants.BOARD_SIZE - area.dy) for (x in 0 until Constants.BOARD_SIZE - area.dx) diff --git a/plugin/src/test/sc/plugin2021/GameStateTest.kt b/plugin/src/test/sc/plugin2021/GameStateTest.kt index 2c7975b2e..01bdf711d 100644 --- a/plugin/src/test/sc/plugin2021/GameStateTest.kt +++ b/plugin/src/test/sc/plugin2021/GameStateTest.kt @@ -14,10 +14,10 @@ class GameStateTest: StringSpec({ state.board shouldBe Board() - state.undeployedPieceShapes[Color.BLUE] shouldBe PieceShape.values().toSet() - state.undeployedPieceShapes[Color.YELLOW] shouldBe PieceShape.values().toSet() - state.undeployedPieceShapes[Color.RED] shouldBe PieceShape.values().toSet() - state.undeployedPieceShapes[Color.GREEN] shouldBe PieceShape.values().toSet() + state.undeployedPieceShapes(Color.BLUE) shouldBe PieceShape.values().toSet() + state.undeployedPieceShapes(Color.YELLOW) shouldBe PieceShape.values().toSet() + state.undeployedPieceShapes(Color.RED) shouldBe PieceShape.values().toSet() + state.undeployedPieceShapes(Color.GREEN) shouldBe PieceShape.values().toSet() state.deployedPieces[Color.BLUE] shouldBe mutableListOf() state.deployedPieces[Color.YELLOW] shouldBe mutableListOf() @@ -49,12 +49,12 @@ class GameStateTest: StringSpec({ val move = SetMove( Piece(Color.BLUE, PieceShape.PENTO_I, Rotation.RIGHT, true)) - state.undeployedPieceShapes.getValue(Color.BLUE).size shouldBe 21 + state.undeployedPieceShapes(Color.BLUE).size shouldBe 21 state.deployedPieces.getValue(Color.BLUE).size shouldBe 0 assertDoesNotThrow { GameRuleLogic.performMove(state, move) } - state.undeployedPieceShapes.getValue(Color.BLUE).size shouldBe 20 + state.undeployedPieceShapes(Color.BLUE).size shouldBe 20 state.deployedPieces.getValue(Color.BLUE).size shouldBe 1 state.deployedPieces.getValue(Color.BLUE)[0] shouldBe move.piece @@ -62,7 +62,7 @@ class GameStateTest: StringSpec({ assertThrows { GameRuleLogic.performMove(state, move) } - state.undeployedPieceShapes.getValue(Color.BLUE).size shouldBe 20 + state.undeployedPieceShapes(Color.BLUE).size shouldBe 20 state.deployedPieces.getValue(Color.BLUE).size shouldBe 1 state.deployedPieces.getValue(Color.BLUE)[0] shouldBe move.piece diff --git a/plugin/src/test/sc/plugin2021/GameTest.kt b/plugin/src/test/sc/plugin2021/GameTest.kt index 602c62bec..6d47a5f4a 100644 --- a/plugin/src/test/sc/plugin2021/GameTest.kt +++ b/plugin/src/test/sc/plugin2021/GameTest.kt @@ -13,7 +13,6 @@ import sc.shared.ScoreCause class GameTest: StringSpec({ "Game starting works" { - Color.BLUE.team val game = Game() val state = game.gameState val player = Pair(game.onPlayerJoined(), game.onPlayerJoined()) @@ -66,4 +65,4 @@ class GameTest: StringSpec({ } } } -}) \ No newline at end of file +})