summaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/Game.java7
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/boards/Board.java24
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java33
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java24
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java5
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java8
6 files changed, 101 insertions, 0 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/Game.java b/src/main/java/org/luxons/sevenwonders/game/Game.java
index 81e9266a..59ab869c 100644
--- a/src/main/java/org/luxons/sevenwonders/game/Game.java
+++ b/src/main/java/org/luxons/sevenwonders/game/Game.java
@@ -18,6 +18,7 @@ import org.luxons.sevenwonders.game.cards.Decks;
import org.luxons.sevenwonders.game.cards.Hands;
import org.luxons.sevenwonders.game.effects.SpecialAbility;
import org.luxons.sevenwonders.game.moves.Move;
+import org.luxons.sevenwonders.game.scoring.ScoreBoard;
public class Game {
@@ -214,6 +215,12 @@ public class Game {
return currentAge % 2 == 0 ? -1 : 1;
}
+ public ScoreBoard computeScore() {
+ ScoreBoard scoreBoard = new ScoreBoard();
+ table.getBoards().stream().map(b -> b.computePoints(table)).forEach(scoreBoard::add);
+ return scoreBoard;
+ }
+
private static class MissingPreparedMoveException extends IllegalStateException {
MissingPreparedMoveException(int playerIndex) {
super("Player " + playerIndex + " is not ready to play");
diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
index b8893ca3..3b4cfe40 100644
--- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
+++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java
@@ -7,11 +7,14 @@ import java.util.Set;
import org.luxons.sevenwonders.game.Player;
import org.luxons.sevenwonders.game.Settings;
+import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.cards.Card;
import org.luxons.sevenwonders.game.cards.Color;
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.wonders.Wonder;
public class Board {
@@ -32,6 +35,8 @@ public class Board {
private final Set<SpecialAbility> specialAbilities = EnumSet.noneOf(SpecialAbility.class);
+
+
private int gold;
public Board(Wonder wonder, Player player, Settings settings) {
@@ -110,6 +115,25 @@ public class Board {
return specialAbilities.contains(specialAbility);
}
+ public PlayerScore computePoints(Table table) {
+ PlayerScore score = new PlayerScore(player, gold);
+ score.put(ScoreCategory.CIVIL, computePointsForCards(table, Color.BLUE));
+ score.put(ScoreCategory.MILITARY, military.getTotalPoints());
+ 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, player.getIndex()));
+ return score;
+ }
+
+ private int computePointsForCards(Table table, Color color) {
+ return playedCards.stream()
+ .filter(c -> c.getColor() == color)
+ .flatMap(c -> c.getEffects().stream())
+ .mapToInt(e -> e.computePoints(table, player.getIndex()))
+ .sum();
+ }
+
static class InsufficientFundsException extends RuntimeException {
InsufficientFundsException(int current, int required) {
super(String.format("Current balance is %d gold, but %d are required", current, required));
diff --git a/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java b/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
new file mode 100644
index 00000000..42acec54
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/scoring/PlayerScore.java
@@ -0,0 +1,33 @@
+package org.luxons.sevenwonders.game.scoring;
+
+import java.util.HashMap;
+
+import org.luxons.sevenwonders.game.Player;
+
+public class PlayerScore extends HashMap<ScoreCategory, Integer> {
+
+ private final Player player;
+
+ private final int boardGold;
+
+ private int totalPoints = 0;
+
+ public PlayerScore(Player player, int boardGold) {
+ this.player = player;
+ this.boardGold = boardGold;
+ }
+
+ @Override
+ public Integer put(ScoreCategory category, Integer points) {
+ totalPoints += points;
+ return super.put(category, points);
+ }
+
+ public int getTotalPoints() {
+ return totalPoints;
+ }
+
+ public int getBoardGold() {
+ return boardGold;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java b/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
new file mode 100644
index 00000000..26b5f8ba
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreBoard.java
@@ -0,0 +1,24 @@
+package org.luxons.sevenwonders.game.scoring;
+
+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() {
+ scores = new PriorityQueue<>(comparator);
+ }
+
+ public void add(PlayerScore score) {
+ scores.add(score);
+ }
+
+ public PriorityQueue<PlayerScore> getScores() {
+ return scores;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java b/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java
new file mode 100644
index 00000000..84d6ee73
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/scoring/ScoreCategory.java
@@ -0,0 +1,5 @@
+package org.luxons.sevenwonders.game.scoring;
+
+public enum ScoreCategory {
+ CIVIL, SCIENCE, MILITARY, TRADE, GUILD, WONDER
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
index d526cf8b..3ddddd30 100644
--- a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
+++ b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
@@ -91,4 +91,12 @@ public class Wonder {
int lastLevel = getNbBuiltStages() - 1;
return stages.get(lastLevel);
}
+
+ public int computePoints(Table table, int playerIndex) {
+ return stages.stream()
+ .filter(WonderStage::isBuilt)
+ .flatMap(c -> c.getEffects().stream())
+ .mapToInt(e -> e.computePoints(table, playerIndex))
+ .sum();
+ }
}
bgstack15