Merge remote-tracking branch 'origin/main'

This commit is contained in:
Leonardo Brandenberger 2021-12-10 09:50:55 +01:00
commit 9bb4550926
4 changed files with 91 additions and 81 deletions

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_X" default="true" project-jdk-name="openjdk-17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View File

@ -26,8 +26,8 @@ public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String
* Value: Field Object * Value: Field Object
*/ */
private final HashMap<Point, Field> fields = new HashMap<>(); private final HashMap<Point, Field> fields = new HashMap<>();
Config.Faction longestRoadFaction = null; private Config.Faction longestRoadFaction = null;
int longestRoadLenth = 0; private int longestRoadLenth = 0;
/** /**
* Method to create the predefined game field from Config. * Method to create the predefined game field from Config.
@ -161,25 +161,10 @@ public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String
} }
} }
if (longestRoadFaction == null) { for (Config.Faction factionA : players.keySet()) {
Config.Faction currentFaction = null; if (players.get(factionA) > longestRoadLenth && players.get(factionA) > 4) {
int currentRoad = 4; longestRoadFaction = factionA;
for (Config.Faction factionA : players.keySet()) { longestRoadLenth = players.get(factionA);
if (players.get(factionA) > 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);
}
} }
} }
return longestRoadFaction; return longestRoadFaction;
@ -202,75 +187,55 @@ public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String
*/ */
private HashSet<Road> countRoad(Config.Faction faction, Point position, HashSet<Road> roads, boolean add) { private HashSet<Road> countRoad(Config.Faction faction, Point position, HashSet<Road> roads, boolean add) {
List<Road> roadsList = getAdjacentEdges(position); List<Road> roadsList = getAdjacentEdges(position);
//Checks if roads is interrupted by other players settlement
if (getCorner(position) != null && getCorner(position).getFaction() != faction) { if (getCorner(position) != null && getCorner(position).getFaction() != faction) {
return roads; 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) { for (Road roadsRoad : roads) {
Iterator<Road> it3 = roadsList.iterator(); Iterator<Road> it = roadsList.iterator();
while (it3.hasNext()) { while (it.hasNext()) {
Road roadsListRoad = it3.next(); Road roadsListRoad = it.next();
if (roadsListRoad == roadsRoad || roadsListRoad.getFaction() != faction) { if (roadsListRoad == roadsRoad || roadsListRoad.getFaction() != faction) {
it3.remove(); it.remove();
} }
} }
} }
if (roadsList.size() == 1) { if (roadsList.size() == 1) {
roads.add(roadsList.get(0)); roads.add(roadsList.get(0));
position = getNextPoint(roadsList.get(0), position); position = getNextPoint(roadsList.get(0), position);
roads = countRoad(faction, position, roads, false); roads = countRoad(faction, position, roads, false);
} else if (roadsList.size() == 2) { } else if (roadsList.size() == 2) {
HashSet<Road> listOne = (HashSet<Road>) roads.clone(); ArrayList<HashSet<Road>> possibleRoads = checkRoadsSwitch(faction,2,roadsList,position);
HashSet<Road> listTwo = (HashSet<Road>) 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);
if (add) { if (add) {
listTwo.addAll(listOne); possibleRoads.get(1).addAll(possibleRoads.get(0));
roads = listTwo; roads.addAll(possibleRoads.get(1));
} else { } else {
HashSet<Road> tallest; HashSet<Road> tallest;
if (listOne.size() >= listTwo.size()) { if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) {
tallest = listOne; tallest = possibleRoads.get(0);
} else { } else {
tallest = listTwo; tallest = possibleRoads.get(1);
} }
roads.addAll(tallest); roads.addAll(tallest);
} }
} else if (roadsList.size() == 3) { } else if (roadsList.size() == 3) {
HashSet<Road> listOne = (HashSet<Road>) roads.clone(); ArrayList<HashSet<Road>> possibleRoads = checkRoadsSwitch(faction,3,roadsList,position);
HashSet<Road> listTwo = (HashSet<Road>) roads.clone();
HashSet<Road> listThree = (HashSet<Road>) 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);
HashSet<Road> tallest; HashSet<Road> tallest;
HashSet<Road> secondTallest; HashSet<Road> secondTallest;
if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) {
if (listOne.size() >= listTwo.size()) { tallest = possibleRoads.get(0);
tallest = listOne; secondTallest = possibleRoads.get(1);
secondTallest = listTwo;
} else { } else {
tallest = listTwo; tallest = possibleRoads.get(1);
secondTallest = listOne; secondTallest = possibleRoads.get(0);
} }
if (listThree.size() >= secondTallest.size()) { if (possibleRoads.get(2).size() >= secondTallest.size()) {
secondTallest = listThree; secondTallest = possibleRoads.get(2);
} }
tallest.addAll(secondTallest); tallest.addAll(secondTallest);
roads = tallest; roads.addAll(tallest);
} }
return roads; return roads;
} }
@ -292,4 +257,24 @@ public class SiedlerBoard extends HexBoard<Config.Land, Settlement, Road, String
} }
return position; return position;
} }
/**
* This method checks at a roads switch, which road starting at the switch will be the longest possibility.
* @param faction the faction of the player to check on
* @param numberOfSwitches how many adjacent roads there are at the corner
* @param roadsList the list of adjacent roads
* @param position the current corner from where to count further.
* @return ArrayList of HashSet with all possible forks.
*/
private ArrayList<HashSet<Road>> checkRoadsSwitch(Config.Faction faction,int numberOfSwitches,List<Road> roadsList,Point position) {
ArrayList<HashSet<Road>> possibleRoads = new ArrayList<>();
for (int i = 0;i<numberOfSwitches;i++) {
HashSet<Road> 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;
}
} }

