diff options
author | jbion <joffrey.bion@amadeus.com> | 2018-07-08 13:30:41 +0200 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2018-07-08 13:34:13 +0200 |
commit | 3b844531a2f9ad8037c9666cfff00916b5321cf9 (patch) | |
tree | a8505a260ab6cccf4fb3c153c3a1009b0d5a559e /game-engine | |
parent | Kotlin mig: api package tests (diff) | |
download | seven-wonders-3b844531a2f9ad8037c9666cfff00916b5321cf9.tar.gz seven-wonders-3b844531a2f9ad8037c9666cfff00916b5321cf9.tar.bz2 seven-wonders-3b844531a2f9ad8037c9666cfff00916b5321cf9.zip |
Kotlin mig: wonders package
Diffstat (limited to 'game-engine')
9 files changed, 118 insertions, 205 deletions
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java deleted file mode 100644 index dd0170da..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.luxons.sevenwonders.game.wonders; - -import java.util.Arrays; -import java.util.List; - -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.cards.CardBack; -import org.luxons.sevenwonders.game.resources.ResourceTransactions; -import org.luxons.sevenwonders.game.resources.ResourceType; - -public class Wonder { - - private String name; - - private ResourceType initialResource; - - private List<WonderStage> stages; - - private String image; - - public Wonder() { - } - - public Wonder(String name, ResourceType initialResource, WonderStage... stages) { - this.name = name; - this.initialResource = initialResource; - this.stages = Arrays.asList(stages); - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public ResourceType getInitialResource() { - return initialResource; - } - - public void setInitialResource(ResourceType initialResource) { - this.initialResource = initialResource; - } - - public List<WonderStage> getStages() { - return stages; - } - - public void setStages(List<WonderStage> stages) { - this.stages = stages; - } - - public int getNbBuiltStages() { - return (int) stages.stream().filter(WonderStage::isBuilt).count(); - } - - public String getImage() { - return image; - } - - public void setImage(String image) { - this.image = image; - } - - public boolean isNextStageBuildable(Table table, int playerIndex, ResourceTransactions boughtResources) { - int nextLevel = getNbBuiltStages(); - if (nextLevel == stages.size()) { - return false; - } - return getNextStage().isBuildable(table, playerIndex, boughtResources); - } - - public void buildLevel(CardBack cardBack) { - getNextStage().build(cardBack); - } - - private WonderStage getNextStage() { - int nextLevel = getNbBuiltStages(); - if (nextLevel == stages.size()) { - throw new IllegalStateException("This wonder has already reached its maximum level"); - } - return stages.get(nextLevel); - } - - public void activateLastBuiltStage(Table table, int playerIndex, ResourceTransactions boughtResources) { - getLastBuiltStage().activate(table, playerIndex, boughtResources); - } - - private WonderStage getLastBuiltStage() { - int lastLevel = getNbBuiltStages() - 1; - return stages.get(lastLevel); - } - - public int computePoints(Table table, int playerIndex) { - return stages.stream() - .filter(WonderStage::isBuilt) - .flatMap(c -> c.getEffects().stream()) - .mapToInt(e -> e.computePoints(table, playerIndex)) - .sum(); - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java deleted file mode 100644 index f4ec1140..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.luxons.sevenwonders.game.wonders; - -import java.util.List; - -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.boards.Board; -import org.luxons.sevenwonders.game.cards.CardBack; -import org.luxons.sevenwonders.game.cards.Requirements; -import org.luxons.sevenwonders.game.effects.Effect; -import org.luxons.sevenwonders.game.resources.ResourceTransactions; - -public class WonderStage { - - private final Requirements requirements; - - private final List<Effect> effects; - - private CardBack cardBack; - - public WonderStage(Requirements requirements, List<Effect> effects) { - this.requirements = requirements; - this.effects = effects; - } - - public Requirements getRequirements() { - return requirements; - } - - public List<Effect> getEffects() { - return effects; - } - - public CardBack getCardBack() { - return cardBack; - } - - public boolean isBuilt() { - return cardBack != null; - } - - public boolean isBuildable(Table table, int playerIndex, ResourceTransactions boughtResources) { - Board board = table.getBoard(playerIndex); - return requirements.areMetWithHelpBy(board, boughtResources); - } - - void build(CardBack cardBack) { - this.cardBack = cardBack; - } - - void activate(Table table, int playerIndex, ResourceTransactions boughtResources) { - effects.forEach(e -> e.apply(table, playerIndex)); - requirements.pay(table, playerIndex, boughtResources); - } -} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt index 558f16e8..013a3d9c 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/WonderDefinition.kt @@ -9,13 +9,8 @@ internal data class WonderDefinition( private val sides: Map<WonderSide, WonderSideDefinition> ) { fun create(settings: Settings): Wonder { - val wonder = Wonder() - wonder.name = name - val wonderSideDef = sides[settings.pickWonderSide()]!! - wonder.initialResource = wonderSideDef.initialResource - wonder.stages = wonderSideDef.createStages() - wonder.image = wonderSideDef.image - return wonder + val stages = wonderSideDef.createStages() + return Wonder(name, wonderSideDef.initialResource, stages, wonderSideDef.image) } } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt new file mode 100644 index 00000000..beb87580 --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/Wonder.kt @@ -0,0 +1,45 @@ +package org.luxons.sevenwonders.game.wonders + +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.resources.ResourceTransactions +import org.luxons.sevenwonders.game.resources.ResourceType + +class Wonder( + val name: String, + val initialResource: ResourceType, + val stages: List<WonderStage>, + val image: String +) { + val nbBuiltStages: Int + get() = stages.filter { it.isBuilt }.count() + + private val nextStage: WonderStage + get() { + val nextLevel = nbBuiltStages + if (nextLevel == stages.size) { + throw IllegalStateException("This wonder has already reached its maximum level") + } + return stages[nextLevel] + } + + private val lastBuiltStage: WonderStage + get() { + val lastLevel = nbBuiltStages - 1 + return stages[lastLevel] + } + + fun isNextStageBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean = + nbBuiltStages < stages.size && nextStage.isBuildable(table, playerIndex, boughtResources) + + fun buildLevel(cardBack: CardBack) = nextStage.build(cardBack) + + fun activateLastBuiltStage(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) = + lastBuiltStage.activate(table, playerIndex, boughtResources) + + fun computePoints(table: Table, playerIndex: Int): Int = stages + .filter { it.isBuilt } + .flatMap { c -> c.effects } + .map { e -> e.computePoints(table, playerIndex) } + .sum() +} diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt new file mode 100644 index 00000000..6aad135d --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/wonders/WonderStage.kt @@ -0,0 +1,30 @@ +package org.luxons.sevenwonders.game.wonders + +import org.luxons.sevenwonders.game.api.Table +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.cards.Requirements +import org.luxons.sevenwonders.game.effects.Effect +import org.luxons.sevenwonders.game.resources.ResourceTransactions + +class WonderStage(val requirements: Requirements, val effects: List<Effect>) { + + var cardBack: CardBack? = null + private set + + val isBuilt: Boolean + get() = cardBack != null + + fun isBuildable(table: Table, playerIndex: Int, boughtResources: ResourceTransactions): Boolean { + val board = table.getBoard(playerIndex) + return requirements.areMetWithHelpBy(board, boughtResources) + } + + internal fun build(cardBack: CardBack) { + this.cardBack = cardBack + } + + internal fun activate(table: Table, playerIndex: Int, boughtResources: ResourceTransactions) { + effects.forEach { e -> e.apply(table, playerIndex) } + requirements.pay(table, playerIndex, boughtResources) + } +} diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/wonders/WonderTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/wonders/WonderTest.java deleted file mode 100644 index a7988f8a..00000000 --- a/game-engine/src/test/java/org/luxons/sevenwonders/game/wonders/WonderTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.luxons.sevenwonders.game.wonders; - -import org.junit.Test; -import org.luxons.sevenwonders.game.cards.CardBack; -import org.luxons.sevenwonders.game.test.TestUtilsKt; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -public class WonderTest { - - @Test - public void buildLevel_increasesNbBuiltStages() { - Wonder wonder = TestUtilsKt.testWonder(); - assertEquals(0, wonder.getNbBuiltStages()); - wonder.buildLevel(new CardBack("img")); - assertEquals(1, wonder.getNbBuiltStages()); - wonder.buildLevel(new CardBack("img")); - assertEquals(2, wonder.getNbBuiltStages()); - wonder.buildLevel(new CardBack("img")); - assertEquals(3, wonder.getNbBuiltStages()); - } - - @Test - public void buildLevel_failsIfFull() { - Wonder wonder = TestUtilsKt.testWonder(); - wonder.buildLevel(new CardBack("img")); - wonder.buildLevel(new CardBack("img")); - wonder.buildLevel(new CardBack("img")); - try { - wonder.buildLevel(new CardBack("img")); - fail(); - } catch (IllegalStateException e) { - // expected exception because there is no 4th level in this wonder - } - } -} diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt index 3253136c..783e4c38 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/cards/CardTest.kt @@ -10,7 +10,6 @@ import org.luxons.sevenwonders.game.effects.ProductionIncrease import org.luxons.sevenwonders.game.resources.Production import org.luxons.sevenwonders.game.resources.ResourceTransactions import org.luxons.sevenwonders.game.resources.ResourceType -import org.luxons.sevenwonders.game.resources.Resources import org.luxons.sevenwonders.game.test.testCard import org.luxons.sevenwonders.game.wonders.Wonder @@ -25,9 +24,9 @@ class CardTest { val settings = Settings(3) val boards = listOf( - Board(Wonder("TestWonder", ResourceType.WOOD), 0, settings), - Board(Wonder("TestWonder", ResourceType.STONE), 1, settings), - Board(Wonder("TestWonder", ResourceType.PAPYRUS), 2, settings) + Board(Wonder("TestWonder", ResourceType.WOOD, emptyList(), ""), 0, settings), + Board(Wonder("TestWonder", ResourceType.STONE, emptyList(), ""), 1, settings), + Board(Wonder("TestWonder", ResourceType.PAPYRUS, emptyList(), ""), 2, settings) ) table = Table(boards) diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt index 28ab714d..feda9423 100644 --- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/test/TestUtils.kt @@ -78,7 +78,7 @@ fun testWonder(initialResource: ResourceType = ResourceType.WOOD): Wonder { val stage1 = createWonderStage() val stage2 = createWonderStage() val stage3 = createWonderStage() - return Wonder("Test Wonder " + initialResource.symbol!!, initialResource, stage1, stage2, stage3) + return Wonder("Test Wonder " + initialResource.symbol!!, initialResource, listOf(stage1, stage2, stage3), "") } private fun createWonderStage(vararg effects: Effect): WonderStage { diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt new file mode 100644 index 00000000..9270bcca --- /dev/null +++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/wonders/WonderTest.kt @@ -0,0 +1,37 @@ +package org.luxons.sevenwonders.game.wonders + +import org.junit.Test +import org.luxons.sevenwonders.game.cards.CardBack +import org.luxons.sevenwonders.game.test.* + +import org.junit.Assert.assertEquals +import org.junit.Assert.fail + +class WonderTest { + + @Test + fun buildLevel_increasesNbBuiltStages() { + val wonder = testWonder() + assertEquals(0, wonder.nbBuiltStages.toLong()) + wonder.buildLevel(CardBack("img")) + assertEquals(1, wonder.nbBuiltStages.toLong()) + wonder.buildLevel(CardBack("img")) + assertEquals(2, wonder.nbBuiltStages.toLong()) + wonder.buildLevel(CardBack("img")) + assertEquals(3, wonder.nbBuiltStages.toLong()) + } + + @Test + fun buildLevel_failsIfFull() { + val wonder = testWonder() + wonder.buildLevel(CardBack("img")) + wonder.buildLevel(CardBack("img")) + wonder.buildLevel(CardBack("img")) + try { + wonder.buildLevel(CardBack("img")) + fail() + } catch (e: IllegalStateException) { + // expected exception because there is no 4th level in this wonder + } + } +} |