summaryrefslogtreecommitdiff
path: root/game-engine
diff options
context:
space:
mode:
Diffstat (limited to 'game-engine')
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java14
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java8
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java43
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java4
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/moves/Move.java10
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java4
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java21
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java56
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransaction.java53
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransactions.java71
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java10
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java15
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java6
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java6
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java15
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java3
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java44
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java12
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java116
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java36
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java31
-rw-r--r--game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java33
22 files changed, 359 insertions, 252 deletions
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java
index bad8f852..807e51a9 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/api/PlayerMove.java
@@ -1,10 +1,10 @@
package org.luxons.sevenwonders.game.api;
import java.util.ArrayList;
-import java.util.List;
+import java.util.Collection;
import org.luxons.sevenwonders.game.moves.MoveType;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransaction;
public class PlayerMove {
@@ -12,7 +12,7 @@ public class PlayerMove {
private String cardName;
- private List<BoughtResources> boughtResources = new ArrayList<>();
+ private Collection<ResourceTransaction> transactions = new ArrayList<>();
public MoveType getType() {
return type;
@@ -30,12 +30,12 @@ public class PlayerMove {
this.cardName = cardName;
}
- public List<BoughtResources> getBoughtResources() {
- return boughtResources;
+ public Collection<ResourceTransaction> getTransactions() {
+ return transactions;
}
- public void setBoughtResources(List<BoughtResources> boughtResources) {
- this.boughtResources = boughtResources;
+ public void setTransactions(Collection<ResourceTransaction> transactions) {
+ this.transactions = transactions;
}
@Override
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
index 084d19a5..2d2f6777 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Card.java
@@ -6,7 +6,7 @@ import java.util.Objects;
import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.boards.Board;
import org.luxons.sevenwonders.game.effects.Effect;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
public class Card {
@@ -93,13 +93,13 @@ public class Card {
if (!isAllowedOnBoard(board)) {
return false;
}
- return isChainableOn(board) || requirements.couldBeMetBy(table, playerIndex);
+ return isChainableOn(board) || requirements.areMetBy(table, playerIndex);
}
- public void applyTo(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ public void applyTo(Table table, int playerIndex, ResourceTransactions transactions) {
Board playerBoard = table.getBoard(playerIndex);
if (!isChainableOn(playerBoard)) {
- requirements.pay(table, playerIndex, boughtResources);
+ requirements.pay(table, playerIndex, transactions);
}
effects.forEach(e -> e.apply(table, playerIndex));
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java
index 3f9c4409..8c7245ed 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/cards/Requirements.java
@@ -1,12 +1,9 @@
package org.luxons.sevenwonders.game.cards;
-import java.util.List;
-
import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.boards.Board;
-import org.luxons.sevenwonders.game.boards.RelativeBoardPosition;
import org.luxons.sevenwonders.game.resources.BestPriceCalculator;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.Resources;
public class Requirements {
@@ -53,7 +50,7 @@ public class Requirements {
*
* @return true if the given board meets these requirements
*/
- public boolean areMetWithHelpBy(Board board, List<BoughtResources> boughtResources) {
+ public boolean areMetWithHelpBy(Board board, ResourceTransactions boughtResources) {
if (!hasRequiredGold(board, boughtResources)) {
return false;
}
@@ -64,7 +61,7 @@ public class Requirements {
}
/**
- * Returns whether the given player's board could meet these requirements, on its own or by buying resources to
+ * Returns whether the given player's board meets these requirements, either on its own or by buying resources to
* neighbours.
*
* @param table
@@ -74,7 +71,7 @@ public class Requirements {
*
* @return true if the given player's board could meet these requirements
*/
- boolean couldBeMetBy(Table table, int playerIndex) {
+ boolean areMetBy(Table table, int playerIndex) {
Board board = table.getBoard(playerIndex);
if (!hasRequiredGold(board)) {
return false;
@@ -89,8 +86,8 @@ public class Requirements {
return board.getGold() >= gold;
}
- private boolean hasRequiredGold(Board board, List<BoughtResources> boughtResources) {
- int resourcesPrice = board.getTradingRules().computeCost(boughtResources);
+ private boolean hasRequiredGold(Board board, ResourceTransactions resourceTransactions) {
+ int resourcesPrice = board.getTradingRules().computeCost(resourceTransactions);
return board.getGold() >= gold + resourcesPrice;
}
@@ -98,34 +95,14 @@ public class Requirements {
return board.getProduction().contains(resources);
}
- private boolean producesRequiredResourcesWithHelp(Board board, List<BoughtResources> boughtResources) {
- Resources totalBoughtResources = getTotalResources(boughtResources);
+ private boolean producesRequiredResourcesWithHelp(Board board, ResourceTransactions transactions) {
+ Resources totalBoughtResources = transactions.asResources();
Resources remainingResources = this.resources.minus(totalBoughtResources);
return board.getProduction().contains(remainingResources);
}
- private static Resources getTotalResources(List<BoughtResources> boughtResources) {
- return boughtResources.stream().map(BoughtResources::getResources).reduce(new Resources(), (r1, r2) -> {
- r1.addAll(r2);
- return r1;
- });
- }
-
- public void pay(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ public void pay(Table table, int playerIndex, ResourceTransactions transactions) {
table.getBoard(playerIndex).removeGold(gold);
- payBoughtResources(table, playerIndex, boughtResources);
- }
-
- private void payBoughtResources(Table table, int playerIndex, List<BoughtResources> boughtResourcesList) {
- boughtResourcesList.forEach(res -> payBoughtResources(table, playerIndex, res));
- }
-
- private void payBoughtResources(Table table, int playerIndex, BoughtResources boughtResources) {
- Board board = table.getBoard(playerIndex);
- int price = board.getTradingRules().computeCost(boughtResources);
- board.removeGold(price);
- RelativeBoardPosition providerPosition = boughtResources.getProvider().getBoardPosition();
- Board providerBoard = table.getBoard(playerIndex, providerPosition);
- providerBoard.addGold(price);
+ transactions.execute(table, playerIndex);
}
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
index f1cb50b3..f70d2626 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/BuildWonderMove.java
@@ -18,7 +18,7 @@ public class BuildWonderMove extends CardFromHandMove {
public void validate(Table table, List<Card> playerHand) throws InvalidMoveException {
super.validate(table, playerHand);
Board board = table.getBoard(getPlayerIndex());
- if (!board.getWonder().isNextStageBuildable(table, getPlayerIndex(), getBoughtResources())) {
+ if (!board.getWonder().isNextStageBuildable(table, getPlayerIndex(), getTransactions())) {
throw new InvalidMoveException(
String.format("Player %d cannot upgrade his wonder with the given resources", getPlayerIndex()));
}
@@ -34,6 +34,6 @@ public class BuildWonderMove extends CardFromHandMove {
public void activate(Table table, List<Card> discardedCards, Settings settings) {
int playerIndex = getPlayerIndex();
Board board = table.getBoard(playerIndex);
- board.getWonder().activateLastBuiltStage(table, playerIndex, getBoughtResources());
+ board.getWonder().activateLastBuiltStage(table, playerIndex, getTransactions());
}
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/Move.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
index 230b9ef1..cfb12e67 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/Move.java
@@ -6,7 +6,7 @@ import org.luxons.sevenwonders.game.Settings;
import org.luxons.sevenwonders.game.api.PlayerMove;
import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.cards.Card;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
public abstract class Move {
@@ -16,13 +16,13 @@ public abstract class Move {
private MoveType type;
- private List<BoughtResources> boughtResources;
+ private ResourceTransactions transactions;
Move(int playerIndex, Card card, PlayerMove move) {
this.playerIndex = playerIndex;
this.card = card;
this.type = move.getType();
- this.boughtResources = move.getBoughtResources();
+ this.transactions = new ResourceTransactions(move.getTransactions());
}
public int getPlayerIndex() {
@@ -37,8 +37,8 @@ public abstract class Move {
return type;
}
- public List<BoughtResources> getBoughtResources() {
- return boughtResources;
+ public ResourceTransactions getTransactions() {
+ return transactions;
}
public abstract void validate(Table table, List<Card> playerHand) throws InvalidMoveException;
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
index 82052981..18d6ba90 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/moves/PlayCardMove.java
@@ -19,7 +19,7 @@ public class PlayCardMove extends CardFromHandMove {
super.validate(table, playerHand);
Board board = table.getBoard(getPlayerIndex());
if (!getCard().isChainableOn(board) && !getCard().getRequirements()
- .areMetWithHelpBy(board, getBoughtResources())) {
+ .areMetWithHelpBy(board, getTransactions())) {
throw new InvalidMoveException(
String.format("Player %d cannot play the card %s with the given resources", getPlayerIndex(),
getCard().getName()));
@@ -34,6 +34,6 @@ public class PlayCardMove extends CardFromHandMove {
@Override
public void activate(Table table, List<Card> discardedCards, Settings settings) {
- getCard().applyTo(table, getPlayerIndex(), getBoughtResources());
+ getCard().applyTo(table, getPlayerIndex(), getTransactions());
}
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java
index bde6dcb9..10ac8343 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java
@@ -14,11 +14,11 @@ public class BestPriceCalculator {
private final List<ResourcePool> pools;
- private final List<BoughtResources> boughtResources;
+ private final ResourceTransactions boughtResources;
private int pricePaid;
- private List<BoughtResources> bestSolution;
+ private ResourceTransactions bestSolution;
private int bestPrice;
@@ -26,7 +26,7 @@ public class BestPriceCalculator {
Board board = table.getBoard(playerIndex);
this.resourcesLeftToPay = resourcesToPay.minus(board.getProduction().getFixedResources());
this.pools = createResourcePools(table, playerIndex);
- this.boughtResources = new ArrayList<>();
+ this.boughtResources = new ResourceTransactions();
this.pricePaid = 0;
this.bestSolution = null;
this.bestPrice = Integer.MAX_VALUE;
@@ -56,10 +56,10 @@ public class BestPriceCalculator {
return bestPriceCalculator.bestPrice;
}
- public static List<BoughtResources> bestSolution(Resources resources, Table table, int playerIndex) {
- BestPriceCalculator bestPriceCalculator = new BestPriceCalculator(resources, table, playerIndex);
- bestPriceCalculator.computePossibilities();
- return bestPriceCalculator.bestSolution;
+ public static ResourceTransactions bestSolution(Resources resources, Table table, int playerIndex) {
+ BestPriceCalculator calculator = new BestPriceCalculator(resources, table, playerIndex);
+ calculator.computePossibilities();
+ return calculator.bestSolution;
}
private void computePossibilities() {
@@ -77,15 +77,14 @@ public class BestPriceCalculator {
resourcesLeftToPay.add(type, 1);
continue;
}
- BoughtResources boughtRes = new BoughtResources(pool.getProvider(), Resources.of(type));
int cost = pool.getCost(type);
resourcesLeftToPay.remove(type, 1);
- boughtResources.add(boughtRes);
+ boughtResources.add(pool.getProvider(), Resources.of(type));
pricePaid += cost;
computePossibilitiesWhenUsing(type, pool);
pricePaid -= cost;
- boughtResources.remove(boughtRes);
+ boughtResources.remove(pool.getProvider(), Resources.of(type));
resourcesLeftToPay.add(type, 1);
}
}
@@ -106,7 +105,7 @@ public class BestPriceCalculator {
private void updateBestSolutionIfNeeded() {
if (pricePaid < bestPrice) {
bestPrice = pricePaid;
- bestSolution = new ArrayList<>(boughtResources);
+ bestSolution = new ResourceTransactions(boughtResources.toTransactions());
}
}
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java
deleted file mode 100644
index 71293190..00000000
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java
+++ /dev/null
@@ -1,56 +0,0 @@
-package org.luxons.sevenwonders.game.resources;
-
-import java.util.Objects;
-
-public class BoughtResources {
-
- private Provider provider;
-
- private Resources resources;
-
- public BoughtResources() {
- }
-
- public BoughtResources(Provider provider, Resources resources) {
- this.provider = provider;
- this.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;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
- BoughtResources that = (BoughtResources) o;
- return provider == that.provider && Objects.equals(resources, that.resources);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(provider, resources);
- }
-
- @Override
- public String toString() {
- return "BoughtResources{" + "provider=" + provider + ", resources=" + resources + '}';
- }
-}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransaction.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransaction.java
new file mode 100644
index 00000000..3cb8eea1
--- /dev/null
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransaction.java
@@ -0,0 +1,53 @@
+package org.luxons.sevenwonders.game.resources;
+
+import java.util.Objects;
+
+import org.luxons.sevenwonders.game.api.Table;
+import org.luxons.sevenwonders.game.boards.Board;
+import org.luxons.sevenwonders.game.boards.RelativeBoardPosition;
+
+public class ResourceTransaction {
+
+ private final Provider provider;
+
+ private final Resources resources;
+
+ public ResourceTransaction(Provider provider, Resources resources) {
+ this.provider = provider;
+ this.resources = resources;
+ }
+
+ public Provider getProvider() {
+ return provider;
+ }
+
+ public Resources getResources() {
+ return resources;
+ }
+
+ void execute(Table table, int playerIndex) {
+ Board board = table.getBoard(playerIndex);
+ int price = board.getTradingRules().computeCost(this);
+ board.removeGold(price);
+ RelativeBoardPosition providerPosition = provider.getBoardPosition();
+ Board providerBoard = table.getBoard(playerIndex, providerPosition);
+ providerBoard.addGold(price);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ ResourceTransaction that = (ResourceTransaction) o;
+ return provider == that.provider && Objects.equals(resources, that.resources);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(provider, resources);
+ }
+}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransactions.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransactions.java
new file mode 100644
index 00000000..a8fdc6c7
--- /dev/null
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransactions.java
@@ -0,0 +1,71 @@
+package org.luxons.sevenwonders.game.resources;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+import org.luxons.sevenwonders.game.api.Table;
+
+public class ResourceTransactions {
+
+ private final Map<Provider, Resources> resourcesByProvider;
+
+ public ResourceTransactions() {
+ this.resourcesByProvider = new HashMap<>();
+ }
+
+ public ResourceTransactions(Collection<ResourceTransaction> transactions) {
+ this();
+ transactions.forEach(t -> add(t.getProvider(), t.getResources()));
+ }
+
+ public void add(Provider provider, Resources resources) {
+ resourcesByProvider.putIfAbsent(provider, new Resources());
+ resourcesByProvider.merge(provider, resources, Resources::addAll);
+ }
+
+ public void remove(Provider provider, Resources resources) {
+ resourcesByProvider.compute(provider, (p, prevResources) -> {
+ if (prevResources == null) {
+ throw new IllegalStateException("Cannot remove resources from resource transactions");
+ }
+ return prevResources.minus(resources);
+ });
+ }
+
+ public void execute(Table table, int playerIndex) {
+ toTransactions().forEach(t -> t.execute(table, playerIndex));
+ }
+
+ public Set<ResourceTransaction> toTransactions() {
+ return resourcesByProvider.entrySet()
+ .stream()
+ .filter(e -> !e.getValue().isEmpty())
+ .map(e -> new ResourceTransaction(e.getKey(), e.getValue()))
+ .collect(Collectors.toSet());
+ }
+
+ public Resources asResources() {
+ return resourcesByProvider.values().stream().reduce(new Resources(), Resources::addAll);
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ ResourceTransactions that = (ResourceTransactions) o;
+ return Objects.equals(resourcesByProvider, that.resourcesByProvider);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(resourcesByProvider);
+ }
+}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
index 1b263760..d354f121 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
@@ -32,8 +32,9 @@ public class Resources {
quantities.computeIfPresent(type, (t, oldQty) -> oldQty - quantity);
}
- public void addAll(Resources resources) {
+ public Resources addAll(Resources resources) {
resources.quantities.forEach(this::add);
+ return this;
}
public int getQuantity(ResourceType type) {
@@ -55,6 +56,13 @@ public class Resources {
return quantity.getValue() <= getQuantity(quantity.getKey());
}
+ public Resources plus(Resources resources) {
+ Resources merged = new Resources();
+ merged.addAll(this);
+ merged.addAll(resources);
+ return merged;
+ }
+
/**
* Creates new {@link Resources} object containing these resources minus the given resources.
*
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java
index 8cd1d9bc..ffbce8ab 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java
@@ -1,7 +1,6 @@
package org.luxons.sevenwonders.game.resources;
import java.util.EnumMap;
-import java.util.List;
import java.util.Map;
public class TradingRules {
@@ -26,13 +25,17 @@ public class TradingRules {
costs.computeIfAbsent(type, t -> new EnumMap<>(Provider.class)).put(provider, cost);
}
- public int computeCost(List<BoughtResources> boughtResources) {
- return boughtResources.stream().mapToInt(this::computeCost).sum();
+ public int computeCost(ResourceTransactions transactions) {
+ return transactions.toTransactions().stream().mapToInt(this::computeCost).sum();
}
- public int computeCost(BoughtResources boughtResources) {
- Resources resources = boughtResources.getResources();
- Provider provider = boughtResources.getProvider();
+ int computeCost(ResourceTransaction transaction) {
+ Resources resources = transaction.getResources();
+ Provider provider = transaction.getProvider();
+ return computeCost(resources, provider);
+ }
+
+ private int computeCost(Resources resources, Provider provider) {
int total = 0;
for (ResourceType type : ResourceType.values()) {
int count = resources.getQuantity(type);
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
index 73fff305..dd0170da 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/Wonder.java
@@ -5,7 +5,7 @@ import java.util.List;
import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.cards.CardBack;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.ResourceType;
public class Wonder {
@@ -63,7 +63,7 @@ public class Wonder {
this.image = image;
}
- public boolean isNextStageBuildable(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ public boolean isNextStageBuildable(Table table, int playerIndex, ResourceTransactions boughtResources) {
int nextLevel = getNbBuiltStages();
if (nextLevel == stages.size()) {
return false;
@@ -83,7 +83,7 @@ public class Wonder {
return stages.get(nextLevel);
}
- public void activateLastBuiltStage(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ public void activateLastBuiltStage(Table table, int playerIndex, ResourceTransactions boughtResources) {
getLastBuiltStage().activate(table, playerIndex, boughtResources);
}
diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java
index a53e2694..f4ec1140 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/wonders/WonderStage.java
@@ -7,7 +7,7 @@ import org.luxons.sevenwonders.game.boards.Board;
import org.luxons.sevenwonders.game.cards.CardBack;
import org.luxons.sevenwonders.game.cards.Requirements;
import org.luxons.sevenwonders.game.effects.Effect;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
public class WonderStage {
@@ -38,7 +38,7 @@ public class WonderStage {
return cardBack != null;
}
- public boolean isBuildable(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ public boolean isBuildable(Table table, int playerIndex, ResourceTransactions boughtResources) {
Board board = table.getBoard(playerIndex);
return requirements.areMetWithHelpBy(board, boughtResources);
}
@@ -47,7 +47,7 @@ public class WonderStage {
this.cardBack = cardBack;
}
- void activate(Table table, int playerIndex, List<BoughtResources> boughtResources) {
+ void activate(Table table, int playerIndex, ResourceTransactions boughtResources) {
effects.forEach(e -> e.apply(table, playerIndex));
requirements.pay(table, playerIndex, boughtResources);
}
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java
index 73315dd2..ff34dc27 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/GameTest.java
@@ -5,6 +5,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.junit.Test;
import org.luxons.sevenwonders.game.api.CustomizableSettings;
@@ -17,7 +18,8 @@ import org.luxons.sevenwonders.game.data.GameDefinitionLoader;
import org.luxons.sevenwonders.game.moves.Move;
import org.luxons.sevenwonders.game.moves.MoveType;
import org.luxons.sevenwonders.game.resources.BestPriceCalculator;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
+import org.luxons.sevenwonders.game.resources.ResourceTransaction;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.Resources;
import org.luxons.sevenwonders.game.test.TestUtils;
@@ -95,7 +97,7 @@ public class GameTest {
private static PlayerMove createPlayCardMove(PlayerTurnInfo turnInfo) {
for (HandCard handCard : turnInfo.getHand()) {
if (handCard.isPlayable()) {
- List<BoughtResources> resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo);
+ Set<ResourceTransaction> resourcesToBuy = findResourcesToBuyFor(handCard, turnInfo);
return TestUtils.createPlayerMove(handCard.getCard().getName(), MoveType.PLAY, resourcesToBuy);
}
}
@@ -103,12 +105,15 @@ public class GameTest {
return TestUtils.createPlayerMove(firstCardInHand.getCard().getName(), MoveType.DISCARD);
}
- private static List<BoughtResources> findResourcesToBuyFor(HandCard handCard, PlayerTurnInfo turnInfo) {
+ private static Set<ResourceTransaction> findResourcesToBuyFor(HandCard handCard, PlayerTurnInfo turnInfo) {
if (handCard.isFree()) {
- return Collections.emptyList();
+ return Collections.emptySet();
}
Resources requiredResources = handCard.getCard().getRequirements().getResources();
- return BestPriceCalculator.bestSolution(requiredResources, turnInfo.getTable(), turnInfo.getPlayerIndex());
+ Table table = turnInfo.getTable();
+ int playerIndex = turnInfo.getPlayerIndex();
+ ResourceTransactions transactions = BestPriceCalculator.bestSolution(requiredResources, table, playerIndex);
+ return transactions.toTransactions();
}
private static PlayerMove createPickGuildMove(PlayerTurnInfo turnInfo) {
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java
index 437c5f21..2a3b6806 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/CardTest.java
@@ -11,6 +11,7 @@ import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.boards.Board;
import org.luxons.sevenwonders.game.effects.Effect;
import org.luxons.sevenwonders.game.effects.ProductionIncrease;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.ResourceType;
import org.luxons.sevenwonders.game.wonders.Wonder;
@@ -52,7 +53,7 @@ public class CardTest {
table.getBoard(0).setGold(3);
table.getBoard(1).setGold(3);
table.getBoard(2).setGold(3);
- treeFarmCard.applyTo(table, 0, new ArrayList<>());
+ treeFarmCard.applyTo(table, 0, new ResourceTransactions());
assertEquals(2, table.getBoard(0).getGold());
assertEquals(3, table.getBoard(1).getGold());
assertEquals(3, table.getBoard(2).getGold());
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java
index b01f9002..d2f505a4 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/cards/RequirementsTest.java
@@ -10,8 +10,8 @@ import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.luxons.sevenwonders.game.api.Table;
import org.luxons.sevenwonders.game.boards.Board;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
import org.luxons.sevenwonders.game.resources.Provider;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.ResourceType;
import org.luxons.sevenwonders.game.resources.Resources;
import org.luxons.sevenwonders.game.test.TestUtils;
@@ -27,7 +27,7 @@ public class RequirementsTest {
@DataPoints
public static int[] goldAmounts() {
- return new int[]{0, 1, 2, 5};
+ return new int[] {0, 1, 2, 5};
}
@DataPoints
@@ -36,13 +36,13 @@ public class RequirementsTest {
}
@Test
- public void getResources_emptyAfterInit() throws Exception {
+ public void getResources_emptyAfterInit() {
Requirements requirements = new Requirements();
assertTrue(requirements.getResources().isEmpty());
}
@Test
- public void setResources_success() throws Exception {
+ public void setResources_success() {
Requirements requirements = new Requirements();
Resources resources = new Resources();
requirements.setResources(resources);
@@ -58,8 +58,8 @@ public class RequirementsTest {
Table table = new Table(Collections.singletonList(board));
assertEquals(boardGold >= requiredGold, requirements.areMetWithoutNeighboursBy(board));
- assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, Collections.emptyList()));
- assertEquals(boardGold >= requiredGold, requirements.couldBeMetBy(table, 0));
+ assertEquals(boardGold >= requiredGold, requirements.areMetWithHelpBy(board, new ResourceTransactions()));
+ assertEquals(boardGold >= requiredGold, requirements.areMetBy(table, 0));
}
@Theory
@@ -71,16 +71,16 @@ public class RequirementsTest {
assertEquals(initialResource == requiredResource, requirements.areMetWithoutNeighboursBy(board));
assertEquals(initialResource == requiredResource,
- requirements.areMetWithHelpBy(board, Collections.emptyList()));
+ requirements.areMetWithHelpBy(board, new ResourceTransactions()));
if (initialResource == requiredResource) {
- assertTrue(requirements.couldBeMetBy(table, 0));
+ assertTrue(requirements.areMetBy(table, 0));
}
}
@Theory
public void resourceRequirement_ownProduction(ResourceType initialResource, ResourceType producedResource,
- ResourceType requiredResource) {
+ ResourceType requiredResource) {
assumeTrue(initialResource != requiredResource);
Requirements requirements = TestUtils.createRequirements(requiredResource);
@@ -91,16 +91,16 @@ public class RequirementsTest {
assertEquals(producedResource == requiredResource, requirements.areMetWithoutNeighboursBy(board));
assertEquals(producedResource == requiredResource,
- requirements.areMetWithHelpBy(board, Collections.emptyList()));
+ requirements.areMetWithHelpBy(board, new ResourceTransactions()));
if (producedResource == requiredResource) {
- assertTrue(requirements.couldBeMetBy(table, 0));
+ assertTrue(requirements.areMetBy(table, 0));
}
}
@Theory
public void resourceRequirement_boughtResource(ResourceType initialResource, ResourceType boughtResource,
- ResourceType requiredResource) {
+ ResourceType requiredResource) {
assumeTrue(initialResource != requiredResource);
Requirements requirements = TestUtils.createRequirements(requiredResource);
@@ -110,16 +110,13 @@ public class RequirementsTest {
neighbourBoard.getPublicProduction().addFixedResource(boughtResource, 1);
Table table = new Table(Arrays.asList(board, neighbourBoard));
- BoughtResources resources = new BoughtResources();
- resources.setProvider(Provider.RIGHT_PLAYER);
- resources.setResources(TestUtils.createResources(boughtResource));
+ ResourceTransactions resources = TestUtils.createTransactions(Provider.RIGHT_PLAYER, boughtResource);
assertFalse(requirements.areMetWithoutNeighboursBy(board));
- assertEquals(boughtResource == requiredResource,
- requirements.areMetWithHelpBy(board, Collections.singletonList(resources)));
+ assertEquals(boughtResource == requiredResource, requirements.areMetWithHelpBy(board, resources));
if (boughtResource == requiredResource) {
- assertTrue(requirements.couldBeMetBy(table, 0));
+ assertTrue(requirements.areMetBy(table, 0));
}
}
@@ -133,15 +130,14 @@ public class RequirementsTest {
Board neighbourBoard = TestUtils.createBoard(requiredResource, 0);
Table table = new Table(Arrays.asList(board, neighbourBoard));
- BoughtResources boughtResources = new BoughtResources();
- boughtResources.setProvider(Provider.RIGHT_PLAYER);
- boughtResources.setResources(TestUtils.createResources(requiredResource));
+ ResourceTransactions transactions = TestUtils.createTransactions(Provider.RIGHT_PLAYER,
+ requiredResource);
assertFalse(requirements.areMetWithoutNeighboursBy(board));
- assertTrue(requirements.areMetWithHelpBy(board, Collections.singletonList(boughtResources)));
- assertTrue(requirements.couldBeMetBy(table, 0));
+ assertTrue(requirements.areMetWithHelpBy(board, transactions));
+ assertTrue(requirements.areMetBy(table, 0));
- requirements.pay(table, 0, Collections.singletonList(boughtResources));
+ requirements.pay(table, 0, transactions);
assertEquals(0, board.getGold());
assertEquals(2, neighbourBoard.getGold());
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java
index c6819d3a..1de3a502 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/effects/DiscountTest.java
@@ -6,8 +6,8 @@ import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;
import org.luxons.sevenwonders.game.boards.Board;
-import org.luxons.sevenwonders.game.resources.BoughtResources;
import org.luxons.sevenwonders.game.resources.Provider;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.ResourceType;
import org.luxons.sevenwonders.game.test.TestUtils;
@@ -40,8 +40,8 @@ public class DiscountTest {
discount.getResourceTypes().add(discountedType);
discount.apply(board);
- BoughtResources boughtResources = TestUtils.createBoughtResources(provider, discountedType);
- assertEquals(discountedPrice, board.getTradingRules().computeCost(boughtResources));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, discountedType);
+ assertEquals(discountedPrice, board.getTradingRules().computeCost(transactions));
}
@Theory
@@ -60,13 +60,13 @@ public class DiscountTest {
// this is the default in the settings used by TestUtils.createBoard()
int normalPrice = 2;
- BoughtResources fromOtherType = TestUtils.createBoughtResources(provider, otherType);
+ ResourceTransactions fromOtherType = TestUtils.createTransactions(provider, otherType);
assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherType));
- BoughtResources fromOtherProvider = TestUtils.createBoughtResources(otherProvider, discountedType);
+ ResourceTransactions fromOtherProvider = TestUtils.createTransactions(otherProvider, discountedType);
assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProvider));
- BoughtResources fromOtherProviderAndType = TestUtils.createBoughtResources(otherProvider, otherType);
+ ResourceTransactions fromOtherProviderAndType = TestUtils.createTransactions(otherProvider, otherType);
assertEquals(normalPrice, board.getTradingRules().computeCost(fromOtherProviderAndType));
}
}
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java
index 9f43ec6b..a5017097 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/BestPriceCalculatorTest.java
@@ -1,7 +1,6 @@
package org.luxons.sevenwonders.game.resources;
import java.util.Arrays;
-import java.util.Collections;
import org.junit.Test;
import org.luxons.sevenwonders.game.api.Table;
@@ -9,6 +8,13 @@ import org.luxons.sevenwonders.game.boards.Board;
import org.luxons.sevenwonders.game.test.TestUtils;
import static org.junit.Assert.assertEquals;
+import static org.luxons.sevenwonders.game.resources.Provider.LEFT_PLAYER;
+import static org.luxons.sevenwonders.game.resources.Provider.RIGHT_PLAYER;
+import static org.luxons.sevenwonders.game.resources.ResourceType.CLAY;
+import static org.luxons.sevenwonders.game.resources.ResourceType.GLASS;
+import static org.luxons.sevenwonders.game.resources.ResourceType.ORE;
+import static org.luxons.sevenwonders.game.resources.ResourceType.STONE;
+import static org.luxons.sevenwonders.game.resources.ResourceType.WOOD;
public class BestPriceCalculatorTest {
@@ -17,107 +23,107 @@ public class BestPriceCalculatorTest {
Table table = TestUtils.createTable(3);
Resources resources = new Resources();
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 0));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 0));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 0));
}
@Test
public void bestPrice_fixedResources_defaultCost() {
- Board left = TestUtils.createBoard(ResourceType.STONE);
- Board main = TestUtils.createBoard(ResourceType.STONE);
- Board right = TestUtils.createBoard(ResourceType.WOOD);
+ Board left = TestUtils.createBoard(STONE);
+ Board main = TestUtils.createBoard(STONE);
+ Board right = TestUtils.createBoard(WOOD);
Table table = new Table(Arrays.asList(main, right, left));
- Resources resources = new Resources();
- resources.add(ResourceType.STONE, 2);
+ Resources resources = TestUtils.createResources(STONE, STONE);
assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 0));
assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 1));
assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2));
- BoughtResources stoneLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.STONE));
- BoughtResources stoneRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.STONE));
- assertEquals(Collections.singletonList(stoneLeft), BestPriceCalculator.bestSolution(resources, table, 0));
- assertEquals(Arrays.asList(stoneLeft, stoneRight), BestPriceCalculator.bestSolution(resources, table, 1));
- assertEquals(Collections.singletonList(stoneRight), BestPriceCalculator.bestSolution(resources, table, 2));
+ ResourceTransaction stoneLeftSingle = TestUtils.createTransaction(LEFT_PLAYER, STONE);
+ ResourceTransaction stoneRightSingle = TestUtils.createTransaction(RIGHT_PLAYER, STONE);
+
+ ResourceTransactions stoneLeft = TestUtils.createTransactions(stoneLeftSingle);
+ ResourceTransactions stoneRight = TestUtils.createTransactions(stoneRightSingle);
+ ResourceTransactions stoneLeftAndRight = TestUtils.createTransactions(stoneLeftSingle, stoneRightSingle);
+
+ assertEquals(stoneLeft, BestPriceCalculator.bestSolution(resources, table, 0));
+ assertEquals(stoneLeftAndRight, BestPriceCalculator.bestSolution(resources, table, 1));
+ assertEquals(stoneRight, BestPriceCalculator.bestSolution(resources, table, 2));
}
@Test
public void bestPrice_fixedResources_overridenCost() {
- Board main = TestUtils.createBoard(ResourceType.STONE);
- main.getTradingRules().setCost(ResourceType.WOOD, Provider.RIGHT_PLAYER, 1);
+ Board main = TestUtils.createBoard(STONE);
+ main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1);
- Board left = TestUtils.createBoard(ResourceType.WOOD);
- Board right = TestUtils.createBoard(ResourceType.WOOD);
- Board opposite = TestUtils.createBoard(ResourceType.GLASS);
+ Board left = TestUtils.createBoard(WOOD);
+ Board right = TestUtils.createBoard(WOOD);
+ Board opposite = TestUtils.createBoard(GLASS);
Table table = new Table(Arrays.asList(main, right, opposite, left));
- Resources resources = new Resources();
- resources.add(ResourceType.WOOD, 1);
+ Resources resources = TestUtils.createResources(WOOD);
assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0));
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1));
assertEquals(2, BestPriceCalculator.bestPrice(resources, table, 2));
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 3));
- BoughtResources woodLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.WOOD));
- BoughtResources woodRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.WOOD));
- assertEquals(Collections.singletonList(woodRight), BestPriceCalculator.bestSolution(resources, table, 0));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1));
- assertEquals(Collections.singletonList(woodLeft), BestPriceCalculator.bestSolution(resources, table, 2));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 3));
+ ResourceTransactions woodLeft = TestUtils.createTransactions(LEFT_PLAYER, WOOD);
+ ResourceTransactions woodRight = TestUtils.createTransactions(RIGHT_PLAYER, WOOD);
+ assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1));
+ assertEquals(woodLeft, BestPriceCalculator.bestSolution(resources, table, 2));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 3));
}
@Test
public void bestPrice_mixedResources_overridenCost() {
- Board left = TestUtils.createBoard(ResourceType.WOOD);
+ Board left = TestUtils.createBoard(WOOD);
- Board main = TestUtils.createBoard(ResourceType.STONE);
- main.getTradingRules().setCost(ResourceType.WOOD, Provider.RIGHT_PLAYER, 1);
+ Board main = TestUtils.createBoard(STONE);
+ main.getTradingRules().setCost(WOOD, RIGHT_PLAYER, 1);
- Board right = TestUtils.createBoard(ResourceType.ORE);
- right.getProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY);
- right.getPublicProduction().addChoice(ResourceType.WOOD, ResourceType.CLAY);
+ Board right = TestUtils.createBoard(ORE);
+ right.getProduction().addChoice(WOOD, CLAY);
+ right.getPublicProduction().addChoice(WOOD, CLAY);
Table table = new Table(Arrays.asList(main, right, left));
- Resources resources = new Resources();
- resources.add(ResourceType.WOOD, 1);
+ Resources resources = TestUtils.createResources(WOOD);
assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0));
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1));
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 2));
- BoughtResources woodRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.WOOD));
- assertEquals(Collections.singletonList(woodRight), BestPriceCalculator.bestSolution(resources, table, 0));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 2));
+ ResourceTransactions woodRight = TestUtils.createTransactions(RIGHT_PLAYER, WOOD);
+
+ assertEquals(woodRight, BestPriceCalculator.bestSolution(resources, table, 0));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 2));
}
@Test
public void bestPrice_chooseCheapest() {
- Board left = TestUtils.createBoard(ResourceType.WOOD);
+ Board left = TestUtils.createBoard(WOOD);
- Board main = TestUtils.createBoard(ResourceType.WOOD);
- main.getProduction().addChoice(ResourceType.CLAY, ResourceType.ORE);
- main.getTradingRules().setCost(ResourceType.CLAY, Provider.RIGHT_PLAYER, 1);
+ Board main = TestUtils.createBoard(WOOD);
+ main.getProduction().addChoice(CLAY, ORE);
+ main.getTradingRules().setCost(CLAY, RIGHT_PLAYER, 1);
- Board right = TestUtils.createBoard(ResourceType.WOOD);
- right.getProduction().addFixedResource(ResourceType.ORE, 1);
- right.getProduction().addFixedResource(ResourceType.CLAY, 1);
- right.getPublicProduction().addFixedResource(ResourceType.ORE, 1);
- right.getPublicProduction().addFixedResource(ResourceType.CLAY, 1);
+ Board right = TestUtils.createBoard(WOOD);
+ right.getProduction().addFixedResource(ORE, 1);
+ right.getProduction().addFixedResource(CLAY, 1);
+ right.getPublicProduction().addFixedResource(ORE, 1);
+ right.getPublicProduction().addFixedResource(CLAY, 1);
Table table = new Table(Arrays.asList(main, right, left));
- Resources resources = new Resources();
- resources.add(ResourceType.ORE, 1);
- resources.add(ResourceType.CLAY, 1);
+ Resources resources = TestUtils.createResources(ORE, CLAY);
assertEquals(1, BestPriceCalculator.bestPrice(resources, table, 0));
assertEquals(0, BestPriceCalculator.bestPrice(resources, table, 1));
assertEquals(4, BestPriceCalculator.bestPrice(resources, table, 2));
- BoughtResources oreLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.ORE));
- BoughtResources clayLeft = new BoughtResources(Provider.LEFT_PLAYER, Resources.of(ResourceType.CLAY));
- BoughtResources clayRight = new BoughtResources(Provider.RIGHT_PLAYER, Resources.of(ResourceType.CLAY));
- assertEquals(Collections.singletonList(clayRight), BestPriceCalculator.bestSolution(resources, table, 0));
- assertEquals(Collections.emptyList(), BestPriceCalculator.bestSolution(resources, table, 1));
- assertEquals(Arrays.asList(oreLeft, clayLeft), BestPriceCalculator.bestSolution(resources, table, 2));
+ ResourceTransactions oreAndClayLeft = TestUtils.createTransactions(LEFT_PLAYER, ORE, CLAY);
+ ResourceTransactions clayRight = TestUtils.createTransactions(RIGHT_PLAYER, CLAY);
+ assertEquals(clayRight, BestPriceCalculator.bestSolution(resources, table, 0));
+ assertEquals(new ResourceTransactions(), BestPriceCalculator.bestSolution(resources, table, 1));
+ assertEquals(oreAndClayLeft, BestPriceCalculator.bestSolution(resources, table, 2));
}
}
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java
new file mode 100644
index 00000000..68795053
--- /dev/null
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/ResourceTransactionsTest.java
@@ -0,0 +1,36 @@
+package org.luxons.sevenwonders.game.resources;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.junit.Test;
+import org.luxons.sevenwonders.game.test.TestUtils;
+
+import static org.junit.Assert.assertEquals;
+
+public class ResourceTransactionsTest {
+
+ @Test
+ public void toTransactions() {
+ List<ResourceTransaction> transactionList = new ArrayList<>();
+ transactionList.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD));
+ transactionList.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.CLAY));
+ transactionList.add(TestUtils.createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD));
+
+ ResourceTransactions transactions = new ResourceTransactions(transactionList);
+
+ Set<ResourceTransaction> expectedNormalized = new HashSet<>();
+ expectedNormalized.add(TestUtils.createTransaction(Provider.LEFT_PLAYER, ResourceType.WOOD, ResourceType.CLAY));
+ expectedNormalized.add(TestUtils.createTransaction(Provider.RIGHT_PLAYER, ResourceType.WOOD));
+
+ assertEquals(expectedNormalized, new HashSet<>(transactions.toTransactions()));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void remove_failsIfNoResourceForThatProvider() {
+ ResourceTransactions transactions = new ResourceTransactions();
+ transactions.remove(Provider.LEFT_PLAYER, Resources.of(ResourceType.WOOD));
+ }
+}
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java
index 0fd1b52b..d9f32c78 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/resources/TradingRulesTest.java
@@ -1,7 +1,6 @@
package org.luxons.sevenwonders.game.resources;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import org.junit.experimental.theories.DataPoints;
@@ -47,29 +46,29 @@ public class TradingRulesTest {
@Theory
public void computeCost_zeroForNoResources(int defaultCost) {
TradingRules rules = new TradingRules(defaultCost);
- assertEquals(0, rules.computeCost(new ArrayList<>()));
+ assertEquals(0, rules.computeCost(new ResourceTransactions()));
}
@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)));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, type);
+ assertEquals(defaultCost, rules.computeCost(transactions));
}
@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)));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, type, type);
+ assertEquals(2 * defaultCost, rules.computeCost(transactions));
}
@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)));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, type);
+ assertEquals(overriddenCost, rules.computeCost(transactions));
}
@Theory
@@ -80,8 +79,8 @@ public class TradingRulesTest {
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)));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, type);
+ assertEquals(defaultCost, rules.computeCost(transactions));
}
@Theory
@@ -91,8 +90,8 @@ public class TradingRulesTest {
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)));
+ ResourceTransactions transactions = TestUtils.createTransactions(provider, overriddenType, type);
+ assertEquals(defaultCost + overriddenCost, rules.computeCost(transactions));
}
@Theory
@@ -103,10 +102,10 @@ public class TradingRulesTest {
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));
+ List<ResourceTransaction> boughtResources = new ArrayList<>(2);
+ boughtResources.add(TestUtils.createTransaction(provider, type));
+ boughtResources.add(TestUtils.createTransaction(overriddenProvider, type));
- assertEquals(defaultCost + overriddenCost, rules.computeCost(boughtResources));
+ assertEquals(defaultCost + overriddenCost, rules.computeCost(new ResourceTransactions(boughtResources)));
}
}
diff --git a/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java b/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
index 5adaa098..73a08a32 100644
--- a/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
+++ b/game-engine/src/test/java/org/luxons/sevenwonders/game/test/TestUtils.java
@@ -2,6 +2,7 @@ package org.luxons.sevenwonders.game.test;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@@ -22,9 +23,10 @@ import org.luxons.sevenwonders.game.effects.Effect;
import org.luxons.sevenwonders.game.effects.ScienceProgress;
import org.luxons.sevenwonders.game.moves.Move;
import org.luxons.sevenwonders.game.moves.MoveType;
-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.ResourceTransaction;
+import org.luxons.sevenwonders.game.resources.ResourceTransactions;
import org.luxons.sevenwonders.game.resources.ResourceType;
import org.luxons.sevenwonders.game.resources.Resources;
import org.luxons.sevenwonders.game.wonders.Wonder;
@@ -111,11 +113,17 @@ 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 ResourceTransactions createTransactions(Provider provider, ResourceType... resources) {
+ ResourceTransaction transaction = createTransaction(provider, resources);
+ return new ResourceTransactions(Collections.singletonList(transaction));
+ }
+
+ public static ResourceTransactions createTransactions(ResourceTransaction... transactions) {
+ return new ResourceTransactions(Arrays.asList(transactions));
+ }
+
+ public static ResourceTransaction createTransaction(Provider provider, ResourceType... resources) {
+ return new ResourceTransaction(provider, TestUtils.createResources(resources));
}
public static Requirements createRequirements(ResourceType... types) {
@@ -216,23 +224,24 @@ public class TestUtils {
Card card = createCard(color, effect);
Board board = table.getBoard(playerIndex);
board.addCard(card);
- card.applyTo(table, playerIndex, Collections.emptyList());
+ card.applyTo(table, playerIndex, new ResourceTransactions());
}
- public static Move createMove(int playerIndex, Card card, MoveType type, BoughtResources... boughtResources) {
- PlayerMove playerMove = createPlayerMove(card.getName(), type, Arrays.asList(boughtResources));
+ public static Move createMove(int playerIndex, Card card, MoveType type, ResourceTransaction... transactions) {
+ PlayerMove playerMove = createPlayerMove(card.getName(), type, Arrays.asList(transactions));
return type.resolve(playerIndex, card, playerMove);
}
public static PlayerMove createPlayerMove(String cardName, MoveType type) {
- return createPlayerMove(cardName, type, Collections.emptyList());
+ return createPlayerMove(cardName, type, Collections.emptySet());
}
- public static PlayerMove createPlayerMove(String cardName, MoveType type, List<BoughtResources> boughtResources) {
+ public static PlayerMove createPlayerMove(String cardName, MoveType type,
+ Collection<ResourceTransaction> transactions) {
PlayerMove playerMove = new PlayerMove();
playerMove.setCardName(cardName);
playerMove.setType(type);
- playerMove.setBoughtResources(boughtResources);
+ playerMove.setTransactions(transactions);
return playerMove;
}
}
bgstack15