diff --git a/.idea/misc.xml b/.idea/misc.xml
index c3dfb30..b573818 100644
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,6 +1,6 @@
-
+
\ No newline at end of file
diff --git a/src/ch/zhaw/catan/Bank.java b/src/ch/zhaw/catan/Bank.java
index 7adecac..52bb9eb 100644
--- a/src/ch/zhaw/catan/Bank.java
+++ b/src/ch/zhaw/catan/Bank.java
@@ -2,19 +2,39 @@ package ch.zhaw.catan;
import java.util.HashMap;
-
+/**
+ * Bank Class that stores Resources when not being owned by a player,
+ * and the needed functions to take and give resources to it.
+ *
+ * @author Leonardo Brandenberger
+ */
public class Bank {
private final HashMap resources = new HashMap<>();
+ /**
+ * Construct a Bank Object and stores Config Values in own HashMap.
+ */
public Bank() {
resources.putAll(Config.INITIAL_RESOURCE_CARDS_BANK);
-
}
+ /**
+ * Stores a desired resource in desired quantity in the bank.
+ *
+ * @param resource the resource type that gets added to the bank
+ * @param numberOfResources the quantity of resources of the chosen type get added
+ */
public void storeResourceToBank(Config.Resource resource, int numberOfResources) {
resources.put(resource, resources.get(resource) + numberOfResources);
}
+ /**
+ * Checks if a Resource is available in the quantity desired. and then deducts it from the bank inventory.
+ *
+ * @param resource the resource type that has to be deducted
+ * @param numberOfResources the quantity of the resource that gets deducted from the inventory
+ * @return true if resources available and deducted false if not enough resources are in the bank
+ */
public boolean getResourceFromBank(Config.Resource resource, int numberOfResources) {
if (resources.get(resource) >= numberOfResources) {
Integer newResourceNumber = resources.get(resource) - numberOfResources;
diff --git a/src/ch/zhaw/catan/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java
index 099a4ec..cd7ceb0 100644
--- a/src/ch/zhaw/catan/SiedlerBoard.java
+++ b/src/ch/zhaw/catan/SiedlerBoard.java
@@ -26,8 +26,8 @@ public class SiedlerBoard extends HexBoard fields = new HashMap<>();
- Config.Faction longestRoadFaction = null;
- int longestRoadLenth = 0;
+ private Config.Faction longestRoadFaction = null;
+ private int longestRoadLenth = 0;
/**
* Method to create the predefined game field from Config.
@@ -162,25 +162,10 @@ public class SiedlerBoard extends HexBoard currentRoad) {
- currentFaction = factionA;
- currentRoad = players.get(factionA);
- }
- }
- if (currentFaction != null) {
- longestRoadFaction = currentFaction;
- longestRoadLenth = currentRoad;
- }
- } else {
- for (Config.Faction faction : players.keySet()) {
- if (players.get(faction) >= 5 && players.get(faction) > longestRoadLenth) {
- longestRoadFaction = faction;
- longestRoadLenth = players.get(faction);
- }
+ for (Config.Faction factionA : players.keySet()) {
+ if (players.get(factionA) > longestRoadLenth && players.get(factionA) > 4) {
+ longestRoadFaction = factionA;
+ longestRoadLenth = players.get(factionA);
}
}
return longestRoadFaction;
@@ -203,75 +188,55 @@ public class SiedlerBoard extends HexBoard countRoad(Config.Faction faction, Point position, HashSet roads, boolean add) {
List roadsList = getAdjacentEdges(position);
+ //Checks if roads is interrupted by other players settlement
if (getCorner(position) != null && getCorner(position).getFaction() != faction) {
return roads;
}
-
+ //removes roads from roadsList which doesn't belong to the same player or which are allready added to roads.
for (Road roadsRoad : roads) {
- Iterator it3 = roadsList.iterator();
- while (it3.hasNext()) {
- Road roadsListRoad = it3.next();
+ Iterator it = roadsList.iterator();
+ while (it.hasNext()) {
+ Road roadsListRoad = it.next();
if (roadsListRoad == roadsRoad || roadsListRoad.getFaction() != faction) {
- it3.remove();
+ it.remove();
}
}
}
-
if (roadsList.size() == 1) {
roads.add(roadsList.get(0));
position = getNextPoint(roadsList.get(0), position);
roads = countRoad(faction, position, roads, false);
} else if (roadsList.size() == 2) {
- HashSet listOne = (HashSet) roads.clone();
- HashSet listTwo = (HashSet) roads.clone();
- listOne.add(roadsList.get(0));
- Point positionOne = getNextPoint(roadsList.get(0), position);
- listTwo.add(roadsList.get(1));
- Point positionTwo = getNextPoint(roadsList.get(1), position);
- listOne = countRoad(faction, positionOne, listOne, false);
- listTwo = countRoad(faction, positionTwo, listTwo, false);
+ ArrayList> possibleRoads = checkRoadsSwitch(faction,2,roadsList,position);
if (add) {
- listTwo.addAll(listOne);
- roads = listTwo;
+ possibleRoads.get(1).addAll(possibleRoads.get(0));
+ roads.addAll(possibleRoads.get(1));
} else {
HashSet tallest;
- if (listOne.size() >= listTwo.size()) {
- tallest = listOne;
+ if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) {
+ tallest = possibleRoads.get(0);
} else {
- tallest = listTwo;
+ tallest = possibleRoads.get(1);
}
roads.addAll(tallest);
}
} else if (roadsList.size() == 3) {
- HashSet listOne = (HashSet) roads.clone();
- HashSet listTwo = (HashSet) roads.clone();
- HashSet listThree = (HashSet) roads.clone();
- listOne.add(roadsList.get(0));
- Point positionOne = getNextPoint(roadsList.get(0), position);
- listTwo.add(roadsList.get(1));
- Point positionTwo = getNextPoint(roadsList.get(1), position);
- listThree.add(roadsList.get(2));
- Point positionThree = getNextPoint(roadsList.get(2), position);
- listOne = countRoad(faction, positionOne, listOne, false);
- listTwo = countRoad(faction, positionTwo, listTwo, false);
- listThree = countRoad(faction, positionThree, listThree, false);
-
+ ArrayList> possibleRoads = checkRoadsSwitch(faction,3,roadsList,position);
HashSet tallest;
HashSet secondTallest;
-
- if (listOne.size() >= listTwo.size()) {
- tallest = listOne;
- secondTallest = listTwo;
+ if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) {
+ tallest = possibleRoads.get(0);
+ secondTallest = possibleRoads.get(1);
} else {
- tallest = listTwo;
- secondTallest = listOne;
+ tallest = possibleRoads.get(1);
+ secondTallest = possibleRoads.get(0);
}
- if (listThree.size() >= secondTallest.size()) {
- secondTallest = listThree;
+ if (possibleRoads.get(2).size() >= secondTallest.size()) {
+ secondTallest = possibleRoads.get(2);
}
tallest.addAll(secondTallest);
- roads = tallest;
+ roads.addAll(tallest);
}
return roads;
}
@@ -293,4 +258,24 @@ public class SiedlerBoard extends HexBoard> checkRoadsSwitch(Config.Faction faction,int numberOfSwitches,List roadsList,Point position) {
+ ArrayList> possibleRoads = new ArrayList<>();
+ for (int i = 0;i list = new HashSet<>();
+ list.add(roadsList.get(i));
+ Point nextPosition = getNextPoint(roadsList.get(i),position);
+ list = countRoad(faction, nextPosition, list, false);
+ possibleRoads.add(list);
+ }
+ return possibleRoads;
+ }
}