From d0a5abc2bd0f86be86b773e3f77712b2602bdc24 Mon Sep 17 00:00:00 2001 From: joffrey-bion Date: Wed, 25 Nov 2020 23:57:16 +0100 Subject: Fix resource transactions calculations Resolves: https://github.com/joffrey-bion/seven-wonders/issues/49 --- .../engine/resources/BestPriceCalculator.kt | 4 +- .../engine/resources/BestPriceCalculatorTest.kt | 45 +++++++++++++++++++--- .../luxons/sevenwonders/engine/test/TestUtils.kt | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) (limited to 'sw-engine') 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>, ) { - val choices: Set> = choices.mapTo(HashSet()) { it.toMutableSet() } + val choices: List> = 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 @@ -108,6 +104,45 @@ class BestPriceCalculatorTest { assertEquals(solutions(0, noTransactions()), bestSolution(resources, player2)) } + @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)) -- cgit