summaryrefslogtreecommitdiff
path: root/game-engine
diff options
context:
space:
mode:
Diffstat (limited to 'game-engine')
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java38
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java26
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java11
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/Game.kt9
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/boards/Board.kt38
-rw-r--r--game-engine/src/main/kotlin/org/luxons/sevenwonders/game/score/Score.kt23
-rw-r--r--game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt8
7 files changed, 50 insertions, 103 deletions
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
deleted file mode 100644
index f4a0d832..00000000
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
+++ /dev/null
@@ -1,38 +0,0 @@
-package org.luxons.sevenwonders.game.scoring;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public class PlayerScore {
-
- private final int boardGold;
-
- private final Map<ScoreCategory, Integer> scoresByCategory = new HashMap<>();
-
- private int totalPoints = 0;
-
- public PlayerScore(int boardGold) {
- this.boardGold = boardGold;
- }
-
- public Integer put(ScoreCategory category, Integer points) {
- totalPoints += points;
- return scoresByCategory.put(category, points);
- }
-
- public int getPoints(ScoreCategory category) {
- return scoresByCategory.get(category);
- }
-
- public Map<ScoreCategory, Integer> getPointsPerCategory() {
- return scoresByCategory;
- }
-
- public int getTotalPoints() {
- return totalPoints;
- }
-
- public int getBoardGold() {
- return boardGold;
- }
-}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
deleted file mode 100644
index 80f5e510..00000000
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package org.luxons.sevenwonders.game.scoring;
-
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.PriorityQueue;
-
-public class ScoreBoard {
-
- private static final Comparator<PlayerScore> comparator = Comparator.comparing(PlayerScore::getTotalPoints)
- .thenComparing(PlayerScore::getBoardGold);
-
- private PriorityQueue<PlayerScore> scores;
-
- public ScoreBoard(Collection<PlayerScore> scores) {
- this.scores = new PriorityQueue<>(comparator);
- this.scores.addAll(scores);
- }
-
- public void add(PlayerScore score) {
- scores.add(score);
- }
-
- public PriorityQueue<PlayerScore> getScores() {
- return scores;
- }
-}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java
deleted file mode 100644
index a6a9537d..00000000
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.luxons.sevenwonders.game.scoring;
-
-public enum ScoreCategory {
- CIVIL,
- SCIENCE,
- MILITARY,
- TRADE,
- GUILD,
- WONDER,
- GOLD
-}
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
+}
diff --git a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt
index 4142206b..526fbced 100644
--- a/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt
+++ b/game-engine/src/test/kotlin/org/luxons/sevenwonders/game/boards/BoardTest.kt
@@ -19,7 +19,7 @@ import org.luxons.sevenwonders.game.effects.RawPointsIncrease
import org.luxons.sevenwonders.game.effects.SpecialAbility
import org.luxons.sevenwonders.game.effects.SpecialAbilityActivation
import org.luxons.sevenwonders.game.resources.ResourceType
-import org.luxons.sevenwonders.game.scoring.ScoreCategory
+import org.luxons.sevenwonders.game.score.ScoreCategory
import org.luxons.sevenwonders.game.test.addCards
import org.luxons.sevenwonders.game.test.createResources
import org.luxons.sevenwonders.game.test.getDifferentColorFrom
@@ -171,7 +171,7 @@ class BoardTest {
board.gold = gold
val score = board.computePoints(table)
- assertEquals(gold / 3, score.getPoints(ScoreCategory.GOLD))
+ assertEquals(gold / 3, score.pointsByCategory[ScoreCategory.GOLD])
assertEquals(gold / 3, score.totalPoints)
}
@@ -186,8 +186,8 @@ class BoardTest {
playCardWithEffect(table, 0, Color.BLUE, effect)
val score = board.computePoints(table)
- assertEquals(gold / 3, score.getPoints(ScoreCategory.GOLD))
- assertEquals(5, score.getPoints(ScoreCategory.CIVIL))
+ assertEquals(gold / 3, score.pointsByCategory[ScoreCategory.GOLD])
+ assertEquals(5, score.pointsByCategory[ScoreCategory.CIVIL])
assertEquals(5 + gold / 3, score.totalPoints)
}
bgstack15