diff options
author | jbion <joffrey.bion@amadeus.com> | 2016-12-22 23:36:35 +0100 |
---|---|---|
committer | jbion <joffrey.bion@amadeus.com> | 2016-12-22 23:36:35 +0100 |
commit | 54ba69d8961a96e7455401ec669f75f38352809f (patch) | |
tree | 9e701e9ee9f25bd0e32b0509d489b15079687fa2 /src/main/java | |
parent | Remove middleman methods in Board (diff) | |
download | seven-wonders-54ba69d8961a96e7455401ec669f75f38352809f.tar.gz seven-wonders-54ba69d8961a96e7455401ec669f75f38352809f.tar.bz2 seven-wonders-54ba69d8961a96e7455401ec669f75f38352809f.zip |
Add bought resources cost computation
Diffstat (limited to 'src/main/java')
12 files changed, 63 insertions, 37 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/api/Move.java b/src/main/java/org/luxons/sevenwonders/game/api/Move.java index 223f6615..55af635e 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Move.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Move.java @@ -3,6 +3,8 @@ package org.luxons.sevenwonders.game.api; import java.util.ArrayList; import java.util.List; +import org.luxons.sevenwonders.game.resources.BoughtResources; + public class Move { private int playerIndex; diff --git a/src/main/java/org/luxons/sevenwonders/game/api/Table.java b/src/main/java/org/luxons/sevenwonders/game/api/Table.java index 44c9076f..686b7ea5 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/Table.java +++ b/src/main/java/org/luxons/sevenwonders/game/api/Table.java @@ -6,6 +6,7 @@ import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.CardBack; +import org.luxons.sevenwonders.game.resources.BoughtResources; /** * The table contains what is visible by all the players in the game: the boards and their played cards, and the 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 f2f39503..d0fc22b8 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Board.java +++ b/src/main/java/org/luxons/sevenwonders/game/boards/Board.java @@ -8,6 +8,7 @@ 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.resources.TradingRules; import org.luxons.sevenwonders.game.wonders.Wonder; public class Board { diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java b/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java deleted file mode 100644 index 97b60ea0..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.luxons.sevenwonders.game.boards; - -import java.util.EnumMap; -import java.util.Map; - -import org.luxons.sevenwonders.game.resources.ResourceType; - -public class TradingRules { - - private final Map<ResourceType, Map<Provider, Integer>> costs = new EnumMap<>(ResourceType.class); - - private final int defaultCost; - - public TradingRules(int defaultCost) { - this.defaultCost = defaultCost; - } - - public int getCost(ResourceType type, Provider provider) { - return costs.computeIfAbsent(type, t -> new EnumMap<>(Provider.class)).getOrDefault(provider, defaultCost); - } - - public void setCost(ResourceType type, Provider provider, int cost) { - costs.computeIfAbsent(type, t -> new EnumMap<>(Provider.class)).put(provider, cost); - } -} diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java index 94c46839..76a3a82e 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Card.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Card.java @@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game.cards; import java.util.List; import java.util.Objects; -import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.effects.Effect; diff --git a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java index 714f86a6..8fc6241a 100644 --- a/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java +++ b/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java @@ -2,7 +2,7 @@ package org.luxons.sevenwonders.game.cards; import java.util.List; -import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.resources.Resources; @@ -52,6 +52,16 @@ public class Requirements { } void pay(Board board) { - board.setGold(board.getGold() - gold); + int newBalance = board.getGold() - gold; + if (newBalance < 0) { + throw new InsufficientFundsException(board.getGold(), gold); + } + board.setGold(newBalance); + } + + private 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/effects/Discount.java b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java index a943b027..3a44574b 100644 --- a/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java +++ b/src/main/java/org/luxons/sevenwonders/game/effects/Discount.java @@ -4,8 +4,8 @@ import java.util.ArrayList; import java.util.List; import org.luxons.sevenwonders.game.boards.Board; -import org.luxons.sevenwonders.game.boards.Provider; -import org.luxons.sevenwonders.game.boards.TradingRules; +import org.luxons.sevenwonders.game.resources.Provider; +import org.luxons.sevenwonders.game.resources.TradingRules; import org.luxons.sevenwonders.game.resources.ResourceType; public class Discount extends InstantOwnBoardEffect { diff --git a/src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java b/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java index 01277cab..ec261c8c 100644 --- a/src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java +++ b/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java @@ -1,7 +1,4 @@ -package org.luxons.sevenwonders.game.api; - -import org.luxons.sevenwonders.game.boards.Provider; -import org.luxons.sevenwonders.game.resources.Resources; +package org.luxons.sevenwonders.game.resources; public class BoughtResources { diff --git a/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java b/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java index bacd1e73..9a105725 100644 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java +++ b/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java @@ -1,4 +1,4 @@ -package org.luxons.sevenwonders.game.boards; +package org.luxons.sevenwonders.game.resources; public enum Provider { LEFT_PLAYER, RIGHT_PLAYER diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java b/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java new file mode 100644 index 00000000..4965c00e --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java @@ -0,0 +1,40 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.EnumMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +public class TradingRules { + + private final Map<ResourceType, Map<Provider, Integer>> costs = new EnumMap<>(ResourceType.class); + + private final int defaultCost; + + public TradingRules(int defaultCost) { + this.defaultCost = defaultCost; + } + + private int getCost(ResourceType type, Provider provider) { + return costs.computeIfAbsent(type, t -> new EnumMap<>(Provider.class)).getOrDefault(provider, defaultCost); + } + + public void setCost(ResourceType type, Provider provider, int cost) { + costs.computeIfAbsent(type, t -> new EnumMap<>(Provider.class)).put(provider, cost); + } + + public int computeCost(List<BoughtResources> boughtResources) { + return boughtResources.stream().mapToInt(this::computeCost).sum(); + } + + private int computeCost(BoughtResources boughtResources) { + Resources resources = boughtResources.getResources(); + int total = 0; + for (Entry<ResourceType, Integer> entry : resources.getQuantities().entrySet()) { + ResourceType type = entry.getKey(); + int count = entry.getValue(); + total += getCost(type, boughtResources.getProvider()) * count; + } + return total; + } +} 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 d739354a..15b2b863 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java @@ -3,7 +3,7 @@ package org.luxons.sevenwonders.game.wonders; import java.util.Arrays; import java.util.List; -import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.resources.ResourceType; diff --git a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java index 00a53fb1..64d506fc 100644 --- a/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java +++ b/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java @@ -2,7 +2,7 @@ package org.luxons.sevenwonders.game.wonders; import java.util.List; -import org.luxons.sevenwonders.game.api.BoughtResources; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.api.Table; import org.luxons.sevenwonders.game.cards.CardBack; import org.luxons.sevenwonders.game.cards.Requirements; |