Task 11 Linking progress for now.

NOTE: Yeah idk if this is correct or not, but that's it for now

Signed-off-by: Immanuel Alvaro Bhirawa <u7280427@anu.edu.au>
This commit is contained in:
Immanuel Alvaro Bhirawa 2023-05-04 13:05:56 +10:00
parent cf4bb47c8f
commit ad3a9c4a00
2 changed files with 59 additions and 198 deletions

View File

@ -508,9 +508,11 @@ public class BlueLagoon {
public static int[] calculateIslandLinksScore(String stateString){ public static int[] calculateIslandLinksScore(String stateString){
State state = new State(stateString); State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()]; int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) { for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.scoreLinks(i); scores[i] = state.scoreLinks(i);
} }
return scores; return scores;
} }

View File

@ -1,8 +1,6 @@
package comp1110.ass2; package comp1110.ass2;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Random;
/** /**
* Object to store the game state * Object to store the game state
@ -562,215 +560,76 @@ public class State {
* @return int score * @return int score
*/ */
// public boolean isAdjacent(Coord coord) {
// if (this.y == coord.y) {
// return (this.x == coord.x - 1 || this.x == coord.x + 1);
// }
// if (this.x == coord.x) {
// return (this.y == coord.y - 1 || this.y == coord.y + 1);
// }
// return false;
// }
//
// /**
// * Check if two coordinates are adjacent (includes diagonals)
// * @param coord Coord object to compare to
// */
// 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);
// }
// public Player(int playerID) {
// this.playerID = playerID;
// this.score = 0;
// this.numCoconuts = 0;
// this.numBamboo = 0;
// this.numWater = 0;
// this.numPreciousStones = 0;
// this.numStatuette = 0;
// this.settlers = new Coord[0];
// this.villages = new Coord[0];
// }
// // endregion
// // region Getters and Setters
//
// /**
// * Get the player's ID
// * @return int player ID
// */
// public int getPlayerID() {
// return playerID;
// }
// /**
// * Get the player's settlers
// * @return Coord[] list of the player's settlers coords
// */
// public Coord[] getSettlers() {
// return settlers;
// }
//
// /**
// * Get the player's villages
// * @return Coord[] list of the player's villages coords
// */
// public Coord[] getVillages() {
// return villages;
// }
// public Coord[] getPieces() {
// Coord[] pieces = new Coord[settlers.length + villages.length];
// for (int i = 0; i < settlers.length; i++) {
// pieces[i] = settlers[i];
// }
// for (int i = 0; i < villages.length; i++) {
// pieces[settlers.length + i] = villages[i];
// }
// return pieces;
// }
//
// /**
// * Get number of pieces on island
// * @param island Island island to check
// * @return int number of pieces on island
// */
// public int getNumPiecesOnIsland(Island island) {
// int numPieces = 0;
// for (Coord piece : getPieces()) {
// if (island.containsCoord(piece)) {
// numPieces++;
// }
// }
// return numPieces;
// }
// /**
// * Get the coordinates of the island
// * @return Coord[] coordinates of the island
// */
// public Coord[] getCoords() {
// return coords;
// }
// /**
// * Check if the island contains a coordinate
// * @param coord the coordinate to be checked
// * @return boolean true if the island contains the coordinate
// */
// public boolean containsCoord(Coord coord) {
// for (Coord c : this.coords) {
// if (c.equals(coord)) {
// return true;
// }
// }
// return false;
// }
public int scoreLinks(int playerID) { public int scoreLinks(int playerID) {
int maxIslands = 0; int numOfIslands = 0;
int distinctIslandsCounter = 0;
int score = 0;
Coord[] playerCoords = players[playerID].getPieces(); // playerCoords Coord[] playerCoords = players[playerID].getPieces(); // playerCoords
Set<Coord> playerCoordsSet = new HashSet<>(Arrays.asList(playerCoords));
Set<Coord> playerLongestLink = findLongestLink(playerCoordsSet, islands);
outerLoop:
for(Island island : islands) { for(Island island : islands) {
Coord[] islandCoords = island.getCoords(); for ( Coord playerCoord : playerLongestLink ) {
for ( Coord playerCoord : playerCoords ) {
if (island.containsCoord(playerCoord)) { if (island.containsCoord(playerCoord)) {
numOfIslands++;
continue outerLoop;
} }
} }
} }
// for(Island island : islands) {
// Coord[] islandCoords = island.getCoords(); // Island Coords
//
// for ( Coord playerCoord : playerCoords) {
// for( Coord islandCoord : islandCoords) {
// if (playerCoord.areTwoCoordsLink(islandCoord) && )
// }
// }
//
// }
return maxIslands * 5; //! TODO return numOfIslands * 5;
} }
// public boolean areTwoCoordsLink ( Coord coord) { public static Set<Coord> findLongestLink ( Set<Coord> allCoords, Island[] islands) {
// if(isAdjacent(coord) || isAdjacentDiagonal(coord)) return true; Set<Coord> longest = new HashSet<>();
// else return false; Set<Coord> current = new HashSet<>();
// }
//
// public Coord[] longestLink ( Coord coord) {
// ArrayList<Coord> linkContainer = new ArrayList<>();
// if (areTwoCoordsLink(coord)) {
// linkContainer.add(coord);
// }
//
// Coord[] endLinkContainer = new Coord[linkContainer.size()];
// return endLinkContainer;
// }
// Score Links Coord now;
// * A (potentially) branching path of neighbouring settlers and villages
// * belonging to a player forms a chain. Players earn points from the chain
// * of their pieces which links the most islands. Players earn 5 points
// * per linked island in this chain.
// public int scoreTotalIslands(int playerID) { for(Island islandIter : islands) {
// int score = 0; for ( Coord coords : allCoords) {
// int islandCount = 0; if(islandIter.containsCoord(coords)) {
// for (Island island : islands) { now = coords;
// // Get island coords current.add(coords);
// Coord[] islandCoords = island.getCoords(); recurssionLink(allCoords, current, longest, now);
// // Get player's coords
// Coord[] playerCoords = players[playerID].getPieces();
// // Check if player has a piece on the island
// boolean hasPiece = false;
// for (Coord playerCoord : playerCoords) {
// for (Coord islandCoord : islandCoords) {
// if (playerCoord.equals(islandCoord)) {
// hasPiece = true;
// break;
// }
// }
// if (hasPiece) break;
// }
// if (hasPiece) islandCount++;
// }
//
// if (islandCount >= 8) score = 20;
// else if (islandCount == 7) score = 10;
//
// return score;
// }
// public boolean isAdjacent(Coord coord) { // if the score is bigger, update the Set
// if (this.y == coord.y) { if(scoreForLink(current, islands) > scoreForLink(longest,islands)) {
// return (this.x == coord.x - 1 || this.x == coord.x + 1); longest.clear();
// } longest.addAll(current);
// if (this.x == coord.x) { }
// return (this.y == coord.y - 1 || this.y == coord.y + 1); current = new HashSet<>();
// } }
// return false; }
// } }
// return longest;
// /** }
// * Check if two coordinates are adjacent (includes diagonals)
// * @param coord Coord object to compare to public static void recurssionLink (Set<Coord> allCoords, Set<Coord> current, Set<Coord> longest, Coord now) {
// */
// public boolean isAdjacentDiagonal(Coord coord){ current.add(now);
// if (isAdjacent(coord)) return true; for(Coord c : allCoords) {
// if (this.x == coord.x - 1 && this.y == coord.y - 1) return true; if( (now.isAdjacent(c) || now.isAdjacentDiagonal(c)) && !current.contains(c) ) {
// if (this.x == coord.x - 1 && this.y == coord.y + 1) return true; recurssionLink(allCoords, current, longest, c);
// if (this.x == coord.x + 1 && this.y == coord.y - 1) return true; }
// return (this.x == coord.x + 1 && this.y == coord.y + 1); }
// } }
public static int scoreForLink(Set<Coord> longestLink, Island[] islands) {
int numOfIslands = 0;
outerLoop:
for (Island i : islands) {
for ( Coord c : longestLink ) {
if (i.containsCoord(c)) {
numOfIslands++;
continue outerLoop;
}
}
}
return numOfIslands * 5;
}
/** /**
* Score resources * Score resources