diff options
author | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-02 14:40:26 +0100 |
---|---|---|
committer | joffrey-bion <joffrey.bion@gmail.com> | 2021-02-02 14:47:32 +0100 |
commit | 2762d7d0ea69291593662a9c2d934e45caa23dbb (patch) | |
tree | 92f067984c0862a676dcb4e5b095506e54f23ea2 | |
parent | Move prepareMove log before the actual preparation (diff) | |
download | seven-wonders-2762d7d0ea69291593662a9c2d934e45caa23dbb.tar.gz seven-wonders-2762d7d0ea69291593662a9c2d934e45caa23dbb.tar.bz2 seven-wonders-2762d7d0ea69291593662a9c2d934e45caa23dbb.zip |
Filter only affordable transactions
Resolves:
https://github.com/joffrey-bion/seven-wonders/issues/81
-rw-r--r-- | sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Requirements.kt | 15 | ||||
-rw-r--r-- | sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/RequirementsTest.kt | 33 |
2 files changed, 42 insertions, 6 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Requirements.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Requirements.kt index 1d03aa76..4872f44f 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Requirements.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/cards/Requirements.kt @@ -6,6 +6,8 @@ import org.luxons.sevenwonders.engine.boards.Board import org.luxons.sevenwonders.engine.resources.* import org.luxons.sevenwonders.model.resources.ResourceTransactions import org.luxons.sevenwonders.model.resources.bestPrice +import org.luxons.sevenwonders.model.resources.noTransactionOptions +import org.luxons.sevenwonders.model.resources.totalPrice @Serializable data class Requirements internal constructor( @@ -36,15 +38,16 @@ data class Requirements internal constructor( } private fun satisfactionWithPotentialHelp(player: Player): RequirementsSatisfaction { - val options = transactionOptions(resources, player) - val minPrice = options.bestPrice + gold - if (options.isEmpty()) { + val allOptions = transactionOptions(resources, player) + if (allOptions.isEmpty()) { return RequirementsSatisfaction.unavailableResources() } - if (player.board.gold < minPrice) { - return RequirementsSatisfaction.missingGoldForResources(minPrice, options) + val minPrice = allOptions.bestPrice + gold + if (minPrice > player.board.gold) { + return RequirementsSatisfaction.missingGoldForResources(minPrice, noTransactionOptions()) } - return RequirementsSatisfaction.metWithHelp(minPrice, options) + val availableOptions = allOptions.filter { it.totalPrice + gold <= player.board.gold } + return RequirementsSatisfaction.metWithHelp(minPrice, availableOptions) } /** diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/RequirementsTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/RequirementsTest.kt index 36dc8ed1..20100537 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/RequirementsTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/cards/RequirementsTest.kt @@ -128,6 +128,39 @@ class RequirementsTest { } @Theory + fun allTransactionOptionsAreValid( + initialResource: ResourceType, + boughtResource: ResourceType, + requiredResource: ResourceType, + ) { + assumeTrue(initialResource != requiredResource) + + val requirements = createRequirements(requiredResource) + + val board = testBoard(initialResource, 1) + board.tradingRules.setCost(boughtResource, Provider.RIGHT_PLAYER, 1) + + val leftNeighbourBoard = testBoard(initialResource, 0) + leftNeighbourBoard.publicProduction.addFixedResource(boughtResource, 1) + + val rightNeighbourBoard = testBoard(initialResource, 0) + rightNeighbourBoard.publicProduction.addFixedResource(boughtResource, 1) + + val table = Table(listOf(leftNeighbourBoard, board, rightNeighbourBoard)) + val player = SimplePlayer(0, table) + + val satisfaction = requirements.assess(player) + if (satisfaction.satisfied) { + satisfaction.transactionOptions.forEach { + assertTrue( + requirements.areMetWithHelpBy(board, it), + "all transaction options should be valid, but this is not: $it" + ) + } + } + } + + @Theory fun pay_boughtResource(initialResource: ResourceType, requiredResource: ResourceType) { assumeTrue(initialResource != requiredResource) |