From c6bd7d05e665aa6645de13b55f4623372218fcc0 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Tue, 10 Jul 2018 22:12:15 +0200 Subject: Refactor decks creation --- .../sevenwonders/game/data/GameDefinition.kt | 2 +- .../game/data/definitions/DecksDefinition.kt | 58 ++++++++-------------- 2 files changed, 22 insertions(+), 38 deletions(-) (limited to 'game-engine/src/main/kotlin/org') 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, + val backImage: String +) { + fun create(nbPlayers: Int): List = cards.flatMap { it.create(CardBack(backImage), nbPlayers) } +} internal class DecksDefinition( - private val age1: List, - private val age2: List, - private val age3: List, - 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 ) { - 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, settings: Settings, backImage: String): List { - val back = CardBack(backImage) - val cards = createDeck(defs, settings, back).toMutableList() - cards.shuffle(settings.random) - return cards - } - - private fun prepareAge3Deck(settings: Settings): List { - 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, settings: Settings, back: CardBack): List { - return defs.flatMap { it.create(back, settings.nbPlayers) } - } + ) - private fun createGuildCards(settings: Settings, back: CardBack): List { - 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 { + val back = CardBack(age3.backImage) + val guild = guildCards.map { it.create(back) }.shuffled(random) + return guild.subList(0, nbPlayers + 2) } } -- cgit