summaryrefslogtreecommitdiff
path: root/game-engine/src/main/kotlin
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2019-02-21 00:52:51 +0100
committerjbion <joffrey.bion@amadeus.com>2019-02-21 00:52:51 +0100
commit548dd277dd8ad1ab9828f6ab1b548e962ce6530a (patch)
treecd1653a7648a5df63cf910d532458d89196c7229 /game-engine/src/main/kotlin
parentSimplify playability further (diff)
downloadseven-wonders-548dd277dd8ad1ab9828f6ab1b548e962ce6530a.tar.gz
seven-wonders-548dd277dd8ad1ab9828f6ab1b548e962ce6530a.tar.bz2
seven-wonders-548dd277dd8ad1ab9828f6ab1b548e962ce6530a.zip
Remove non idiomatic usages of lists
Diffstat (limited to 'game-engine/src/main/kotlin')
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt4
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt2
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.kt37
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt32
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Production.kt3
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Resources.kt6
6 files changed, 33 insertions, 51 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt
index f92177c8..124cf435 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt
@@ -26,8 +26,8 @@ class Game internal constructor(
) {
private val table: Table = Table(boards)
private val players: List<Player> = boards.map { SimplePlayer(it.playerIndex, table) }
- private val discardedCards: MutableList<Card> = ArrayList()
- private val preparedMoves: MutableMap<Int, Move> = HashMap()
+ private val discardedCards: MutableList<Card> = mutableListOf()
+ private val preparedMoves: MutableMap<Int, Move> = mutableMapOf()
private var currentTurnInfo: List<PlayerTurnInfo> = emptyList()
private var hands: Hands = Hands(emptyList())
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt
index 22b9ac93..04dd2d25 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt
@@ -23,7 +23,7 @@ internal class Board(val wonder: Wonder, val playerIndex: Int, settings: Setting
private val pointsPer3Gold: Int = settings.pointsPer3Gold
- private val playedCards: MutableList<Card> = arrayListOf()
+ private val playedCards: MutableList<Card> = mutableListOf()
private val specialAbilities: MutableSet<SpecialAbility> = hashSetOf()
private val consumedFreeCards: MutableMap<Age, Boolean> = mutableMapOf()
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.kt
index 23a8d3ee..4d0348ea 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/EffectsDefinition.kt
@@ -10,7 +10,6 @@ import org.luxons.sevenwonders.game.effects.RawPointsIncrease
import org.luxons.sevenwonders.game.effects.ScienceProgress
import org.luxons.sevenwonders.game.effects.SpecialAbility
import org.luxons.sevenwonders.game.effects.SpecialAbilityActivation
-import java.util.ArrayList
internal class EffectsDefinition(
private val gold: GoldIncrease? = null,
@@ -22,32 +21,14 @@ internal class EffectsDefinition(
private val points: RawPointsIncrease? = null,
private val action: SpecialAbility? = null
) {
- fun create(): List<Effect> {
- val effects = ArrayList<Effect>()
- if (gold != null) {
- effects.add(gold)
- }
- if (military != null) {
- effects.add(military)
- }
- if (science != null) {
- effects.add(science)
- }
- if (discount != null) {
- effects.add(discount)
- }
- if (perBoardElement != null) {
- effects.add(perBoardElement)
- }
- if (production != null) {
- effects.add(production)
- }
- if (points != null) {
- effects.add(points)
- }
- if (action != null) {
- effects.add(SpecialAbilityActivation(action))
- }
- return effects
+ fun create(): List<Effect> = mutableListOf<Effect>().apply {
+ gold?.let { add(it) }
+ military?.let { add(it) }
+ science?.let { add(it) }
+ discount?.let { add(it) }
+ perBoardElement?.let { add(it) }
+ production?.let { add(it) }
+ points?.let { add(it) }
+ action?.let { add(SpecialAbilityActivation(it)) }
}
}
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt
index 3d051732..dea6f2c2 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/BestPriceCalculator.kt
@@ -1,7 +1,6 @@
package org.luxons.sevenwonders.game.resources
import org.luxons.sevenwonders.game.Player
-import java.util.ArrayList
import java.util.EnumSet
internal fun bestSolution(resources: Resources, player: Player): TransactionPlan =
@@ -12,8 +11,10 @@ data class TransactionPlan(val price: Int, val possibleTransactions: Set<Resourc
private class ResourcePool(
val provider: Provider?,
private val rules: TradingRules,
- val choices: Set<MutableSet<ResourceType>>
+ choices: Set<Set<ResourceType>>
) {
+ val choices: Set<MutableSet<ResourceType>> = choices.map { it.toMutableSet() }.toSet()
+
fun getCost(type: ResourceType): Int = if (provider == null) 0 else rules.getCost(type, provider)
}
@@ -34,23 +35,18 @@ private class BestPriceCalculator(resourcesToPay: Resources, player: Player) {
}
private fun createResourcePools(player: Player): List<ResourcePool> {
- val providers = Provider.values()
+ // we only take alternative resources here, because fixed resources were already removed for optimization
+ val ownBoardChoices = player.board.production.getAlternativeResources()
+ val ownPool = ResourcePool(null, player.board.tradingRules, ownBoardChoices)
+ val providerPools = Provider.values().map { it.toResourcePoolFor(player) }
- val board = player.board
- val rules = board.tradingRules
+ return providerPools + ownPool
+ }
- val pools = ArrayList<ResourcePool>(providers.size + 1)
- // we only take alternative resources here, because fixed resources were already removed for optimization
- val ownBoardChoices = board.production.getAlternativeResources()
- pools.add(ResourcePool(null, rules, ownBoardChoices.map { it.toMutableSet() }.toSet()))
-
- for (provider in providers) {
- val providerBoard = player.getBoard(provider.boardPosition)
- val choices = providerBoard.publicProduction.asChoices().map { it.toMutableSet() }.toSet()
- val pool = ResourcePool(provider, rules, choices)
- pools.add(pool)
- }
- return pools
+ private fun Provider.toResourcePoolFor(player: Player): ResourcePool {
+ val providerBoard = player.getBoard(boardPosition)
+ val choices = providerBoard.publicProduction.asChoices()
+ return ResourcePool(this, player.board.tradingRules, choices)
}
fun computeBestSolution(): TransactionPlan {
@@ -120,6 +116,6 @@ private class BestPriceCalculator(resourcesToPay: Resources, player: Player) {
bestSolutions.clear()
}
// avoid mutating the resources from the transactions
- bestSolutions.add(boughtResources.mapValues { MutableResources(HashMap(it.value.quantities)) }.toTransactions())
+ bestSolutions.add(boughtResources.mapValues { (_, res) -> res.copy() }.toTransactions())
}
}
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Production.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Production.kt
index 0b069677..66a63463 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Production.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Production.kt
@@ -1,6 +1,5 @@
package org.luxons.sevenwonders.game.resources
-import java.util.Arrays
import java.util.EnumSet
data class Production internal constructor(
@@ -14,7 +13,7 @@ data class Production internal constructor(
fun addFixedResource(type: ResourceType, quantity: Int) = fixedResources.add(type, quantity)
fun addChoice(vararg options: ResourceType) {
- val optionSet = EnumSet.copyOf(Arrays.asList(*options))
+ val optionSet = EnumSet.copyOf(options.toList())
alternativeResources.add(optionSet)
}
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Resources.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Resources.kt
index c02fd260..a91a2af1 100644
--- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Resources.kt
+++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/resources/Resources.kt
@@ -10,6 +10,10 @@ fun resourcesOf(vararg resources: Pair<ResourceType, Int>): Resources = mutableR
fun Iterable<Pair<ResourceType, Int>>.toResources(): Resources = toMutableResources()
+/**
+ * Creates [Resources] from a copy of the given map. Future modifications to the input map won't affect the return
+ * resources.
+ */
fun Map<ResourceType, Int>.toResources(): Resources = toMutableResources()
fun Iterable<Resources>.merge(): Resources = fold(MutableResources()) { r1, r2 -> r1.add(r2); r1 }
@@ -53,6 +57,8 @@ interface Resources {
quantities.mapValues { (type, q) -> Math.max(0, q - resources[type]) }.toResources()
fun toList(): List<ResourceType> = quantities.flatMap { (type, quantity) -> List(quantity) { type } }
+
+ fun copy(): Resources = quantities.toResources()
}
class MutableResources(
bgstack15