summaryrefslogtreecommitdiff
path: root/game-engine
diff options
context:
space:
mode:
Diffstat (limited to 'game-engine')
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Boards.kt15
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt16
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/BoardsKtTest.kt84
3 files changed, 105 insertions, 10 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Boards.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Boards.kt
index 2dd481ca..470b4efa 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Boards.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/api/Boards.kt
@@ -23,7 +23,7 @@ data class Board(
val publicProduction: ApiProduction,
val science: ApiScience,
val military: ApiMilitary,
- val playedCards: List<TableCard>,
+ val playedCards: List<List<TableCard>>,
val gold: Int
)
@@ -34,10 +34,21 @@ internal fun InternalBoard.toApiBoard(player: Player, lastMove: Move?): Board =
publicProduction = publicProduction.toApiProduction(),
science = science.toApiScience(),
military = military.toApiMilitary(),
- playedCards = getPlayedCards().map { it.toTableCard(lastMove) },
+ playedCards = getPlayedCards().map { it.toTableCard(lastMove) }.toColumns(),
gold = gold
)
+internal fun List<TableCard>.toColumns(): List<List<TableCard>> {
+ val cardsByColor = this.groupBy { it.color }
+ val (resourceCardsCols, otherCols) = cardsByColor.values.partition { it[0].color.isResource }
+ val resourceCardsCol = resourceCardsCols.flatten()
+ val otherColsSorted = otherCols.sortedBy { it[0].color }
+ if (resourceCardsCol.isEmpty()) {
+ return otherColsSorted // we want only non-empty columns
+ }
+ return listOf(resourceCardsCol) + otherColsSorted
+}
+
data class Wonder(
val name: String,
val initialResource: ResourceType,
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt
index cf4df44e..18d9cb96 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/cards/Cards.kt
@@ -41,14 +41,14 @@ internal data class Card(
}
}
-enum class Color {
- BROWN,
- GREY,
- YELLOW,
- BLUE,
- GREEN,
- RED,
- PURPLE
+enum class Color(val isResource: Boolean) {
+ BROWN(true),
+ GREY(true),
+ YELLOW(false),
+ BLUE(false),
+ GREEN(false),
+ RED(false),
+ PURPLE(false)
}
data class CardPlayability(
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/BoardsKtTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/BoardsKtTest.kt
new file mode 100644
index 00000000..5cf60eaf
--- /dev/null
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/api/BoardsKtTest.kt
@@ -0,0 +1,84 @@
+package org.luxons.sevenwonders.game.api
+
+import org.junit.Test
+
+import org.junit.Assert.*
+import org.luxons.sevenwonders.game.cards.Color
+import org.luxons.sevenwonders.game.test.testCard
+
+class BoardsKtTest {
+
+ @Test
+ fun `toColumns on empty list should return no cols`() {
+ val cols = emptyList<TableCard>().toColumns()
+ assertEquals(emptyList<List<TableCard>>(), cols)
+ }
+
+ @Test
+ fun `toColumns with single resource should return a single column`() {
+ val card = testCard(color = Color.BROWN).toTableCard()
+ val cols = listOf(card).toColumns()
+ assertEquals(listOf(listOf(card)), cols)
+ }
+
+ @Test
+ fun `toColumns with single non-resource should return a single column`() {
+ val card = testCard(color = Color.BLUE).toTableCard()
+ val cols = listOf(card).toColumns()
+ assertEquals(listOf(listOf(card)), cols)
+ }
+
+ @Test
+ fun `toColumns with two same-color cards should return a single column`() {
+ val card1 = testCard(color = Color.BLUE).toTableCard()
+ val card2 = testCard(color = Color.BLUE).toTableCard()
+ val cards = listOf(card1, card2)
+ val cols = cards.toColumns()
+ assertEquals(listOf(cards), cols)
+ }
+
+ @Test
+ fun `toColumns with two resource cards should return a single column`() {
+ val card1 = testCard(color = Color.BROWN).toTableCard()
+ val card2 = testCard(color = Color.GREY).toTableCard()
+ val cards = listOf(card1, card2)
+ val cols = cards.toColumns()
+ assertEquals(listOf(cards), cols)
+ }
+
+ @Test
+ fun `toColumns with 2 different non-resource cards should return 2 columns`() {
+ val card1 = testCard(color = Color.BLUE).toTableCard()
+ val card2 = testCard(color = Color.GREEN).toTableCard()
+ val cards = listOf(card1, card2)
+ val cols = cards.toColumns()
+ assertEquals(listOf(listOf(card1), listOf(card2)), cols)
+ }
+
+ @Test
+ fun `toColumns with 1 res and 1 non-res card should return 2 columns`() {
+ val card1 = testCard(color = Color.BROWN).toTableCard()
+ val card2 = testCard(color = Color.GREEN).toTableCard()
+ val cards = listOf(card1, card2)
+ val cols = cards.toColumns()
+ assertEquals(listOf(listOf(card1), listOf(card2)), cols)
+ }
+
+ @Test
+ fun `toColumns should return 1 col for res cards and 1 for each other color`() {
+ val res1 = testCard(color = Color.BROWN).toTableCard()
+ val res2 = testCard(color = Color.BROWN).toTableCard()
+ val res3 = testCard(color = Color.GREY).toTableCard()
+ val blue1 = testCard(color = Color.BLUE).toTableCard()
+ val green1 = testCard(color = Color.GREEN).toTableCard()
+ val green2 = testCard(color = Color.GREEN).toTableCard()
+ val cards = listOf(res1, green1, green2, res2, blue1, res3)
+ val cols = cards.toColumns()
+ val expectedCols = listOf(
+ listOf(res1, res2, res3),
+ listOf(blue1),
+ listOf(green1, green2)
+ )
+ assertEquals(expectedCols, cols)
+ }
+}
bgstack15