Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions plugin/src/shared/sc/plugin2021/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
44 changes: 27 additions & 17 deletions plugin/src/shared/sc/plugin2021/GameState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ class GameState @JvmOverloads constructor(

@XStreamAsAttribute
override val board: Board = Board()

@XStreamAsAttribute
val undeployedPieceShapes: Map<Color, MutableSet<PieceShape>> = Color.values().map {
it to PieceShape.values().toMutableSet()
}.toMap()


fun undeployedPieceShapes(color: Color): MutableSet<PieceShape> = when (color) {
Color.BLUE -> blueShapes
Color.YELLOW -> yellowShapes
Color.RED -> redShapes
Color.GREEN -> greenShapes
}

private val blueShapes: MutableSet<PieceShape> = PieceShape.values().toMutableSet()
private val yellowShapes: MutableSet<PieceShape> = PieceShape.values().toMutableSet()
private val redShapes: MutableSet<PieceShape> = PieceShape.values().toMutableSet()
private val greenShapes: MutableSet<PieceShape> = PieceShape.values().toMutableSet()

@XStreamOmitField
val deployedPieces: Map<Color, MutableList<Piece>> = Color.values().map {
it to mutableListOf<Piece>()
Expand All @@ -43,12 +50,8 @@ class GameState @JvmOverloads constructor(
val lastMoveMono: MutableMap<Color, Boolean> = 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)!!

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}

}
10 changes: 5 additions & 5 deletions plugin/src/shared/sc/plugin2021/util/GameRuleLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -255,7 +255,7 @@ object GameRuleLogic {
@JvmStatic
fun streamAllPossibleMoves(gameState: GameState) = sequence<SetMove> {
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)
Expand Down
14 changes: 7 additions & 7 deletions plugin/src/test/sc/plugin2021/GameStateTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Piece>()
state.deployedPieces[Color.YELLOW] shouldBe mutableListOf<Piece>()
Expand Down Expand Up @@ -49,20 +49,20 @@ 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

state.turn += 4
assertThrows<InvalidMoveException> {
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

Expand Down
3 changes: 1 addition & 2 deletions plugin/src/test/sc/plugin2021/GameTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -66,4 +65,4 @@ class GameTest: StringSpec({
}
}
}
})
})