# Conflicts:
#	src/comp1110/ass2/State.java
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-05-06 20:02:09 +10:00
commit 3d84fa1e83
3 changed files with 28 additions and 26 deletions

View File

View File

@ -70,10 +70,20 @@ public record Coord(int y, int x) {
*/ */
public boolean isAdjacentDiagonal(Coord coord) { public boolean isAdjacentDiagonal(Coord coord) {
if (isAdjacent(coord)) return true; 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; // Consider hex offsets
if (this.x == coord.x + 1 && this.y == coord.y - 1) return true; if (y%2 == 0){
return (this.x == coord.x + 1 && this.y == coord.y + 1); 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);
}
} }
/** /**

View File

@ -561,61 +561,53 @@ public class State {
*/ */
public int scoreLinks(int playerID) { public int scoreLinks(int playerID) {
int numOfIslands = 0; Coord[] playerCoords = players[playerID].getPieces();
Coord[] playerCoords = players[playerID].getPieces(); // playerCoords
Set<Coord> playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords)); Set<Coord> playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords));
Set<Coord> playerLongestLink = findLongestLink(playerCoordsSet, islands); int maxScore = findLongestLinkScore(playerCoordsSet, islands);
outerLoop: return maxScore;
for(Island island : islands) {
for ( Coord playerCoord : playerLongestLink ) {
if (island.containsCoord(playerCoord)) {
numOfIslands++;
continue outerLoop;
}
}
}
return numOfIslands * 5;
} }
public static Set<Coord> findLongestLink ( Set<Coord> allCoords, Island[] islands) { public static int findLongestLinkScore ( Set<Coord> allCoords, Island[] islands) {
Set<Coord> longest = new HashSet<>(); Set<Coord> longest = new HashSet<>();
Set<Coord> current = new HashSet<>(); Set<Coord> current = new HashSet<>();
Coord now; Coord now;
int max = scoreForLink(longest,islands);
for(Island islandIter : islands) { for(Island islandIter : islands) {
for ( Coord coords : allCoords) { for ( Coord coords : allCoords) {
if(islandIter.containsCoord(coords)) { if(islandIter.containsCoord(coords)) {
now = coords; now = coords;
current.add(coords); current.add(coords);
recurssionLink(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(helperScoreForLink(current, islands) > helperScoreForLink(longest,islands)) { if(scoreForLink(current, islands) > max) {
longest.clear(); longest.clear();
longest.addAll(current); longest.addAll(current);
max = scoreForLink(longest, islands);
} }
current = new HashSet<>(); current = new HashSet<>();
} }
} }
} }
return longest; return max;
} }
public static void recurssionLink (Set<Coord> allCoords, Set<Coord> current, Set<Coord> longest, Coord now) { public static void recursionLink(Set<Coord> allCoords, Set<Coord> current, Set<Coord> longest, Coord now) {
current.add(now); current.add(now);
for(Coord c : allCoords) { for(Coord c : allCoords) {
if( (now.isAdjacent(c) || now.isAdjacentDiagonal(c)) && !current.contains(c) ) { if( now.isAdjacentDiagonal(c) && !current.contains(c) ) {
recurssionLink(allCoords, current, longest, c); recursionLink(allCoords, current, longest, c);
} }
} }
} }
public static int helperScoreForLink(Set<Coord> longestLink, Island[] islands) { public static int scoreForLink(Set<Coord> longestLink, Island[] islands) {
int numOfIslands = 0; int numOfIslands = 0;
outerLoop: outerLoop: