diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2020-11-25 23:57:16 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2020-11-25 23:57:16 +0100 |
commit | d0a5abc2bd0f86be86b773e3f77712b2602bdc24 (patch) | |
tree | 0ab7bd4f540a4e6d4e090c1c4f48e5331a00f24c /sw-engine | |
parent | Upgrade dependencies (diff) | |
download | seven-wonders-d0a5abc2bd0f86be86b773e3f77712b2602bdc24.tar.gz seven-wonders-d0a5abc2bd0f86be86b773e3f77712b2602bdc24.tar.bz2 seven-wonders-d0a5abc2bd0f86be86b773e3f77712b2602bdc24.zip |
Fix resource transactions calculations
Resolves:
https://github.com/joffrey-bion/seven-wonders/issues/49
Diffstat (limited to 'sw-engine')
3 files changed, 43 insertions, 8 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt index e5f27c05..846e7fd2 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculator.kt @@ -17,7 +17,7 @@ private class ResourcePool( private val rules: TradingRules, choices: List<Set<ResourceType>>, ) { - val choices: Set<MutableSet<ResourceType>> = choices.mapTo(HashSet()) { it.toMutableSet() } + val choices: List<MutableSet<ResourceType>> = choices.map { it.toMutableSet() } fun getCost(type: ResourceType): Int = if (provider == null) 0 else rules.getCost(type, provider) } @@ -98,7 +98,7 @@ private class BestPriceCalculator(resourcesToPay: Resources, player: Player) { fun unbuyOne(provider: Provider, type: ResourceType, cost: Int) { pricePaid -= cost - boughtResources.get(provider)!!.remove(type, 1) + boughtResources[provider]!!.remove(type, 1) } private fun computePossibilitiesWhenUsing(type: ResourceType, pool: ResourcePool) { diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculatorTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculatorTest.kt index ba0fdf1b..36e4046f 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculatorTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/resources/BestPriceCalculatorTest.kt @@ -10,11 +10,7 @@ import org.luxons.sevenwonders.engine.test.testTable import org.luxons.sevenwonders.model.resources.Provider.LEFT_PLAYER import org.luxons.sevenwonders.model.resources.Provider.RIGHT_PLAYER import org.luxons.sevenwonders.model.resources.ResourceTransactions -import org.luxons.sevenwonders.model.resources.ResourceType.CLAY -import org.luxons.sevenwonders.model.resources.ResourceType.GLASS -import org.luxons.sevenwonders.model.resources.ResourceType.ORE -import org.luxons.sevenwonders.model.resources.ResourceType.STONE -import org.luxons.sevenwonders.model.resources.ResourceType.WOOD +import org.luxons.sevenwonders.model.resources.ResourceType.* import org.luxons.sevenwonders.model.resources.noTransactions import kotlin.test.assertEquals @@ -109,6 +105,45 @@ class BestPriceCalculatorTest { } @Test + fun bestPrice_mixedResources_overridenCost_sameMultipleTimes() { + val left = testBoard(WOOD) + + val main = testBoard(WOOD) + main.tradingRules.setCost(CLAY, LEFT_PLAYER, 1) + main.tradingRules.setCost(CLAY, RIGHT_PLAYER, 1) + + main.publicProduction.addFixedResource(STONE, 1) + main.publicProduction.addChoice(CLAY, ORE) + main.production.addFixedResource(STONE, 1) + main.production.addChoice(CLAY, ORE) + + val right = testBoard(CLAY) + right.publicProduction.addFixedResource(ORE, 1) + right.publicProduction.addFixedResource(CLAY, 1) + right.publicProduction.addFixedResource(WOOD, 2) + right.production.addFixedResource(ORE, 1) + right.production.addFixedResource(CLAY, 1) + right.production.addFixedResource(WOOD, 2) + + val table = Table(listOf(main, right, left)) + + val player0 = SimplePlayer(0, table) + val player1 = SimplePlayer(1, table) + val player2 = SimplePlayer(2, table) + + val resources = resourcesOf(WOOD, CLAY, CLAY, CLAY) + + val claysRight = createTransaction(RIGHT_PLAYER, CLAY, CLAY) + val claysLeft = createTransaction(LEFT_PLAYER, CLAY, CLAY) + val clayLeft = createTransaction(LEFT_PLAYER, CLAY) + val clayRight = createTransaction(RIGHT_PLAYER, CLAY) + + assertEquals(solutions(2, createTransactions(claysRight)), bestSolution(resources, player0)) + assertEquals(solutions(2, createTransactions(clayLeft)), bestSolution(resources, player1)) + assertEquals(solutions(6, createTransactions(claysLeft, clayRight)), bestSolution(resources, player2)) + } + + @Test fun bestPrice_chooseCheapest() { val left = testBoard(WOOD) diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt index a1b42b49..a7ed406b 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/test/TestUtils.kt @@ -70,7 +70,7 @@ internal fun createTransactions(provider: Provider, vararg resources: ResourceTy internal fun createTransactions(vararg transactions: ResourceTransaction): ResourceTransactions = transactions.toSet() internal fun createTransaction(provider: Provider, vararg resources: ResourceType): ResourceTransaction = - ResourceTransaction(provider, resources.map { CountedResource(1, it) }) + ResourceTransaction(provider, resources.groupBy { it }.map { (type, reps) -> CountedResource(reps.size, type) }) internal fun createRequirements(vararg types: ResourceType): Requirements = Requirements(resources = resourcesOf(*types)) |