task8: Sped up code some more

This commit is contained in:
Nathan Woodburn 2023-04-14 17:19:48 +10:00
parent a12db3b0bd
commit be28548cd7
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -402,7 +402,7 @@ public class BlueLagoon {
* @return true if the move is valid, false otherwise
*/
public static boolean isMoveValidTrim(String stateString, String moveString, int numberOfPlayer,int boardHeight,String currentPhase, String playerId,ArrayList<String> coordsContainer) {
public static boolean isMoveValidTrim(String stateString, String moveString, int numberOfPlayer,int boardHeight,String currentPhase, String playerId,ArrayList<String> coordsContainer, String[] pStates) {
String[] parts = stateString.split("; ?");
@ -421,12 +421,9 @@ public class BlueLagoon {
int settlerCounter = 0;
int villageCounter = 0;
for (String part : parts) {
String[] parseSplit = part.split(" ");
String stateCases = parseSplit[0];
for (String pState:pStates) {
switch (stateCases) {
case "p":
String[] parseSplit = pState.split(" ");
// Check if there's enough pieces left for that player that is moving
pStatePlayerId = parseSplit[1];
@ -437,23 +434,23 @@ public class BlueLagoon {
// If the current player ID is the same as the placed settler's player ID
// Store it into array
if(pStatePlayerId.equals(playerId)) playerSettlerCoords.add(parseSplit[i]);
if (pStatePlayerId.equals(playerId)) playerSettlerCoords.add(parseSplit[i]);
i++;
}
// If the current player ID is the same as the placed settler's player ID
// iterate the settlerCounter
if(pStatePlayerId.equals(playerId)) settlerCounter = playerSettlerCoords.size();
if (pStatePlayerId.equals(playerId)) settlerCounter = playerSettlerCoords.size();
i++;
// Collecting the village coords that has been placed
while (i < parseSplit.length) {
if(pStatePlayerId.equals(playerId)) villageCounter = i - 9 - settlerCounter;
if (pStatePlayerId.equals(playerId)) villageCounter = i - 9 - settlerCounter;
villageCoords.add(parseSplit[i]); // Store all the village Coords
// If the current player ID is the same as the placed Village's player ID
// Store it into array
if(pStatePlayerId.equals(playerId)) playerVillageCoords.add(parseSplit[i]);
if (pStatePlayerId.equals(playerId)) playerVillageCoords.add(parseSplit[i]);
i++;
}
@ -483,12 +480,7 @@ public class BlueLagoon {
}
}
}
break;
default:
break;
}
}
// For Exploration Phase and or Settlement Phase
switch(currentPhase){
// Exploration Phase
@ -554,6 +546,9 @@ public class BlueLagoon {
// Get player data
String allPlayerData = stateString.substring(stateString.indexOf("p " + currentPlayer));
String playerData = allPlayerData.substring(0, allPlayerData.indexOf(";"));
String[] pStates = stateString.substring(stateString.indexOf("p ")).split("; ?");
// Get placed pieces
String settlersPlaced = playerData.substring(playerData.indexOf("S") + 2, playerData.indexOf("T"));
int numSettlersPlaced = settlersPlaced.split(" ").length;
@ -617,12 +612,12 @@ public class BlueLagoon {
// If the player has not placed all their settlers
if (hasSettler){
if (isMoveValidTrim(stateString, "S " + cord,numPlayers,boardHeight,gamePhase,currentPlayer,coordsContainer)) {
if (isMoveValidTrim(stateString, "S " + cord,numPlayers,boardHeight,gamePhase,currentPlayer,coordsContainer,pStates)) {
allMoves.add("S " + cord);
}
}
if (hasVillage && gamePhase == "E") {
if (isMoveValidTrim(stateString, "T " + cord,numPlayers,boardHeight,gamePhase,currentPlayer,coordsContainer)) {
if (isMoveValidTrim(stateString, "T " + cord,numPlayers,boardHeight,gamePhase,currentPlayer,coordsContainer,pStates)) {
allMoves.add("T " + cord);
}
}