diff options
author | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-10 21:23:24 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-10 21:23:42 +0200 |
commit | d083b7dddf95a3a4f29109670616bc347ac1976d (patch) | |
tree | df028853e6bc3bf2aca9ff4bd135ec0bc428efd4 /game-engine/src/main/kotlin/org | |
parent | Kotlin mig: resources package (diff) | |
download | seven-wonders-d083b7dddf95a3a4f29109670616bc347ac1976d.tar.gz seven-wonders-d083b7dddf95a3a4f29109670616bc347ac1976d.tar.bz2 seven-wonders-d083b7dddf95a3a4f29109670616bc347ac1976d.zip |
Kotlin mig: score package
Diffstat (limited to 'game-engine/src/main/kotlin/org')
3 files changed, 46 insertions, 24 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 41e3f764..46c4a366 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 @@ -14,7 +14,7 @@ import org.luxons.sevenwonders.game.data.LAST_AGE import org.luxons.sevenwonders.game.effects.SpecialAbility import org.luxons.sevenwonders.game.moves.InvalidMoveException import org.luxons.sevenwonders.game.moves.Move -import org.luxons.sevenwonders.game.scoring.ScoreBoard +import org.luxons.sevenwonders.game.score.ScoreBoard class Game( val id: Long, @@ -167,11 +167,10 @@ class Game( hands = hands.discardHand(playerIndex) } - private fun activatePlayedCards(playedMoves: List<Move>) { - playedMoves.forEach { move -> move.activate(table, discardedCards, settings) } - } + private fun activatePlayedCards(playedMoves: List<Move>) = + playedMoves.forEach { it.activate(table, discardedCards, settings) } - fun computeScore(): ScoreBoard = ScoreBoard(table.boards.map { b -> b.computePoints(table) }) + fun computeScore(): ScoreBoard = ScoreBoard(table.boards.map { it.computePoints(table) }) private class MissingPreparedMoveException internal constructor(playerIndex: Int) : IllegalStateException("Player $playerIndex has not prepared his move") 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 b02777e5..77358d03 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 @@ -8,8 +8,8 @@ import org.luxons.sevenwonders.game.data.Age import org.luxons.sevenwonders.game.effects.SpecialAbility import org.luxons.sevenwonders.game.resources.Production import org.luxons.sevenwonders.game.resources.TradingRules -import org.luxons.sevenwonders.game.scoring.PlayerScore -import org.luxons.sevenwonders.game.scoring.ScoreCategory +import org.luxons.sevenwonders.game.score.PlayerScore +import org.luxons.sevenwonders.game.score.ScoreCategory import org.luxons.sevenwonders.game.wonders.Wonder class Board(val wonder: Wonder, val playerIndex: Int, settings: Settings) { @@ -74,23 +74,23 @@ class Board(val wonder: Wonder, val playerIndex: Int, settings: Settings) { consumedFreeCards[age] = true } - fun computePoints(table: Table): PlayerScore { - val score = PlayerScore(gold) - score.put(ScoreCategory.CIVIL, computePointsForCards(table, Color.BLUE)) - score.put(ScoreCategory.MILITARY, military.totalPoints) - score.put(ScoreCategory.SCIENCE, science.computePoints()) - score.put(ScoreCategory.TRADE, computePointsForCards(table, Color.YELLOW)) - score.put(ScoreCategory.GUILD, computePointsForCards(table, Color.PURPLE)) - score.put(ScoreCategory.WONDER, wonder.computePoints(table, playerIndex)) - score.put(ScoreCategory.GOLD, computeGoldPoints()) - return score - } - - private fun computePointsForCards(table: Table, color: Color): Int = playedCards - .filter { (_, color1) -> color1 === color } - .flatMap { (_, _, _, effects) -> effects } - .map { e -> e.computePoints(table, playerIndex) } - .sum() + fun computePoints(table: Table): PlayerScore = PlayerScore( + gold, mapOf( + ScoreCategory.CIVIL to computePointsForCards(table, Color.BLUE), + ScoreCategory.MILITARY to military.totalPoints, + ScoreCategory.SCIENCE to science.computePoints(), + ScoreCategory.TRADE to computePointsForCards(table, Color.YELLOW), + ScoreCategory.GUILD to computePointsForCards(table, Color.PURPLE), + ScoreCategory.WONDER to wonder.computePoints(table, playerIndex), + ScoreCategory.GOLD to computeGoldPoints() + ) + ) + + private fun computePointsForCards(table: Table, color: Color): Int = + playedCards.filter { it.color === color } + .flatMap { it.effects } + .map { it.computePoints(table, playerIndex) } + .sum() private fun computeGoldPoints(): Int = gold / 3 * pointsPer3Gold diff --git a/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/score/Score.kt b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/score/Score.kt new file mode 100644 index 00000000..c1d34d5d --- /dev/null +++ b/game-engine/src/main/kotlin/org/luxons/sevenwonders/game/score/Score.kt @@ -0,0 +1,23 @@ +package org.luxons.sevenwonders.game.score + +class ScoreBoard(scores: Collection<PlayerScore>) { + + val scores: Collection<PlayerScore> = scores.sortedDescending() +} + +data class PlayerScore(val boardGold: Int, val pointsByCategory: Map<ScoreCategory, Int>) : Comparable<PlayerScore> { + + val totalPoints = pointsByCategory.map { it.value }.sum() + + override fun compareTo(other: PlayerScore) = compareValuesBy(this, other, { it.totalPoints }, { it.boardGold }) +} + +enum class ScoreCategory { + CIVIL, + SCIENCE, + MILITARY, + TRADE, + GUILD, + WONDER, + GOLD +} |