diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-10 22:12:15 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-10 22:16:30 +0200 |
commit | c6bd7d05e665aa6645de13b55f4623372218fcc0 (patch) | |
tree | b27373c5a0d7d1dc9033273f5ee907743632dbb7 /game-engine/src/main/kotlin/org | |
parent | Clean class visibilities (diff) | |
download | seven-wonders-c6bd7d05e665aa6645de13b55f4623372218fcc0.tar.gz seven-wonders-c6bd7d05e665aa6645de13b55f4623372218fcc0.tar.bz2 seven-wonders-c6bd7d05e665aa6645de13b55f4623372218fcc0.zip |
Refactor decks creation
Diffstat (limited to 'game-engine/src/main/kotlin/org')
-rw-r--r-- | game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt | 2 | ||||
-rw-r--r-- | game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.kt | 58 |
2 files changed, 22 insertions, 38 deletions
diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt index 60dce0ee..43703877 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/GameDefinition.kt @@ -18,7 +18,7 @@ class GameDefinition internal constructor( fun initGame(id: Long, customSettings: CustomizableSettings, nbPlayers: Int): Game { val settings = Settings(nbPlayers, customSettings) val boards = assignBoards(settings, nbPlayers) - val decks = decksDefinition.prepareDecks(settings) + val decks = decksDefinition.prepareDecks(settings.nbPlayers, settings.random) return Game(id, settings, boards, decks) } diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.kt index bc025ea2..20477266 100644 --- a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.kt +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/data/definitions/DecksDefinition.kt @@ -1,50 +1,34 @@ package org.luxons.sevenwonders.game.data.definitions -import org.luxons.sevenwonders.game.Settings import org.luxons.sevenwonders.game.cards.Card import org.luxons.sevenwonders.game.cards.CardBack import org.luxons.sevenwonders.game.cards.Decks +import java.util.Random + +internal class DeckDefinition( + val cards: List<CardDefinition>, + val backImage: String +) { + fun create(nbPlayers: Int): List<Card> = cards.flatMap { it.create(CardBack(backImage), nbPlayers) } +} internal class DecksDefinition( - private val age1: List<CardDefinition>, - private val age2: List<CardDefinition>, - private val age3: List<CardDefinition>, - private val age1Back: String, - private val age2Back: String, - private val age3Back: String, + private val age1: DeckDefinition, + private val age2: DeckDefinition, + private val age3: DeckDefinition, private val guildCards: List<CardDefinition> ) { - fun prepareDecks(settings: Settings): Decks { - val cardsPerAge = mapOf( - 1 to prepareStandardDeck(age1, settings, age1Back), - 2 to prepareStandardDeck(age2, settings, age2Back), - 3 to prepareAge3Deck(settings) + fun prepareDecks(nbPlayers: Int, random: Random) = Decks( + mapOf( + 1 to age1.create(nbPlayers).shuffled(random), + 2 to age2.create(nbPlayers).shuffled(random), + 3 to (age3.create(nbPlayers) + pickGuildCards(nbPlayers, random)).shuffled(random) ) - return Decks(cardsPerAge) - } - - private fun prepareStandardDeck(defs: List<CardDefinition>, settings: Settings, backImage: String): List<Card> { - val back = CardBack(backImage) - val cards = createDeck(defs, settings, back).toMutableList() - cards.shuffle(settings.random) - return cards - } - - private fun prepareAge3Deck(settings: Settings): List<Card> { - val back = CardBack(age3Back) - val age3deck = createDeck(age3, settings, back).toMutableList() - age3deck.addAll(createGuildCards(settings, back)) - age3deck.shuffle(settings.random) - return age3deck - } - - private fun createDeck(defs: List<CardDefinition>, settings: Settings, back: CardBack): List<Card> { - return defs.flatMap { it.create(back, settings.nbPlayers) } - } + ) - private fun createGuildCards(settings: Settings, back: CardBack): List<Card> { - val guild = guildCards.map { it.create(back) }.toMutableList() - guild.shuffle(settings.random) - return guild.subList(0, settings.nbPlayers + 2) + private fun pickGuildCards(nbPlayers: Int, random: Random): List<Card> { + val back = CardBack(age3.backImage) + val guild = guildCards.map { it.create(back) }.shuffled(random) + return guild.subList(0, nbPlayers + 2) } } |