summaryrefslogtreecommitdiff
path: root/game-engine/src/main
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2018-04-26 01:08:21 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2018-04-26 01:08:21 +0200
commit598beca9a5b5357e15b2e7803876446a4484d1ee (patch)
tree9f1b636c8033863e04fcecb602c4795b40f12e12 /game-engine/src/main
parentExtract move creation in GameTest (diff)
downloadseven-wonders-598beca9a5b5357e15b2e7803876446a4484d1ee.tar.gz
seven-wonders-598beca9a5b5357e15b2e7803876446a4484d1ee.tar.bz2
seven-wonders-598beca9a5b5357e15b2e7803876446a4484d1ee.zip
Fix and improve BestPriceCalculator
Diffstat (limited to 'game-engine/src/main')
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java96
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BoughtResources.java32
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourcePool.java8
-rw-r--r--game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java13
4 files changed, 117 insertions, 32 deletions
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 0cae3fe2..bde6dcb9 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
@@ -10,13 +10,26 @@ import org.luxons.sevenwonders.game.boards.Board;
public class BestPriceCalculator {
- private final Resources resources;
+ private final Resources resourcesLeftToPay;
private final List<ResourcePool> pools;
- private BestPriceCalculator(Resources resources, Table table, int playerIndex) {
- this.resources = resources;
+ private final List<BoughtResources> boughtResources;
+
+ private int pricePaid;
+
+ private List<BoughtResources> bestSolution;
+
+ private int bestPrice;
+
+ private BestPriceCalculator(Resources resourcesToPay, Table table, int playerIndex) {
+ Board board = table.getBoard(playerIndex);
+ this.resourcesLeftToPay = resourcesToPay.minus(board.getProduction().getFixedResources());
this.pools = createResourcePools(table, playerIndex);
+ this.boughtResources = new ArrayList<>();
+ this.pricePaid = 0;
+ this.bestSolution = null;
+ this.bestPrice = Integer.MAX_VALUE;
}
private static List<ResourcePool> createResourcePools(Table table, int playerIndex) {
@@ -25,53 +38,76 @@ public class BestPriceCalculator {
Board board = table.getBoard(playerIndex);
TradingRules rules = board.getTradingRules();
- pools.add(new ResourcePool(board.getProduction(), null, rules));
+ // we only take alternative resources here, because fixed resources were already removed for optimization
+ Set<Set<ResourceType>> ownBoardChoices = board.getProduction().getAlternativeResources();
+ pools.add(new ResourcePool(null, rules, ownBoardChoices));
for (Provider provider : providers) {
Board providerBoard = table.getBoard(playerIndex, provider.getBoardPosition());
- ResourcePool pool = new ResourcePool(providerBoard.getPublicProduction(), provider, rules);
+ ResourcePool pool = new ResourcePool(provider, rules, providerBoard.getPublicProduction().asChoices());
pools.add(pool);
}
return pools;
}
public static int bestPrice(Resources resources, Table table, int playerIndex) {
- Board board = table.getBoard(playerIndex);
- Resources leftToPay = resources.minus(board.getProduction().getFixedResources());
- return new BestPriceCalculator(leftToPay, table, playerIndex).bestPrice();
+ BestPriceCalculator bestPriceCalculator = new BestPriceCalculator(resources, table, playerIndex);
+ bestPriceCalculator.computePossibilities();
+ return bestPriceCalculator.bestPrice;
}
- private int bestPrice() {
- if (resources.isEmpty()) {
- return 0;
+ public static List<BoughtResources> bestSolution(Resources resources, Table table, int playerIndex) {
+ BestPriceCalculator bestPriceCalculator = new BestPriceCalculator(resources, table, playerIndex);
+ bestPriceCalculator.computePossibilities();
+ return bestPriceCalculator.bestSolution;
+ }
+
+ private void computePossibilities() {
+ if (resourcesLeftToPay.isEmpty()) {
+ updateBestSolutionIfNeeded();
+ return;
}
- int currentMinPrice = Integer.MAX_VALUE;
for (ResourceType type : ResourceType.values()) {
- if (resources.getQuantity(type) > 0) {
- int minPriceUsingOwnResource = bestPriceWithout(type);
- currentMinPrice = Math.min(currentMinPrice, minPriceUsingOwnResource);
+ if (resourcesLeftToPay.getQuantity(type) > 0) {
+ for (ResourcePool pool : pools) {
+ boolean ownResource = pool.getProvider() == null;
+ if (ownResource) {
+ resourcesLeftToPay.remove(type, 1);
+ computePossibilitiesWhenUsing(type, pool);
+ 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);
+ pricePaid += cost;
+ computePossibilitiesWhenUsing(type, pool);
+ pricePaid -= cost;
+ boughtResources.remove(boughtRes);
+ resourcesLeftToPay.add(type, 1);
+ }
}
}
- return currentMinPrice;
}
- private int bestPriceWithout(ResourceType type) {
- resources.remove(type, 1);
- int currentMinPrice = Integer.MAX_VALUE;
- for (ResourcePool pool : pools) {
- int resCostInPool = pool.getCost(type);
- for (Set<ResourceType> choice : pool.getChoices()) {
- if (choice.contains(type)) {
- Set<ResourceType> temp = EnumSet.copyOf(choice);
- choice.clear();
- currentMinPrice = Math.min(currentMinPrice, bestPrice() + resCostInPool);
- choice.addAll(temp);
- }
+ private void computePossibilitiesWhenUsing(ResourceType type, ResourcePool pool) {
+ for (Set<ResourceType> choice : pool.getChoices()) {
+ if (choice.contains(type)) {
+ Set<ResourceType> temp = EnumSet.copyOf(choice);
+ choice.clear();
+ computePossibilities();
+ choice.addAll(temp);
}
}
- resources.add(type, 1);
- return currentMinPrice;
}
+ private void updateBestSolutionIfNeeded() {
+ if (pricePaid < bestPrice) {
+ bestPrice = pricePaid;
+ bestSolution = new ArrayList<>(boughtResources);
+ }
+ }
}
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
index ec261c8c..71293190 100644
--- 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
@@ -1,11 +1,21 @@
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;
}
@@ -21,4 +31,26 @@ public class BoughtResources {
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/ResourcePool.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourcePool.java
index ad368066..75a02afc 100644
--- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourcePool.java
+++ b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourcePool.java
@@ -10,8 +10,8 @@ class ResourcePool {
private final TradingRules rules;
- ResourcePool(Production production, Provider provider, TradingRules rules) {
- this.choices = production.asChoices();
+ ResourcePool(Provider provider, TradingRules rules, Set<Set<ResourceType>> choices) {
+ this.choices = choices;
this.provider = provider;
this.rules = rules;
}
@@ -20,6 +20,10 @@ class ResourcePool {
return choices;
}
+ Provider getProvider() {
+ return provider;
+ }
+
int getCost(ResourceType type) {
if (provider == null) {
return 0;
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 04278fea..1b263760 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
@@ -13,6 +13,14 @@ public class Resources {
private final Map<ResourceType, Integer> quantities = new EnumMap<>(ResourceType.class);
+ public static Resources of(ResourceType... types) {
+ Resources resources = new Resources();
+ for (ResourceType type : types) {
+ resources.add(type, 1);
+ }
+ return resources;
+ }
+
public void add(ResourceType type, int quantity) {
quantities.merge(type, quantity, (x, y) -> x + y);
}
@@ -88,4 +96,9 @@ public class Resources {
public int hashCode() {
return Objects.hash(quantities);
}
+
+ @Override
+ public String toString() {
+ return "Resources{" + "quantities=" + quantities + '}';
+ }
}
bgstack15