Added documentation and Optimized task 11 scoreLink #2

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

View File

@ -572,7 +572,7 @@ public class State {
Set<Coord> current = new HashSet<>(); Set<Coord> current = new HashSet<>();
Coord now; Coord now;
int max = scoreForLink(longest,islands); int max = helperScoreForLink(longest,islands);
for(Island islandIter : islands) { for(Island islandIter : islands) {
for ( Coord coords : allCoords) { for ( Coord coords : allCoords) {
@ -582,10 +582,10 @@ public class State {
recursionLink(allCoords, current, longest, now); recursionLink(allCoords, current, longest, now);
// if the score is bigger, update the Set // if the score is bigger, update the Set
if(scoreForLink(current, islands) > max) { if(helperScoreForLink(current, islands) > max) {
longest.clear(); longest.clear();
longest.addAll(current); longest.addAll(current);
max = scoreForLink(longest, islands); max = helperScoreForLink(longest, islands);
} }
current = new HashSet<>(); current = new HashSet<>();
} }
@ -594,18 +594,43 @@ public class State {
return max; return max;
} }
public static void recursionLink(Set<Coord> allCoords, Set<Coord> current, Set<Coord> longest, Coord now) { /**
* a DFS (Depth-First-Search) Recursion algorithm that traverses all the coordinates of a player's pieces with
* each step of the DFS algorithm determined by if the current coordinate is adjacent with other coordinates
* within the allCoords set and wether or not the currentPath is contained inside the allCoords.
* @param allCoords all coordinates of a Player's pieces
* @param currentPath a set to store the path that the algorithm has gone through
* @param longestPath a set to store the longest path
* @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) {
// Add the staring point to the `history` of the path that has been taken by the algorithm
currentPath.add(startingPoint);
current.add(now);
for(Coord c : allCoords) { for(Coord c : allCoords) {
if( now.isAdjacentDiagonal(c) && !current.contains(c) ) { //if (currentPath.size() > longestPath.size() ) longestPath = currentPath;
recursionLink(allCoords, current, longest, c); // 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
if( startingPoint.isAdjacentDiagonal(c) && !currentPath.contains(c) ) {
// if the currentPath is bigger than the longestPath, update the longest Path
if (currentPath.size() > longestPath.size() ) longestPath = currentPath;
recursionLink(allCoords, currentPath, longestPath, c);
} }
} }
} }
public static int scoreForLink(Set<Coord> longestLink, Island[] islands) { /**
Set<Island> connectedIslands = new HashSet<>(); * Helper method for finding the scores for a link set. This helper method is purely to help the
* `findLongestLink` method.
* @param longestLink the link that wants to be searched the score for
* @param islands the islands within the world
* @return the score of the link set
*/
public static int helperScoreForLink(Set<Coord> longestLink, Island[] islands) {
Set<Island> connectedIslands = new HashSet<>(); // Container for connected Islands within the longest Link set
for (Coord c : longestLink) { for (Coord c : longestLink) {
for (Island i : islands) { for (Island i : islands) {
if (i.containsCoord(c)) { if (i.containsCoord(c)) {