View File

@ -31,27 +31,47 @@ public class SiedlerGameTest {
@DisplayName("Positive test cases") @DisplayName("Positive test cases")
class PositiveTestcases { class PositiveTestcases {
/** @Nested
* To Test getLongestRoad in SiedlerBoard class LongestRoadTest {
* /**
*/ * To Test getLongestRoad in SiedlerBoard
@Test */
public void testLongestRoad() {
List<Config.Faction> factionList = Arrays.asList(Config.Faction.values()); List<Config.Faction> factionList = Arrays.asList(Config.Faction.values());
SiedlerBoard board = new SiedlerBoard(); SiedlerBoard board = new SiedlerBoard();
board.createFixGameField();
board.setEdge(new Point(6, 6), new Point(5, 7), new Road(Config.Faction.BLUE,new Point(6, 6),new Point(5, 7)));
board.setEdge(new Point(4, 6), new Point(5, 7), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(5, 7))); @BeforeEach
board.setEdge(new Point(4, 6), new Point(4, 4), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(4, 4))); public void buildLongestRoad(){
board.setEdge(new Point(4, 6), new Point(3, 7), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(3, 7))); board.createFixGameField();
board.setEdge(new Point(3, 7), new Point(3, 9), new Road(Config.Faction.BLUE,new Point(3, 7),new Point(3, 9))); board.setEdge(new Point(6, 6), new Point(5, 7), new Road(Config.Faction.BLUE, new Point(6, 6), new Point(5, 7)));
board.setEdge(new Point(3, 9), new Point(4, 10), new Road(Config.Faction.BLUE,new Point(3, 9),new Point(4, 10))); board.setEdge(new Point(4, 6), new Point(5, 7), new Road(Config.Faction.BLUE, new Point(4, 6), new Point(5, 7)));
board.setEdge(new Point(4, 10), new Point(5, 9), new Road(Config.Faction.BLUE,new Point(4, 10),new Point(5, 9))); board.setEdge(new Point(4, 6), new Point(4, 4), new Road(Config.Faction.BLUE, new Point(4, 6), new Point(4, 4)));
board.setCorner(new Point(3,7),new Settlement(Config.Faction.BLUE,new Point(3,7))); board.setEdge(new Point(4, 6), new Point(3, 7), new Road(Config.Faction.BLUE, new Point(4, 6), new Point(3, 7)));
assertEquals(Config.Faction.BLUE, board.getLongestRoadFaction(factionList)); board.setEdge(new Point(3, 7), new Point(3, 9), new Road(Config.Faction.BLUE, new Point(3, 7), new Point(3, 9)));
assertEquals(6,board.getLongestRoadLenth()); board.setEdge(new Point(3, 9), new Point(4, 10), new Road(Config.Faction.BLUE, new Point(3, 9), new Point(4, 10)));
//todo prüfen ob länge Stimmt. board.setEdge(new Point(4, 10), new Point(5, 9), new Road(Config.Faction.BLUE, new Point(4, 10), new Point(5, 9)));
board.setCorner(new Point(3, 7), new Settlement(Config.Faction.BLUE, new Point(3, 7)));
}
@Test
public void testLongestRoadSimple() {
System.out.println(board.getTextView());
assertEquals(Config.Faction.BLUE, board.getLongestRoadFaction(factionList));
assertEquals(6, board.getLongestRoadLenth());
}
@Test
public void testLongestRoadWithInterrupt() {
board.setCorner(new Point(4, 10), new Settlement(Config.Faction.RED, new Point(4, 10)));
System.out.println(board.getTextView());
assertEquals(Config.Faction.BLUE, board.getLongestRoadFaction(factionList));
assertEquals(5, board.getLongestRoadLenth());
}
} }
/** /**