From 54ba69d8961a96e7455401ec669f75f38352809f Mon Sep 17 00:00:00 2001 From: jbion Date: Thu, 22 Dec 2016 23:36:35 +0100 Subject: Add bought resources cost computation --- .../sevenwonders/game/api/BoughtResources.java | 27 --------------- .../org/luxons/sevenwonders/game/api/Move.java | 2 ++ .../org/luxons/sevenwonders/game/api/Table.java | 1 + .../org/luxons/sevenwonders/game/boards/Board.java | 1 + .../luxons/sevenwonders/game/boards/Provider.java | 5 --- .../sevenwonders/game/boards/TradingRules.java | 25 -------------- .../org/luxons/sevenwonders/game/cards/Card.java | 2 +- .../sevenwonders/game/cards/Requirements.java | 14 ++++++-- .../luxons/sevenwonders/game/effects/Discount.java | 4 +-- .../game/resources/BoughtResources.java | 24 +++++++++++++ .../sevenwonders/game/resources/Provider.java | 5 +++ .../sevenwonders/game/resources/TradingRules.java | 40 ++++++++++++++++++++++ .../luxons/sevenwonders/game/wonders/Wonder.java | 2 +- .../sevenwonders/game/wonders/WonderStage.java | 2 +- 14 files changed, 90 insertions(+), 64 deletions(-) delete mode 100644 src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/boards/Provider.java delete mode 100644 src/main/java/org/luxons/sevenwonders/game/boards/TradingRules.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/resources/Provider.java create mode 100644 src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java (limited to 'src/main/java/org/luxons') diff --git a/src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java b/src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java deleted file mode 100644 index 01277cab..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/api/BoughtResources.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.luxons.sevenwonders.game.api; - -import org.luxons.sevenwonders.game.boards.Provider; -import org.luxons.sevenwonders.game.resources.Resources; - -public class BoughtResources { - - private Provider provider; - - private Resources resources; - - public Provider getProvider() { - return provider; - } - - public void setProvider(Provider provider) { - this.provider = provider; - } - - public Resources getResources() { - return resources; - } - - public void setResources(Resources resources) { - this.resources = resources; - } -} 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/Provider.java b/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java deleted file mode 100644 index bacd1e73..00000000 --- a/src/main/java/org/luxons/sevenwonders/game/boards/Provider.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.luxons.sevenwonders.game.boards; - -public enum Provider { - LEFT_PLAYER, RIGHT_PLAYER -} 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> 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/resources/BoughtResources.java b/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java new file mode 100644 index 00000000..ec261c8c --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java @@ -0,0 +1,24 @@ +package org.luxons.sevenwonders.game.resources; + +public class BoughtResources { + + private Provider provider; + + private Resources resources; + + public Provider getProvider() { + return provider; + } + + public void setProvider(Provider provider) { + this.provider = provider; + } + + public Resources getResources() { + return resources; + } + + public void setResources(Resources resources) { + this.resources = resources; + } +} diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java b/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java new file mode 100644 index 00000000..9a105725 --- /dev/null +++ b/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java @@ -0,0 +1,5 @@ +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> 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) { + return boughtResources.stream().mapToInt(this::computeCost).sum(); + } + + private int computeCost(BoughtResources boughtResources) { + Resources resources = boughtResources.getResources(); + int total = 0; + for (Entry 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; -- cgit