diff options
author | Joffrey Bion <joffrey.bion@amadeus.com> | 2018-07-09 19:39:33 +0200 |
---|---|---|
committer | Joffrey BION <joffrey.bion@gmail.com> | 2018-07-10 01:00:08 +0200 |
commit | 80fd463ce92e5bd12215e8c3b82d5ea669447fd6 (patch) | |
tree | 372afc9cfc3763597cf954397a1b5aebe795e92b /game-engine/src/main/java/org | |
parent | Kotlin mig: moves package (diff) | |
download | seven-wonders-80fd463ce92e5bd12215e8c3b82d5ea669447fd6.tar.gz seven-wonders-80fd463ce92e5bd12215e8c3b82d5ea669447fd6.tar.bz2 seven-wonders-80fd463ce92e5bd12215e8c3b82d5ea669447fd6.zip |
Kotlin mig: resources package
Diffstat (limited to 'game-engine/src/main/java/org')
9 files changed, 0 insertions, 599 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 deleted file mode 100644 index 10ac8343..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/BestPriceCalculator.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.ArrayList; -import java.util.EnumSet; -import java.util.List; -import java.util.Set; - -import org.luxons.sevenwonders.game.api.Table; -import org.luxons.sevenwonders.game.boards.Board; - -public class BestPriceCalculator { - - private final Resources resourcesLeftToPay; - - private final List<ResourcePool> pools; - - private final ResourceTransactions boughtResources; - - private int pricePaid; - - private ResourceTransactions 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 ResourceTransactions(); - this.pricePaid = 0; - this.bestSolution = null; - this.bestPrice = Integer.MAX_VALUE; - } - - private static List<ResourcePool> createResourcePools(Table table, int playerIndex) { - Provider[] providers = Provider.values(); - List<ResourcePool> pools = new ArrayList<>(providers.length + 1); - - Board board = table.getBoard(playerIndex); - TradingRules rules = board.getTradingRules(); - // 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(provider, rules, providerBoard.getPublicProduction().asChoices()); - pools.add(pool); - } - return pools; - } - - public static int bestPrice(Resources resources, Table table, int playerIndex) { - BestPriceCalculator bestPriceCalculator = new BestPriceCalculator(resources, table, playerIndex); - bestPriceCalculator.computePossibilities(); - return bestPriceCalculator.bestPrice; - } - - 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() { - if (resourcesLeftToPay.isEmpty()) { - updateBestSolutionIfNeeded(); - return; - } - for (ResourceType type : ResourceType.values()) { - 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; - } - int cost = pool.getCost(type); - - resourcesLeftToPay.remove(type, 1); - boughtResources.add(pool.getProvider(), Resources.of(type)); - pricePaid += cost; - computePossibilitiesWhenUsing(type, pool); - pricePaid -= cost; - boughtResources.remove(pool.getProvider(), Resources.of(type)); - resourcesLeftToPay.add(type, 1); - } - } - } - } - - 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); - } - } - } - - private void updateBestSolutionIfNeeded() { - if (pricePaid < bestPrice) { - bestPrice = pricePaid; - bestSolution = new ResourceTransactions(boughtResources.toTransactions()); - } - } -} - diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Production.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Production.java deleted file mode 100644 index 7fa83e51..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Production.java +++ /dev/null @@ -1,106 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.Arrays; -import java.util.EnumSet; -import java.util.HashSet; -import java.util.Objects; -import java.util.Set; - -public class Production { - - private final Resources fixedResources = new Resources(); - - private final Set<Set<ResourceType>> alternativeResources = new HashSet<>(); - - public void addFixedResource(ResourceType type, int quantity) { - fixedResources.add(type, quantity); - } - - public void addChoice(ResourceType... options) { - EnumSet<ResourceType> optionSet = EnumSet.copyOf(Arrays.asList(options)); - alternativeResources.add(optionSet); - } - - public void addAll(Resources resources) { - fixedResources.addAll(resources); - } - - public void addAll(Production production) { - fixedResources.addAll(production.getFixedResources()); - alternativeResources.addAll(production.getAlternativeResources()); - } - - public Resources getFixedResources() { - return fixedResources; - } - - public Set<Set<ResourceType>> getAlternativeResources() { - return alternativeResources; - } - - Set<Set<ResourceType>> asChoices() { - Set<Set<ResourceType>> result = new HashSet<>(fixedResources.size() + alternativeResources.size()); - fixedResources.asList().stream().map(EnumSet::of).forEach(result::add); - result.addAll(alternativeResources); - return result; - } - - public boolean contains(Resources resources) { - if (fixedResources.contains(resources)) { - return true; - } - Resources remaining = resources.minus(fixedResources); - return containedInAlternatives(remaining); - } - - private boolean containedInAlternatives(Resources resources) { - return containedInAlternatives(resources, alternativeResources); - } - - private static boolean containedInAlternatives(Resources resources, Set<Set<ResourceType>> alternatives) { - if (resources.isEmpty()) { - return true; - } - for (ResourceType type : ResourceType.values()) { - if (resources.getQuantity(type) <= 0) { - continue; - } - Set<ResourceType> candidate = findFirstAlternativeContaining(alternatives, type); - if (candidate == null) { - return false; // no alternative produces the resource of this entry - } - resources.remove(type, 1); - alternatives.remove(candidate); - boolean remainingAreContainedToo = containedInAlternatives(resources, alternatives); - resources.add(type, 1); - alternatives.add(candidate); - if (remainingAreContainedToo) { - return true; - } - } - return false; - } - - private static Set<ResourceType> findFirstAlternativeContaining(Set<Set<ResourceType>> alternatives, - ResourceType type) { - return alternatives.stream().filter(a -> a.contains(type)).findAny().orElse(null); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Production that = (Production) o; - return Objects.equals(fixedResources, that.fixedResources) && Objects.equals(alternativeResources, - that.alternativeResources); - } - - @Override - public int hashCode() { - return Objects.hash(fixedResources, alternativeResources); - } -} diff --git a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java deleted file mode 100644 index 9c4aa3f9..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Provider.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import org.luxons.sevenwonders.game.boards.RelativeBoardPosition; - -public enum Provider { - LEFT_PLAYER(RelativeBoardPosition.LEFT), - RIGHT_PLAYER(RelativeBoardPosition.RIGHT); - - private final RelativeBoardPosition boardPosition; - - Provider(RelativeBoardPosition boardPosition) { - this.boardPosition = boardPosition; - } - - public RelativeBoardPosition getBoardPosition() { - return boardPosition; - } -} 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 deleted file mode 100644 index 75a02afc..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourcePool.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.Set; - -class ResourcePool { - - private final Set<Set<ResourceType>> choices; - - private final Provider provider; - - private final TradingRules rules; - - ResourcePool(Provider provider, TradingRules rules, Set<Set<ResourceType>> choices) { - this.choices = choices; - this.provider = provider; - this.rules = rules; - } - - Set<Set<ResourceType>> getChoices() { - return choices; - } - - Provider getProvider() { - return provider; - } - - int getCost(ResourceType type) { - if (provider == null) { - return 0; - } - return rules.getCost(type, provider); - } -} 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 deleted file mode 100644 index 3cb8eea1..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransaction.java +++ /dev/null @@ -1,53 +0,0 @@ -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 deleted file mode 100644 index a8fdc6c7..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceTransactions.java +++ /dev/null @@ -1,71 +0,0 @@ -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/ResourceType.java b/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java deleted file mode 100644 index 644437df..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java +++ /dev/null @@ -1,48 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.HashMap; -import java.util.Map; - -public enum ResourceType { - WOOD('W'), - STONE('S'), - ORE('O'), - CLAY('C'), - GLASS('G'), - PAPYRUS('P'), - LOOM('L'); - - private static final Map<Character, ResourceType> typesPerSymbol = new HashMap<>(7); - - static { - for (ResourceType type : values()) { - typesPerSymbol.put(type.symbol, type); - } - } - - private final Character symbol; - - ResourceType(Character symbol) { - this.symbol = symbol; - } - - public static ResourceType fromSymbol(String symbol) { - if (symbol.length() != 1) { - throw new IllegalArgumentException("The given symbol must be a valid single-char resource type, got " - + symbol); - } - return fromSymbol(symbol.charAt(0)); - } - - public static ResourceType fromSymbol(Character symbol) { - ResourceType type = typesPerSymbol.get(symbol); - if (type == null) { - throw new IllegalArgumentException(String.format("Unknown resource type symbol '%s'", symbol)); - } - return type; - } - - public Character getSymbol() { - return symbol; - } -} 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 deleted file mode 100644 index d354f121..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java +++ /dev/null @@ -1,112 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.EnumMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.NoSuchElementException; -import java.util.Objects; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -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); - } - - public void remove(ResourceType type, int quantity) { - if (getQuantity(type) < quantity) { - throw new NoSuchElementException(String.format("Can't remove %d resources of type %s", quantity, type)); - } - quantities.computeIfPresent(type, (t, oldQty) -> oldQty - quantity); - } - - public Resources addAll(Resources resources) { - resources.quantities.forEach(this::add); - return this; - } - - public int getQuantity(ResourceType type) { - return quantities.getOrDefault(type, 0); - } - - public List<ResourceType> asList() { - return quantities.entrySet() - .stream() - .flatMap(e -> Stream.generate(e::getKey).limit(e.getValue())) - .collect(Collectors.toList()); - } - - public boolean contains(Resources resources) { - return resources.quantities.entrySet().stream().allMatch(this::hasAtLeast); - } - - private boolean hasAtLeast(Entry<ResourceType, Integer> quantity) { - 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. - * - * @param resources - * the resources to subtract from these resources - * - * @return a new {@link Resources} object containing these resources minus the given resources. - */ - public Resources minus(Resources resources) { - Resources diff = new Resources(); - quantities.forEach((type, count) -> { - int remainder = count - resources.getQuantity(type); - diff.quantities.put(type, Math.max(0, remainder)); - }); - return diff; - } - - public boolean isEmpty() { - return size() == 0; - } - - public int size() { - return quantities.values().stream().reduce(0, Integer::sum); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - Resources resources = (Resources) o; - return Objects.equals(quantities, resources.quantities); - } - - @Override - public int hashCode() { - return Objects.hash(quantities); - } - - @Override - public String toString() { - return "Resources{" + "quantities=" + quantities + '}'; - } -} 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 deleted file mode 100644 index ffbce8ab..00000000 --- a/game-engine/src/main/java/org/luxons/sevenwonders/game/resources/TradingRules.java +++ /dev/null @@ -1,46 +0,0 @@ -package org.luxons.sevenwonders.game.resources; - -import java.util.EnumMap; -import java.util.Map; - -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 Map<ResourceType, Map<Provider, Integer>> getCosts() { - return costs; - } - - 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(ResourceTransactions transactions) { - return transactions.toTransactions().stream().mapToInt(this::computeCost).sum(); - } - - 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); - total += getCost(type, provider) * count; - } - return total; - } -} |