diff options
author | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-15 00:29:30 +0200 |
---|---|---|
committer | Joffrey Bion <joffrey.bion@booking.com> | 2020-05-15 01:20:15 +0200 |
commit | 813ef5907819226e62a8255eea10d7fffc0df843 (patch) | |
tree | ebbc1f93e128c75168806403cdb28fabe43369c1 /sw-engine | |
parent | Upgrade to krossbow 0.20.1 (migrate to flows) (diff) | |
download | seven-wonders-813ef5907819226e62a8255eea10d7fffc0df843.tar.gz seven-wonders-813ef5907819226e62a8255eea10d7fffc0df843.tar.bz2 seven-wonders-813ef5907819226e62a8255eea10d7fffc0df843.zip |
Clean up
Diffstat (limited to 'sw-engine')
10 files changed, 17 insertions, 18 deletions
diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt index dc02b080..8f683bd7 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Board.kt @@ -88,8 +88,7 @@ internal class Board(val wonder: Wonder, val playerIndex: Int, settings: Setting private fun computePointsForCards(player: Player, color: Color): Int = playedCards.filter { it.color === color } .flatMap { it.effects } - .map { it.computePoints(player) } - .sum() + .sumBy { it.computePoints(player) } private fun computeGoldPoints(): Int = gold / 3 * pointsPer3Gold diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Science.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Science.kt index 436d8995..992f4079 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Science.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/boards/Science.kt @@ -24,7 +24,7 @@ internal class Science { } fun addAll(science: Science) { - science.quantities.forEach { type, quantity -> this.add(type, quantity) } + science.quantities.forEach { (type, quantity) -> this.add(type, quantity) } jokers += science.jokers } @@ -42,14 +42,14 @@ internal class Science { var maxPoints = 0 for (i in values.indices) { values[i]++ - maxPoints = Math.max(maxPoints, computePoints(values, jokers - 1)) + maxPoints = maxPoints.coerceAtLeast(computePoints(values, jokers - 1)) values[i]-- } return maxPoints } private fun computePointsNoJoker(values: List<Int>): Int { - val independentSquaresSum = values.map { i -> i * i }.sum() + val independentSquaresSum = values.sumBy { it * it } val nbGroupsOfAll = values.min() ?: 0 return independentSquaresSum + nbGroupsOfAll * 7 } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt index 3f9d3797..467b0912 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/data/serializers/ProductionSerializer.kt @@ -31,7 +31,7 @@ internal class ProductionSerializer : JsonSerializer<Production>, JsonDeserializ if (choices.size > 1) { throw IllegalArgumentException("Cannot serialize a production with more than one choice") } - val str = choices.flatMap { it }.map { it.symbol }.joinToString("/") + val str = choices.flatten().map { it.symbol }.joinToString("/") return context.serialize(str) } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/BonusPerBoardElement.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/BonusPerBoardElement.kt index 96d177c9..046a7135 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/BonusPerBoardElement.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/BonusPerBoardElement.kt @@ -25,8 +25,7 @@ internal data class BonusPerBoardElement( private fun nbMatchingElementsFor(player: Player): Int = boards .map(player::getBoard) - .map(::nbMatchingElementsIn) - .sum() + .sumBy { nbMatchingElementsIn(it) } private fun nbMatchingElementsIn(board: Board): Int = when (type) { BoardElementType.CARD -> board.getNbCardsOfColor(colors!!) diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbility.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbility.kt index 857c7e63..9d1287f9 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbility.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbility.kt @@ -29,7 +29,7 @@ enum class SpecialAbility { override fun computePoints(player: Player): Int { val copiedGuild = player.board.copiedGuild ?: throw IllegalStateException("The copied Guild has not been chosen, cannot compute points") - return copiedGuild.effects.stream().mapToInt { it.computePoints(player) }.sum() + return copiedGuild.effects.sumBy { it.computePoints(player) } } }; 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 4f2ccd07..85b07a80 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 @@ -4,6 +4,7 @@ import org.luxons.sevenwonders.engine.Player import org.luxons.sevenwonders.model.resources.Provider import org.luxons.sevenwonders.model.resources.ResourceTransactions import org.luxons.sevenwonders.model.resources.ResourceType +import java.util.EnumMap import java.util.EnumSet internal fun bestSolution(resources: Resources, player: Player): TransactionPlan = @@ -16,7 +17,7 @@ private class ResourcePool( private val rules: TradingRules, choices: Set<Set<ResourceType>> ) { - val choices: Set<MutableSet<ResourceType>> = choices.map { it.toMutableSet() }.toSet() + val choices: Set<MutableSet<ResourceType>> = choices.mapTo(HashSet()) { it.toMutableSet() } fun getCost(type: ResourceType): Int = if (provider == null) 0 else rules.getCost(type, provider) } @@ -25,7 +26,7 @@ private class BestPriceCalculator(resourcesToPay: Resources, player: Player) { private val pools: List<ResourcePool> private val resourcesLeftToPay: MutableResources - private val boughtResources: MutableMap<Provider, MutableResources> = HashMap() + private val boughtResources: MutableMap<Provider, MutableResources> = EnumMap(Provider::class.java) private var pricePaid: Int = 0 private var bestSolutions: MutableSet<ResourceTransactions> = mutableSetOf() diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt index 88203144..46ec66f7 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Production.kt @@ -26,7 +26,7 @@ data class Production internal constructor( } internal fun asChoices(): Set<Set<ResourceType>> { - val fixedAsChoices = fixedResources.toList().map { EnumSet.of(it) }.toSet() + val fixedAsChoices = fixedResources.toList().mapTo(HashSet()) { EnumSet.of(it) } return fixedAsChoices + alternativeResources } diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Resources.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Resources.kt index 0edf9588..3d9654ed 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Resources.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/Resources.kt @@ -39,7 +39,7 @@ interface Resources { val quantities: Map<ResourceType, Int> val size: Int - get() = quantities.map { it.value }.sum() + get() = quantities.values.sum() fun isEmpty(): Boolean = size == 0 @@ -56,7 +56,7 @@ interface Resources { * type. */ operator fun minus(resources: Resources): Resources = - quantities.mapValues { (type, q) -> Math.max(0, q - resources[type]) }.toResources() + quantities.mapValues { (type, q) -> (q - resources[type]).coerceAtLeast(0) }.toResources() fun toList(): List<ResourceType> = quantities.flatMap { (type, quantity) -> List(quantity) { type } } @@ -71,7 +71,7 @@ class MutableResources( quantities.merge(type, quantity) { x, y -> x + y } } - fun add(resources: Resources) = resources.quantities.forEach { type, quantity -> this.add(type, quantity) } + fun add(resources: Resources) = resources.quantities.forEach { (type, quantity) -> add(type, quantity) } fun remove(type: ResourceType, quantity: Int) { if (this[type] < quantity) { diff --git a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/TradingRules.kt b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/TradingRules.kt index e0686d9c..bee4b9fe 100644 --- a/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/TradingRules.kt +++ b/sw-engine/src/main/kotlin/org/luxons/sevenwonders/engine/resources/TradingRules.kt @@ -5,7 +5,7 @@ import org.luxons.sevenwonders.model.resources.ResourceTransaction import org.luxons.sevenwonders.model.resources.ResourceTransactions import org.luxons.sevenwonders.model.resources.ResourceType -class TradingRules internal constructor(private val defaultCost: Int) { +internal class TradingRules(private val defaultCost: Int) { private val costs: MutableMap<ResourceType, MutableMap<Provider, Int>> = mutableMapOf() @@ -20,7 +20,7 @@ class TradingRules internal constructor(private val defaultCost: Int) { costs.computeIfAbsent(type) { mutableMapOf() }[provider] = cost } - internal fun computeCost(transactions: ResourceTransactions): Int = transactions.map { computeCost(it) }.sum() + internal fun computeCost(transactions: ResourceTransactions): Int = transactions.sumBy { computeCost(it) } internal fun computeCost(transact: ResourceTransaction) = computeCost(transact.asResources(), transact.provider) diff --git a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbilityActivationTest.kt b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbilityActivationTest.kt index 16fbeae6..5a996369 100644 --- a/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbilityActivationTest.kt +++ b/sw-engine/src/test/kotlin/org/luxons/sevenwonders/engine/effects/SpecialAbilityActivationTest.kt @@ -49,7 +49,7 @@ class SpecialAbilityActivationTest { player.board.copiedGuild = guildCard - val directPointsFromGuildCard = guildCard.effects.stream().mapToInt { e -> e.computePoints(player) }.sum() + val directPointsFromGuildCard = guildCard.effects.sumBy { it.computePoints(player) } assertEquals(directPointsFromGuildCard, effect.computePoints(player)) } |