diff options
Diffstat (limited to 'src')
14 files changed, 168 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; diff --git a/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java b/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java new file mode 100644 index 00000000..cd6661dc --- /dev/null +++ b/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java @@ -0,0 +1,96 @@ +package org.luxons.sevenwonders.game.resources; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; +import org.luxons.sevenwonders.game.test.TestUtils; + +import static org.junit.Assert.*; +import static org.junit.Assume.*; + +@RunWith(Theories.class) +public class TradingRulesTest { + + @DataPoints + public static int[] costs() { + return new int[] {0, 1, 2}; + } + + @DataPoints + public static Provider[] providers() { + return Provider.values(); + } + + @DataPoints + public static ResourceType[] resourceTypes() { + return ResourceType.values(); + } + + @Theory + public void computeCost_zeroForNoResources(int defaultCost) { + TradingRules rules = new TradingRules(defaultCost); + assertEquals(0, rules.computeCost(new ArrayList<>())); + } + + @Theory + public void computeCost_defaultCostWhenNoOverride(int defaultCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_twiceDefaultFor2Resources(int defaultCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type, type); + assertEquals(2 * defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_overriddenCost(int defaultCost, int overriddenCost, Provider provider, ResourceType type) { + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(type, provider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(overriddenCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_defaultCostWhenOverrideOnOtherProviderOrType(int defaultCost, int overriddenCost, + Provider overriddenProvider, ResourceType overriddenType, Provider provider, ResourceType type) { + assumeTrue(overriddenProvider != provider || overriddenType != type); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(overriddenType, overriddenProvider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, type); + assertEquals(defaultCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_oneDefaultAndOneOverriddenType(int defaultCost, int overriddenCost, + ResourceType overriddenType, Provider provider, ResourceType type) { + assumeTrue(overriddenType != type); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(overriddenType, provider, overriddenCost); + BoughtResources resources = TestUtils.createBoughtResources(provider, overriddenType, type); + assertEquals(defaultCost + overriddenCost, rules.computeCost(Collections.singletonList(resources))); + } + + @Theory + public void computeCost_oneDefaultAndOneOverriddenProvider(int defaultCost, int overriddenCost, + Provider overriddenProvider, Provider provider, ResourceType type) { + assumeTrue(overriddenProvider != provider); + TradingRules rules = new TradingRules(defaultCost); + rules.setCost(type, overriddenProvider, overriddenCost); + + List<BoughtResources> boughtResources = new ArrayList<>(2); + boughtResources.add(TestUtils.createBoughtResources(provider, type)); + boughtResources.add(TestUtils.createBoughtResources(overriddenProvider, type)); + + assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources)); + } + +}
\ No newline at end of file diff --git a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java index 968e4821..66a8e7c6 100644 --- a/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java +++ b/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java @@ -9,7 +9,9 @@ import org.luxons.sevenwonders.game.boards.Board; import org.luxons.sevenwonders.game.cards.Card; import org.luxons.sevenwonders.game.cards.Color; import org.luxons.sevenwonders.game.cards.Requirements; +import org.luxons.sevenwonders.game.resources.BoughtResources; import org.luxons.sevenwonders.game.resources.Production; +import org.luxons.sevenwonders.game.resources.Provider; import org.luxons.sevenwonders.game.resources.ResourceType; import org.luxons.sevenwonders.game.resources.Resources; import org.luxons.sevenwonders.game.wonders.Wonder; @@ -69,6 +71,13 @@ public class TestUtils { return resources; } + public static BoughtResources createBoughtResources(Provider provider, ResourceType... resources) { + BoughtResources boughtResources = new BoughtResources(); + boughtResources.setProvider(provider); + boughtResources.setResources(TestUtils.createResources(resources)); + return boughtResources; + } + public static List<Card> createSampleCards(int fromIndex, int nbCards) { List<Card> sampleCards = new ArrayList<>(); for (int i = fromIndex; i < fromIndex + nbCards; i++) { |