summaryrefslogtreecommitdiff
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
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
-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
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java221
-rw-r--r--src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java387
5 files changed, 752 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;
+ }
+}
diff --git a/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java b/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java
new file mode 100644
index 00000000..58b82dc1
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/game/resources/ProductionTest.java
@@ -0,0 +1,221 @@
+package org.luxons.sevenwonders.game.resources;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ProductionTest {
+
+ private Resources emptyResources;
+
+ private Resources resources1Wood;
+
+ private Resources resources1Stone;
+
+ private Resources resources1Stone1Wood;
+
+ private Resources resources2Stones;
+
+ private Resources resources2Stones3Clay;
+
+ @Before
+ public void init() {
+ emptyResources = new Resources();
+
+ resources1Wood = new Resources();
+ resources1Wood.add(ResourceType.WOOD, 1);
+
+ resources1Stone = new Resources();
+ resources1Stone.add(ResourceType.STONE, 1);
+
+ resources1Stone1Wood = new Resources();
+ resources1Stone1Wood.add(ResourceType.STONE, 1);
+ resources1Stone1Wood.add(ResourceType.WOOD, 1);
+
+ resources2Stones = new Resources();
+ resources2Stones.add(ResourceType.STONE, 2);
+
+ resources2Stones3Clay = new Resources();
+ resources2Stones3Clay.add(ResourceType.STONE, 2);
+ resources2Stones3Clay.add(ResourceType.CLAY, 3);
+ }
+
+ @Test
+ public void contains_newProductionContainsEmpty() throws Exception {
+ Production production = new Production();
+ assertTrue(production.contains(emptyResources));
+ }
+
+ @Test
+ public void contains_singleFixedResource_noneAtAll() throws Exception {
+ Production production = new Production();
+ assertFalse(production.contains(resources2Stones));
+ }
+
+ @Test
+ public void contains_singleFixedResource_notEnough() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 1);
+ assertFalse(production.contains(resources2Stones));
+ }
+
+ @Test
+ public void contains_singleFixedResource_justEnough() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 2);
+ assertTrue(production.contains(resources2Stones));
+ }
+
+ @Test
+ public void contains_singleFixedResource_moreThanEnough() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 3);
+ assertTrue(production.contains(resources2Stones));
+ }
+
+ @Test
+ public void contains_singleFixedResource_moreThanEnough_amongOthers() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 3);
+ production.addFixedResource(ResourceType.CLAY, 2);
+ assertTrue(production.contains(resources2Stones));
+ }
+
+ @Test
+ public void contains_multipleFixedResources_notEnoughOfOne() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 3);
+ production.addFixedResource(ResourceType.CLAY, 1);
+ assertFalse(production.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void contains_multipleFixedResources_notEnoughOfBoth() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 1);
+ production.addFixedResource(ResourceType.CLAY, 1);
+ assertFalse(production.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void contains_multipleFixedResources_moreThanEnough() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.STONE, 3);
+ production.addFixedResource(ResourceType.CLAY, 5);
+ assertTrue(production.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void contains_singleChoice_containsEmpty() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.CLAY);
+ assertTrue(production.contains(emptyResources));
+ }
+
+ @Test
+ public void contains_singleChoice_enough() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ assertTrue(production.contains(resources1Wood));
+ assertTrue(production.contains(resources1Stone));
+ }
+
+ @Test
+ public void contains_multipleChoices_notBoth() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.CLAY);
+ production.addChoice(ResourceType.STONE, ResourceType.CLAY);
+ production.addChoice(ResourceType.STONE, ResourceType.CLAY);
+ assertFalse(production.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void contains_multipleChoices_enough() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.ORE);
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+
+ @Test
+ public void contains_multipleChoices_enoughReverseOrder() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ production.addChoice(ResourceType.STONE, ResourceType.ORE);
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+
+ @Test
+ public void contains_multipleChoices_moreThanEnough() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.LINEN, ResourceType.GLASS, ResourceType.PAPYRUS);
+ production.addChoice(ResourceType.STONE, ResourceType.ORE);
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+
+ @Test
+ public void contains_mixedFixedAndChoice_enough() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.WOOD, 1);
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+
+ @Test
+ public void addAll_empty() throws Exception {
+ Production production = new Production();
+ production.addAll(emptyResources);
+ assertTrue(production.contains(emptyResources));
+ }
+
+ @Test
+ public void addAll_singleResource() throws Exception {
+ Production production = new Production();
+ production.addAll(resources1Stone);
+ assertTrue(production.contains(resources1Stone));
+ }
+
+ @Test
+ public void addAll_multipleResources() throws Exception {
+ Production production = new Production();
+ production.addAll(resources2Stones3Clay);
+ assertTrue(production.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void addAll_production_multipleFixedResources() throws Exception {
+ Production production = new Production();
+ production.addAll(resources2Stones3Clay);
+
+ Production production2 = new Production();
+ production2.addAll(production);
+
+ assertTrue(production2.contains(resources2Stones3Clay));
+ }
+
+ @Test
+ public void addAll_production_multipleChoices() throws Exception {
+ Production production = new Production();
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+ production.addChoice(ResourceType.STONE, ResourceType.ORE);
+
+ Production production2 = new Production();
+ production2.addAll(production);
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+
+ @Test
+ public void addAll_production_mixedFixedResourcesAndChoices() throws Exception {
+ Production production = new Production();
+ production.addFixedResource(ResourceType.WOOD, 1);
+ production.addChoice(ResourceType.STONE, ResourceType.WOOD);
+
+ Production production2 = new Production();
+ production2.addAll(production);
+
+ assertTrue(production.contains(resources1Stone1Wood));
+ }
+} \ No newline at end of file
diff --git a/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java b/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java
new file mode 100644
index 00000000..efcea74e
--- /dev/null
+++ b/src/test/java/org/luxons/sevenwonders/game/resources/ResourcesTest.java
@@ -0,0 +1,387 @@
+package org.luxons.sevenwonders.game.resources;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class ResourcesTest {
+
+ @Test
+ public void init_shouldBeEmpty() {
+ Resources resources = new Resources();
+ for (ResourceType resourceType : ResourceType.values()) {
+ assertEquals(0, resources.getQuantity(resourceType));
+ }
+ }
+
+ @Test
+ public void add_zero() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.CLAY, 0);
+ assertEquals(0, resources.getQuantity(ResourceType.CLAY));
+ }
+
+ @Test
+ public void add_simple() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.WOOD, 3);
+ assertEquals(3, resources.getQuantity(ResourceType.WOOD));
+ }
+
+ @Test
+ public void add_multipleCallsStacked() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.ORE, 3);
+ resources.add(ResourceType.ORE, 2);
+ assertEquals(5, resources.getQuantity(ResourceType.ORE));
+ }
+
+ @Test
+ public void add_interlaced() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.GLASS, 3);
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.WOOD, 4);
+ resources.add(ResourceType.GLASS, 2);
+ assertEquals(5, resources.getQuantity(ResourceType.GLASS));
+ }
+
+ @Test
+ public void addAll_empty() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources emptyResources = new Resources();
+
+ resources.addAll(emptyResources);
+ assertEquals(1, resources.getQuantity(ResourceType.STONE));
+ assertEquals(3, resources.getQuantity(ResourceType.CLAY));
+ assertEquals(0, resources.getQuantity(ResourceType.ORE));
+ assertEquals(0, resources.getQuantity(ResourceType.GLASS));
+ assertEquals(0, resources.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void addAll_zeros() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources emptyResources = new Resources();
+ emptyResources.add(ResourceType.STONE, 0);
+ emptyResources.add(ResourceType.CLAY, 0);
+
+ resources.addAll(emptyResources);
+ assertEquals(1, resources.getQuantity(ResourceType.STONE));
+ assertEquals(3, resources.getQuantity(ResourceType.CLAY));
+ assertEquals(0, resources.getQuantity(ResourceType.ORE));
+ assertEquals(0, resources.getQuantity(ResourceType.GLASS));
+ assertEquals(0, resources.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void addAll_same() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources resources2 = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 6);
+
+ resources.addAll(resources2);
+ assertEquals(3, resources.getQuantity(ResourceType.STONE));
+ assertEquals(9, resources.getQuantity(ResourceType.CLAY));
+ assertEquals(0, resources.getQuantity(ResourceType.ORE));
+ assertEquals(0, resources.getQuantity(ResourceType.GLASS));
+ assertEquals(0, resources.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void addAll_overlap() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources resources2 = new Resources();
+ resources.add(ResourceType.CLAY, 6);
+ resources.add(ResourceType.ORE, 4);
+
+ resources.addAll(resources2);
+ assertEquals(1, resources.getQuantity(ResourceType.STONE));
+ assertEquals(9, resources.getQuantity(ResourceType.CLAY));
+ assertEquals(4, resources.getQuantity(ResourceType.ORE));
+ assertEquals(0, resources.getQuantity(ResourceType.GLASS));
+ assertEquals(0, resources.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void contains_emptyContainsEmpty() {
+ Resources emptyResources = new Resources();
+ Resources emptyResources2 = new Resources();
+ assertTrue(emptyResources.contains(emptyResources2));
+ }
+
+ @Test
+ public void contains_singleTypeContainsEmpty() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+
+ Resources emptyResources = new Resources();
+
+ assertTrue(resources.contains(emptyResources));
+ }
+
+ @Test
+ public void contains_multipleTypesContainsEmpty() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources emptyResources = new Resources();
+
+ assertTrue(resources.contains(emptyResources));
+ }
+
+ @Test
+ public void contains_self() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ assertTrue(resources.contains(resources));
+ }
+
+ @Test
+ public void contains_allOfEachType() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.STONE, 1);
+ resources2.add(ResourceType.CLAY, 3);
+
+ assertTrue(resources.contains(resources2));
+ }
+
+ @Test
+ public void contains_someOfEachType() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.STONE, 1);
+ resources2.add(ResourceType.CLAY, 3);
+
+ assertTrue(resources.contains(resources2));
+ }
+
+ @Test
+ public void contains_someOfSomeTypes() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.CLAY, 3);
+
+ assertTrue(resources.contains(resources2));
+ }
+
+ @Test
+ public void contains_allOfSomeTypes() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.CLAY, 4);
+
+ assertTrue(resources.contains(resources2));
+ }
+
+ @Test
+ public void minus_empty() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources emptyResources = new Resources();
+
+ Resources diff = resources.minus(emptyResources);
+ assertEquals(1, diff.getQuantity(ResourceType.STONE));
+ assertEquals(3, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_self() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources diff = resources.minus(resources);
+ assertEquals(0, diff.getQuantity(ResourceType.STONE));
+ assertEquals(0, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_allOfEachType() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 1);
+ resources.add(ResourceType.CLAY, 3);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.STONE, 1);
+ resources2.add(ResourceType.CLAY, 3);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(0, diff.getQuantity(ResourceType.STONE));
+ assertEquals(0, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_someOfEachType() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.STONE, 1);
+ resources2.add(ResourceType.CLAY, 3);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(1, diff.getQuantity(ResourceType.STONE));
+ assertEquals(1, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_someOfSomeTypes() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.CLAY, 3);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(2, diff.getQuantity(ResourceType.STONE));
+ assertEquals(1, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_allOfSomeTypes() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.STONE, 2);
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.CLAY, 4);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(2, diff.getQuantity(ResourceType.STONE));
+ assertEquals(0, diff.getQuantity(ResourceType.CLAY));
+ assertEquals(0, diff.getQuantity(ResourceType.ORE));
+ assertEquals(0, diff.getQuantity(ResourceType.GLASS));
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_tooMuchOfExistingType() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.CLAY, 4);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.CLAY, 5);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(0, diff.getQuantity(ResourceType.CLAY));
+ }
+
+ @Test
+ public void minus_someOfAnAbsentType() {
+ Resources resources = new Resources();
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.LINEN, 5);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void minus_someOfATypeWithZero() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.LINEN, 0);
+
+ Resources resources2 = new Resources();
+ resources2.add(ResourceType.LINEN, 5);
+
+ Resources diff = resources.minus(resources2);
+ assertEquals(0, diff.getQuantity(ResourceType.LINEN));
+ }
+
+ @Test
+ public void isEmpty_noElement() {
+ Resources resources = new Resources();
+ assertTrue(resources.isEmpty());
+ }
+
+ @Test
+ public void isEmpty_singleZeroElement() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.LINEN, 0);
+ assertTrue(resources.isEmpty());
+ }
+
+ @Test
+ public void isEmpty_multipleZeroElements() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.WOOD, 0);
+ resources.add(ResourceType.ORE, 0);
+ resources.add(ResourceType.LINEN, 0);
+ assertTrue(resources.isEmpty());
+ }
+
+ @Test
+ public void isEmpty_singleElementMoreThanZero() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.LINEN, 3);
+ assertFalse(resources.isEmpty());
+ }
+
+ @Test
+ public void isEmpty_mixedZeroAndNonZeroElements() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.WOOD, 0);
+ resources.add(ResourceType.LINEN, 3);
+ assertFalse(resources.isEmpty());
+ }
+
+ @Test
+ public void isEmpty_mixedZeroAndNonZeroElements_reverseOrder() {
+ Resources resources = new Resources();
+ resources.add(ResourceType.ORE, 3);
+ resources.add(ResourceType.PAPYRUS, 0);
+ assertFalse(resources.isEmpty());
+ }
+} \ No newline at end of file
bgstack15