From bf512128f78bcbb1b10ab8692f13229a152a8f50 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 10 Dec 2021 09:12:16 +0100 Subject: [PATCH 1/4] update testLongestRoad --- test/ch/zhaw/catan/SiedlerGameTest.java | 56 +++++++++++++++++-------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/test/ch/zhaw/catan/SiedlerGameTest.java b/test/ch/zhaw/catan/SiedlerGameTest.java index 18ab609..b9f2606 100644 --- a/test/ch/zhaw/catan/SiedlerGameTest.java +++ b/test/ch/zhaw/catan/SiedlerGameTest.java @@ -31,27 +31,47 @@ public class SiedlerGameTest { @DisplayName("Positive test cases") class PositiveTestcases { - /** - * To Test getLongestRoad in SiedlerBoard - * - */ - @Test - public void testLongestRoad() { + @Nested + class LongestRoadTest { + /** + * To Test getLongestRoad in SiedlerBoard + */ + List factionList = Arrays.asList(Config.Faction.values()); 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))); - board.setEdge(new Point(4, 6), new Point(4, 4), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(4, 4))); - board.setEdge(new Point(4, 6), new Point(3, 7), new Road(Config.Faction.BLUE,new Point(4, 6),new Point(3, 7))); - 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(3, 9), new Point(4, 10), new Road(Config.Faction.BLUE,new Point(3, 9),new Point(4, 10))); - 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))); - assertEquals(Config.Faction.BLUE, board.getLongestRoadFaction(factionList)); - assertEquals(6,board.getLongestRoadLenth()); - //todo prüfen ob länge Stimmt. + + + @BeforeEach + public void buildLongestRoad(){ + 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))); + board.setEdge(new Point(4, 6), new Point(4, 4), new Road(Config.Faction.BLUE, new Point(4, 6), new Point(4, 4))); + board.setEdge(new Point(4, 6), new Point(3, 7), new Road(Config.Faction.BLUE, new Point(4, 6), new Point(3, 7))); + 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(3, 9), new Point(4, 10), new Road(Config.Faction.BLUE, new Point(3, 9), new Point(4, 10))); + 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()); + } } /** From 9d0468ec6ec63144c8ffdd6036c8f90b82bcbc61 Mon Sep 17 00:00:00 2001 From: schrom01 Date: Fri, 10 Dec 2021 09:41:11 +0100 Subject: [PATCH 2/4] update codeStyleConfig --- .idea/codeStyles/codeStyleConfig.xml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .idea/codeStyles/codeStyleConfig.xml diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file From 6fa5764e14196a15af0031f167b62946c40134dc Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 10 Dec 2021 09:41:58 +0100 Subject: [PATCH 3/4] refactored longestRoad in SiedlerBoard --- .idea/misc.xml | 2 +- src/ch/zhaw/catan/SiedlerBoard.java | 109 ++++++++++++---------------- 2 files changed, 48 insertions(+), 63 deletions(-) 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/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java index 6a4cbb7..fa72d2e 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. @@ -161,25 +161,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; @@ -202,75 +187,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; } @@ -292,4 +257,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; + } } From 8e307e0617498eaa1d2a064225972ff3f69a90bc Mon Sep 17 00:00:00 2001 From: Andrin Fassbind Date: Fri, 10 Dec 2021 09:50:23 +0100 Subject: [PATCH 4/4] removed warning in SiedlerBoard --- src/ch/zhaw/catan/SiedlerBoard.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ch/zhaw/catan/SiedlerBoard.java b/src/ch/zhaw/catan/SiedlerBoard.java index fa72d2e..4e91ee6 100644 --- a/src/ch/zhaw/catan/SiedlerBoard.java +++ b/src/ch/zhaw/catan/SiedlerBoard.java @@ -206,7 +206,7 @@ public class SiedlerBoard extends HexBoard possibleRoads = checkRoadsSwitch(faction,2,roadsList,position); + ArrayList> possibleRoads = checkRoadsSwitch(faction,2,roadsList,position); if (add) { possibleRoads.get(1).addAll(possibleRoads.get(0)); roads.addAll(possibleRoads.get(1)); @@ -221,7 +221,7 @@ public class SiedlerBoard extends HexBoard possibleRoads = checkRoadsSwitch(faction,3,roadsList,position); + ArrayList> possibleRoads = checkRoadsSwitch(faction,3,roadsList,position); HashSet tallest; HashSet secondTallest; if (possibleRoads.get(0).size() >= possibleRoads.get(1).size()) { @@ -266,8 +266,8 @@ public class SiedlerBoard extends HexBoard checkRoadsSwitch(Config.Faction faction,int numberOfSwitches,List roadsList,Point position) { - ArrayList possibleRoads = new ArrayList<>(); + private ArrayList> 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));