Added more documentation and optimization for scoreLink

Signed-off-by: Immanuel Alvaro Bhirawa <u7280427@anu.edu.au>
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-05-06 21:10:48 +10:00
parent 90674de713
commit 73293edb43

View File

@ -567,31 +567,44 @@ public class State {
return maxScore; return maxScore;
} }
/**
* Combines both the helper methods `DFSRecurssionLink` and `findScoreForLink` to find the link set that has the
* highest score from a set all Player's pieces coordinates
* @param allCoords all coordinates of a Player's pieces
* @param islands the set of islands from the generated world
* @return the highest score from a player's linked pieces
*/
public static int findLongestLinkScore ( Set<Coord> allCoords, Island[] islands) { public static int findLongestLinkScore ( Set<Coord> allCoords, Island[] islands) {
Set<Coord> longest = new HashSet<>(); Set<Coord> longestPath = new HashSet<>();
Set<Coord> current = new HashSet<>(); Set<Coord> currentPath = new HashSet<>();
Coord now; Coord startingPoint;
int max = helperScoreForLink(longest,islands); int maxScore = findScoreForLink(longestPath,islands);
for(Island islandIter : islands) { for(Island i : islands) {
for ( Coord coords : allCoords) { for ( Coord c : allCoords) {
if(islandIter.containsCoord(coords)) { if(i.containsCoord(c)) {
now = coords;
current.add(coords); startingPoint = c; // set starting Point
recursionLink(allCoords, current, longest, now);
// DFS Algo starts
currentPath.add(startingPoint);
DFSRecursionLink(allCoords, currentPath, longestPath, startingPoint);
// if the score is bigger, update the Set // if the score is bigger, update the Set
if(helperScoreForLink(current, islands) > max) { if(findScoreForLink(currentPath, islands) > maxScore) {
longest.clear(); longestPath.clear();
longest.addAll(current); longestPath.addAll(currentPath);
max = helperScoreForLink(longest, islands);
maxScore = findScoreForLink(longestPath, islands);
} }
current = new HashSet<>();
// clear the currentPath as well
currentPath = new HashSet<>();
} }
} }
} }
return max; return maxScore;
} }
/** /**
@ -603,20 +616,21 @@ public class State {
* @param longestPath a set to store the longest path * @param longestPath a set to store the longest path
* @param startingPoint the starting point of each step of DFS * @param startingPoint the starting point of each step of DFS
*/ */
public static void recursionLink(Set<Coord> allCoords, Set<Coord> currentPath, Set<Coord> longestPath, Coord startingPoint) { public static void DFSRecursionLink(Set<Coord> allCoords, Set<Coord> currentPath, Set<Coord> longestPath, Coord startingPoint) {
// Add the staring point to the `history` of the path that has been taken by the algorithm // Add the staring point to the `history` of the path that has been taken by the algorithm
currentPath.add(startingPoint); currentPath.add(startingPoint);
for(Coord c : allCoords) { for(Coord c : allCoords) {
//if (currentPath.size() > longestPath.size() ) longestPath = currentPath;
// if (currentPath.size() > longestPath.size() ) longestPath = currentPath;
// if the startingPoint of the step is adjacent with the coords from allCoords and currentPath // if the startingPoint of the step is adjacent with the coords from allCoords and currentPath
// i.e. the `history tracker` of the path so far does not have `c` coords from allCoords // i.e. the `history tracker` of the path so far does not have `c` coords from allCoords
if( startingPoint.isAdjacentDiagonal(c) && !currentPath.contains(c) ) { if( startingPoint.isAdjacentDiagonal(c) && !currentPath.contains(c) ) {
// if the currentPath is bigger than the longestPath, update the longest Path // if the currentPath is bigger than the longestPath, update the longest Path
if (currentPath.size() > longestPath.size() ) longestPath = currentPath; if (currentPath.size() > longestPath.size() ) longestPath = currentPath;
recursionLink(allCoords, currentPath, longestPath, c); DFSRecursionLink(allCoords, currentPath, longestPath, c);
} }
} }
} }
@ -628,7 +642,7 @@ public class State {
* @param islands the islands within the world * @param islands the islands within the world
* @return the score of the link set * @return the score of the link set
*/ */
public static int helperScoreForLink(Set<Coord> longestLink, Island[] islands) { public static int findScoreForLink(Set<Coord> longestLink, Island[] islands) {
Set<Island> connectedIslands = new HashSet<>(); // Container for connected Islands within the longest Link set Set<Island> connectedIslands = new HashSet<>(); // Container for connected Islands within the longest Link set
for (Coord c : longestLink) { for (Coord c : longestLink) {