From f46792d358caf9875ee8cfdddb762b22f41b6cc3 Mon Sep 17 00:00:00 2001 From: Joffrey BION Date: Sat, 10 Dec 2016 08:31:32 +0100 Subject: Add BonusPerBoardElement effect --- .../sevenwonders/game/RelativePlayerPosition.java | 5 + .../org/luxons/sevenwonders/game/boards/Board.java | 15 +++ .../luxons/sevenwonders/game/boards/Neighbour.java | 5 - .../sevenwonders/game/boards/TradingRules.java | 11 ++- .../game/effects/BoardElementType.java | 5 + .../game/effects/BonusPerBoardElement.java | 101 +++++++++++++++++++++ .../luxons/sevenwonders/game/effects/Discount.java | 14 +-- 7 files changed, 139 insertions(+), 17 deletions(-) create mode 100644 src/main/java/org/luxons/sevenwonders/game/RelativePlayerPosition.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/boards/Neighbour.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java (limited to 'src/main/java/org/luxons') diff --git a/src/main/java/org/luxons/sevenwonders/game/RelativePlayerPosition.java b/src/main/java/org/luxons/sevenwonders/game/RelativePlayerPosition.java new file mode 100644 index 00000000..8d078d50 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/RelativePlayerPosition.java @@ -0,0 +1,5 @@ +package org.luxons.sevenwonders.game; + +public enum RelativePlayerPosition { + LEFT_PLAYER, RIGHT_PLAYER, SELF; +} 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 1744f60b..d676ca82 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -5,6 +5,7 @@ import java.util.List; import org.luxons.sevenwonders.game.Settings; import org.luxons.sevenwonders.game.cards.Card; +import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.resources.Production; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -24,6 +25,8 @@ public class Board { private int wonderLevel; + private int nbDefeatTokens; + public Board(Wonder wonder, Settings settings) { this.wonder = wonder; this.wonderLevel = 0; @@ -44,6 +47,10 @@ public class Board { playedCards.add(card); } + public int getNbCardsOfColor(Color color) { + return (int) playedCards.stream().filter(c -> c.getColor() == color).count(); + } + public Production getProduction() { return production; } @@ -76,4 +83,12 @@ public class Board { this.wonderLevel++; wonder.getLevels().get(wonderLevel).getEffect().apply(this, null, null); } + + public int getNbDefeatTokens() { + return nbDefeatTokens; + } + + public void setNbDefeatTokens(int nbDefeatTokens) { + this.nbDefeatTokens= nbDefeatTokens; + } } diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Neighbour.java b/src/main/java/org/luxons/sevenwonders/game/boards/Neighbour.java deleted file mode 100644 index 63611173..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Neighbour.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.luxons.sevenwonders.game.boards; - -public enum Neighbour { - LEFT, RIGHT; -} diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java b/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java index 96131775..7784306a 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java @@ -3,11 +3,12 @@ package org.luxons.sevenwonders.game.boards; import java.util.EnumMap; import java.util.Map; +import org.luxons.sevenwonders.game.RelativePlayerPosition; import org.luxons.sevenwonders.game.resources.ResourceType; public class TradingRules { - private final Map> costs = new EnumMap<>(ResourceType.class); + private final Map> costs = new EnumMap<>(ResourceType.class); private final int defaultCost; @@ -15,11 +16,11 @@ public class TradingRules { this.defaultCost = defaultCost; } - public int getCost(ResourceType type, Neighbour neighbour) { - return costs.computeIfAbsent(type, t -> new EnumMap<>(Neighbour.class)).getOrDefault(neighbour, defaultCost); + public int getCost(ResourceType type, RelativePlayerPosition target) { + return costs.computeIfAbsent(type, t -> new EnumMap<>(RelativePlayerPosition.class)).getOrDefault(target, defaultCost); } - public void setCost(ResourceType type, Neighbour neighbour, int cost) { - costs.computeIfAbsent(type, t -> new EnumMap<>(Neighbour.class)).put(neighbour, cost); + public void setCost(ResourceType type, RelativePlayerPosition target, int cost) { + costs.computeIfAbsent(type, t -> new EnumMap<>(RelativePlayerPosition.class)).put(target, cost); } } diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java b/src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java new file mode 100644 index 00000000..f2bc2f38 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/effects/BoardElementType.java @@ -0,0 +1,5 @@ +package org.luxons.sevenwonders.game.effects; + +public enum BoardElementType { + CARD, WONDER_LEVEL, DEFEAT_TOKEN +} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java b/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java new file mode 100644 index 00000000..51da55bb --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/effects/BonusPerBoardElement.java @@ -0,0 +1,101 @@ +package org.luxons.sevenwonders.game.effects; + +import java.util.List; + +import org.luxons.sevenwonders.game.boards.Board; +import org.luxons.sevenwonders.game.RelativePlayerPosition; +import org.luxons.sevenwonders.game.cards.Color; + +public class BonusPerBoardElement extends Effect { + + private List boards; + + private int gold; + + private int points; + + private BoardElementType type; + + // only relevant if type=CARD + private Color color; + + public List getBoards() { + return boards; + } + + public void setBoards(List boards) { + this.boards = boards; + } + + public int getGold() { + return gold; + } + + public void setGold(int gold) { + this.gold = gold; + } + + public int getPoints() { + return points; + } + + public void setPoints(int points) { + this.points = points; + } + + public BoardElementType getType() { + return type; + } + + public void setType(BoardElementType type) { + this.type = type; + } + + public Color getColor() { + return color; + } + + public void setColor(Color color) { + this.color = color; + } + + @Override + public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + int goldGain = gold * computeNbOfMatchingElementsIn(board, leftNeighbourBoard, rightNeighbourBoard); + board.setGold(board.getGold() + goldGain); + } + + @Override + public int computePoints(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + return points * computeNbOfMatchingElementsIn(board, leftNeighbourBoard, rightNeighbourBoard); + } + + private int computeNbOfMatchingElementsIn(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { + int totalCount = 0; + if (boards.contains(RelativePlayerPosition.SELF)) { + totalCount += computeNbOfMatchingElementsIn(board); + } + if (boards.contains(RelativePlayerPosition.LEFT_PLAYER)) { + totalCount += computeNbOfMatchingElementsIn(leftNeighbourBoard); + } + if (boards.contains(RelativePlayerPosition.RIGHT_PLAYER)) { + totalCount += computeNbOfMatchingElementsIn(rightNeighbourBoard); + } + return totalCount; + } + + private int computeNbOfMatchingElementsIn(Board board) { + switch (type) { + case CARD: + return board.getNbCardsOfColor(color); + case WONDER_LEVEL: + return board.getWonderLevel(); + case DEFEAT_TOKEN: + return board.getNbDefeatTokens(); + } + throw new UnsupportedBoardElementType(); + } + + private class UnsupportedBoardElementType extends RuntimeException { + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java index 8fce19d4..92d1d74d 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java @@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game.effects; import java.util.ArrayList; import java.util.List; -import org.luxons.sevenwonders.game.boards.Neighbour; +import org.luxons.sevenwonders.game.RelativePlayerPosition; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.TradingRules; import org.luxons.sevenwonders.game.resources.ResourceType; @@ -12,16 +12,16 @@ public class Discount extends InstantEffect { private final List resourceTypes = new ArrayList<>(); - private final List neighbours = new ArrayList<>(); + private final List targets = new ArrayList<>(); - private int discountedPrice; + private int discountedPrice = 1; public List getResourceTypes() { return resourceTypes; } - public List getNeighbours() { - return neighbours; + public List getTargets() { + return targets; } public int getDiscountedPrice() { @@ -36,8 +36,8 @@ public class Discount extends InstantEffect { public void apply(Board board, Board leftNeighbourBoard, Board rightNeighbourBoard) { TradingRules rules = board.getTradingRules(); for (ResourceType type : resourceTypes) { - for (Neighbour neighbour : neighbours) { - rules.setCost(type, neighbour, discountedPrice); + for (RelativePlayerPosition target : targets) { + rules.setCost(type, target, discountedPrice); } } } -- cgit