Optimized task 11 scoreLink #1

Signed-off-by: Immanuel Alvaro Bhirawa <u7280427@anu.edu.au>
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-05-06 20:12:30 +10:00
parent 3d84fa1e83
commit 199a50b088

View File

@ -563,9 +563,7 @@ public class State {
public int scoreLinks(int playerID) { public int scoreLinks(int playerID) {
Coord[] playerCoords = players[playerID].getPieces(); Coord[] playerCoords = players[playerID].getPieces();
Set<Coord> playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords)); Set<Coord> playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords));
int maxScore = findLongestLinkScore(playerCoordsSet, islands); int maxScore = findLongestLinkScore(playerCoordsSet, islands);
return maxScore; return maxScore;
} }
@ -574,7 +572,6 @@ public class State {
Set<Coord> current = new HashSet<>(); Set<Coord> current = new HashSet<>();
Coord now; Coord now;
int max = scoreForLink(longest,islands); int max = scoreForLink(longest,islands);
for(Island islandIter : islands) { for(Island islandIter : islands) {
@ -608,19 +605,16 @@ public class State {
} }
public static int scoreForLink(Set<Coord> longestLink, Island[] islands) { public static int scoreForLink(Set<Coord> longestLink, Island[] islands) {
int numOfIslands = 0; Set<Island> connectedIslands = new HashSet<>();
for (Coord c : longestLink) {
outerLoop: for (Island i : islands) {
for (Island i : islands) {
for ( Coord c : longestLink ) {
if (i.containsCoord(c)) { if (i.containsCoord(c)) {
numOfIslands++; connectedIslands.add(i);
continue outerLoop; break;
} }
} }
} }
return connectedIslands.size() * 5;
return numOfIslands * 5;
} }
/** /**