diff --git a/admin/E-review-u7492895.md b/admin/E-review-u7492895.md new file mode 100644 index 0000000..e69de29 diff --git a/src/comp1110/ass2/Coord.java b/src/comp1110/ass2/Coord.java index ea21bfb..a7d9b8d 100644 --- a/src/comp1110/ass2/Coord.java +++ b/src/comp1110/ass2/Coord.java @@ -70,10 +70,20 @@ public record Coord(int y, int x) { */ public boolean isAdjacentDiagonal(Coord coord) { if (isAdjacent(coord)) return true; - if (this.x == coord.x - 1 && this.y == coord.y - 1) return true; - if (this.x == coord.x - 1 && this.y == coord.y + 1) return true; - if (this.x == coord.x + 1 && this.y == coord.y - 1) return true; - return (this.x == coord.x + 1 && this.y == coord.y + 1); + + // Consider hex offsets + if (y%2 == 0){ + if (this.x == coord.x && this.y == coord.y + 1) return true; + if (this.x == coord.x && this.y == coord.y - 1) return true; + if (this.x == coord.x - 1 && this.y == coord.y + 1) return true; + return (this.x == coord.x - 1 && this.y == coord.y - 1); + } + else { + if (this.x == coord.x && this.y == coord.y + 1) return true; + if (this.x == coord.x && this.y == coord.y - 1) return true; + if (this.x == coord.x + 1 && this.y == coord.y + 1) return true; + return (this.x == coord.x + 1 && this.y == coord.y - 1); + } } /** diff --git a/src/comp1110/ass2/State.java b/src/comp1110/ass2/State.java index a7b3558..bc7eeed 100644 --- a/src/comp1110/ass2/State.java +++ b/src/comp1110/ass2/State.java @@ -561,61 +561,53 @@ public class State { */ public int scoreLinks(int playerID) { - int numOfIslands = 0; - Coord[] playerCoords = players[playerID].getPieces(); // playerCoords + Coord[] playerCoords = players[playerID].getPieces(); Set playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords)); - Set playerLongestLink = findLongestLink(playerCoordsSet, islands); + int maxScore = findLongestLinkScore(playerCoordsSet, islands); - outerLoop: - for(Island island : islands) { - for ( Coord playerCoord : playerLongestLink ) { - if (island.containsCoord(playerCoord)) { - numOfIslands++; - continue outerLoop; - } - } - } - - return numOfIslands * 5; + return maxScore; } - public static Set findLongestLink ( Set allCoords, Island[] islands) { + public static int findLongestLinkScore ( Set allCoords, Island[] islands) { Set longest = new HashSet<>(); Set current = new HashSet<>(); Coord now; + int max = scoreForLink(longest,islands); + for(Island islandIter : islands) { for ( Coord coords : allCoords) { if(islandIter.containsCoord(coords)) { now = coords; current.add(coords); - recurssionLink(allCoords, current, longest, now); + recursionLink(allCoords, current, longest, now); // if the score is bigger, update the Set - if(helperScoreForLink(current, islands) > helperScoreForLink(longest,islands)) { + if(scoreForLink(current, islands) > max) { longest.clear(); longest.addAll(current); + max = scoreForLink(longest, islands); } current = new HashSet<>(); } } } - return longest; + return max; } - public static void recurssionLink (Set allCoords, Set current, Set longest, Coord now) { + public static void recursionLink(Set allCoords, Set current, Set longest, Coord now) { current.add(now); for(Coord c : allCoords) { - if( (now.isAdjacent(c) || now.isAdjacentDiagonal(c)) && !current.contains(c) ) { - recurssionLink(allCoords, current, longest, c); + if( now.isAdjacentDiagonal(c) && !current.contains(c) ) { + recursionLink(allCoords, current, longest, c); } } } - public static int helperScoreForLink(Set longestLink, Island[] islands) { + public static int scoreForLink(Set longestLink, Island[] islands) { int numOfIslands = 0; outerLoop: