summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2016-12-07 03:53:36 +0100
committerJoffrey BION <joffrey.bion@gmail.com>2016-12-08 02:09:42 +0100
commit086c18d3ac0c57e3641c5c1c655d6c55b2d6e23b (patch)
tree14a378456a932faa58b72da3da5d42bf3a21dd99 /src/main
parentAdd game card and wonders data (diff)
downloadseven-wonders-086c18d3ac0c57e3641c5c1c655d6c55b2d6e23b.tar.gz
seven-wonders-086c18d3ac0c57e3641c5c1c655d6c55b2d6e23b.tar.bz2
seven-wonders-086c18d3ac0c57e3641c5c1c655d6c55b2d6e23b.zip
First draft of resource production elements
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/resources/Production.java76
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java21
-rw-r--r--src/main/java/org/luxons/sevenwonders/game/resources/Resources.java47
3 files changed, 144 insertions, 0 deletions
diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Production.java b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java
new file mode 100644
index 00000000..5463bea1
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/resources/Production.java
@@ -0,0 +1,76 @@
+package org.luxons.sevenwonders.game.resources;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Map.Entry;
+import java.util.Set;
+
+public class Production {
+
+ private final Resources fixedResources = new Resources();
+
+ private final List<Set<ResourceType>> alternativeResources = new ArrayList<>();
+
+ public void addFixedResource(ResourceType type, int quantity) {
+ fixedResources.add(type, quantity);
+ }
+
+ public void addChoice(ResourceType... options) {
+ EnumSet<ResourceType> optionSet = EnumSet.noneOf(ResourceType.class);
+ optionSet.addAll(Arrays.asList(options));
+ alternativeResources.add(optionSet);
+ }
+
+ void addAll(Resources resources) {
+ fixedResources.addAll(resources);
+ }
+
+ public void addAll(Production production) {
+ fixedResources.addAll(production.fixedResources);
+ alternativeResources.addAll(production.alternativeResources);
+ }
+
+ 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, List<Set<ResourceType>> alternatives) {
+ if (resources.isEmpty()) {
+ return true;
+ }
+ for (Entry<ResourceType, Integer> entry : resources.getQuantities().entrySet()) {
+ ResourceType type = entry.getKey();
+ int count = entry.getValue();
+ if (count <= 0) {
+ continue;
+ }
+ Set<ResourceType> candidate = findFirstAlternativeContaining(alternatives, type);
+ if (candidate == null) {
+ return false; // no alternative produces the resource of this entry
+ }
+ entry.setValue(count - 1);
+ alternatives.remove(candidate);
+ boolean remainingAreContainedToo = containedInAlternatives(resources, alternatives);
+ entry.setValue(count);
+ alternatives.add(candidate);
+ if (remainingAreContainedToo) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private static Set<ResourceType> findFirstAlternativeContaining(List<Set<ResourceType>> alternatives, ResourceType type) {
+ return alternatives.stream().filter(a -> a.contains(type)).findAny().orElse(null);
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java b/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java
new file mode 100644
index 00000000..5e74254d
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/resources/ResourceType.java
@@ -0,0 +1,21 @@
+package org.luxons.sevenwonders.game.resources;
+
+public enum ResourceType {
+ WOOD("W"),
+ STONE("S"),
+ ORE("O"),
+ CLAY("C"),
+ GLASS("G"),
+ PAPYRUS("P"),
+ LOOM("L");
+
+ private final String symbol;
+
+ ResourceType(String symbol) {
+ this.symbol = symbol;
+ }
+
+ public String getSymbol() {
+ return symbol;
+ }
+}
diff --git a/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java b/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
new file mode 100644
index 00000000..5f7e2606
--- /dev/null
+++ b/src/main/java/org/luxons/sevenwonders/game/resources/Resources.java
@@ -0,0 +1,47 @@
+package org.luxons.sevenwonders.game.resources;
+
+import java.util.EnumMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class Resources {
+
+ private final Map<ResourceType, Integer> quantities = new EnumMap<>(ResourceType.class);
+
+ public void add(ResourceType type, int quantity) {
+ quantities.merge(type, quantity, (x, y) -> x + y);
+ }
+
+ public void addAll(Resources resources) {
+ resources.quantities.forEach(this::add);
+ }
+
+ public int getQuantity(ResourceType type) {
+ return quantities.getOrDefault(type, 0);
+ }
+
+ public Map<ResourceType, Integer> getQuantities() {
+ return quantities;
+ }
+
+ 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 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 quantities.values().stream().reduce(0, Integer::sum) == 0;
+ }
+}
bgstack15