From 199a50b088186596cabab15cfa79b34dff8aba69 Mon Sep 17 00:00:00 2001 From: Immanuel Alvaro Bhirawa Date: Sat, 6 May 2023 20:12:30 +1000 Subject: [PATCH] Optimized task 11 scoreLink #1 Signed-off-by: Immanuel Alvaro Bhirawa --- src/comp1110/ass2/State.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/comp1110/ass2/State.java b/src/comp1110/ass2/State.java index bc7eeed..fe25652 100644 --- a/src/comp1110/ass2/State.java +++ b/src/comp1110/ass2/State.java @@ -563,9 +563,7 @@ public class State { public int scoreLinks(int playerID) { Coord[] playerCoords = players[playerID].getPieces(); Set playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords)); - int maxScore = findLongestLinkScore(playerCoordsSet, islands); - return maxScore; } @@ -574,7 +572,6 @@ public class State { Set current = new HashSet<>(); Coord now; - int max = scoreForLink(longest,islands); for(Island islandIter : islands) { @@ -608,19 +605,16 @@ public class State { } public static int scoreForLink(Set longestLink, Island[] islands) { - int numOfIslands = 0; - - outerLoop: - for (Island i : islands) { - for ( Coord c : longestLink ) { + Set connectedIslands = new HashSet<>(); + for (Coord c : longestLink) { + for (Island i : islands) { if (i.containsCoord(c)) { - numOfIslands++; - continue outerLoop; + connectedIslands.add(i); + break; } } } - - return numOfIslands * 5; + return connectedIslands.size() * 5; } /**