task8: Sped up code some more
This commit is contained in:
parent
a12db3b0bd
commit
be28548cd7
@ -402,7 +402,7 @@ public class BlueLagoon {
|
|||||||
* @return true if the move is valid, false otherwise
|
* @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("; ?");
|
String[] parts = stateString.split("; ?");
|
||||||
|
|
||||||
@ -421,74 +421,66 @@ public class BlueLagoon {
|
|||||||
int settlerCounter = 0;
|
int settlerCounter = 0;
|
||||||
int villageCounter = 0;
|
int villageCounter = 0;
|
||||||
|
|
||||||
for (String part : parts) {
|
for (String pState:pStates) {
|
||||||
String[] parseSplit = part.split(" ");
|
|
||||||
String stateCases = parseSplit[0];
|
|
||||||
|
|
||||||
switch (stateCases) {
|
String[] parseSplit = pState.split(" ");
|
||||||
case "p":
|
// Check if there's enough pieces left for that player that is moving
|
||||||
// Check if there's enough pieces left for that player that is moving
|
pStatePlayerId = parseSplit[1];
|
||||||
pStatePlayerId = parseSplit[1];
|
|
||||||
|
|
||||||
// Collecting the settler Coords that has been placed
|
// Collecting the settler Coords that has been placed
|
||||||
for (int i = 9; i < parseSplit.length; i++) {
|
for (int i = 9; i < parseSplit.length; i++) {
|
||||||
while (!parseSplit[i].equals("T")) {
|
while (!parseSplit[i].equals("T")) {
|
||||||
settlerCoords.add(parseSplit[i]); // Store all the settler coords
|
settlerCoords.add(parseSplit[i]); // Store all the settler coords
|
||||||
|
|
||||||
// If the current player ID is the same as the placed settler's player ID
|
// If the current player ID is the same as the placed settler's player ID
|
||||||
// Store it into array
|
// Store it into array
|
||||||
if(pStatePlayerId.equals(playerId)) playerSettlerCoords.add(parseSplit[i]);
|
if (pStatePlayerId.equals(playerId)) playerSettlerCoords.add(parseSplit[i]);
|
||||||
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();
|
||||||
|
i++;
|
||||||
|
|
||||||
|
// Collecting the village coords that has been placed
|
||||||
|
while (i < parseSplit.length) {
|
||||||
|
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]);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Checking the requirement of how many pieces are left //
|
||||||
|
switch (numberOfPlayer) {
|
||||||
|
case 4:
|
||||||
|
numberOfSettlersPerPlayer -= 10;
|
||||||
|
if (pieceType.equals("S")) {
|
||||||
|
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
||||||
|
} else if (pieceType.equals("T")) {
|
||||||
|
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
// If the current player ID is the same as the placed settler's player ID
|
case 3:
|
||||||
// iterate the settlerCounter
|
numberOfSettlersPerPlayer -= 5;
|
||||||
if(pStatePlayerId.equals(playerId)) settlerCounter = playerSettlerCoords.size();
|
if (pieceType.equals("S")) {
|
||||||
i++;
|
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
||||||
|
} else if (pieceType.equals("T")) {
|
||||||
// Collecting the village coords that has been placed
|
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
||||||
while (i < parseSplit.length) {
|
|
||||||
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]);
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
// Checking the requirement of how many pieces are left //
|
case 2:
|
||||||
switch (numberOfPlayer) {
|
if (pieceType.equals("S")) {
|
||||||
case 4:
|
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
||||||
numberOfSettlersPerPlayer -= 10;
|
} else if (pieceType.equals("T")) {
|
||||||
if (pieceType.equals("S")) {
|
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
||||||
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
|
||||||
} else if (pieceType.equals("T")) {
|
|
||||||
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
numberOfSettlersPerPlayer -= 5;
|
|
||||||
if (pieceType.equals("S")) {
|
|
||||||
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
|
||||||
} else if (pieceType.equals("T")) {
|
|
||||||
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case 2:
|
|
||||||
if (pieceType.equals("S")) {
|
|
||||||
if (settlerCounter + 1 > numberOfSettlersPerPlayer) return false;
|
|
||||||
} else if (pieceType.equals("T")) {
|
|
||||||
if (villageCounter + 1 > numberOfVillagesPerPlayer) return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For Exploration Phase and or Settlement Phase
|
// For Exploration Phase and or Settlement Phase
|
||||||
switch(currentPhase){
|
switch(currentPhase){
|
||||||
// Exploration Phase
|
// Exploration Phase
|
||||||
@ -554,6 +546,9 @@ public class BlueLagoon {
|
|||||||
// Get player data
|
// Get player data
|
||||||
String allPlayerData = stateString.substring(stateString.indexOf("p " + currentPlayer));
|
String allPlayerData = stateString.substring(stateString.indexOf("p " + currentPlayer));
|
||||||
String playerData = allPlayerData.substring(0, allPlayerData.indexOf(";"));
|
String playerData = allPlayerData.substring(0, allPlayerData.indexOf(";"));
|
||||||
|
String[] pStates = stateString.substring(stateString.indexOf("p ")).split("; ?");
|
||||||
|
|
||||||
|
|
||||||
// Get placed pieces
|
// Get placed pieces
|
||||||
String settlersPlaced = playerData.substring(playerData.indexOf("S") + 2, playerData.indexOf("T"));
|
String settlersPlaced = playerData.substring(playerData.indexOf("S") + 2, playerData.indexOf("T"));
|
||||||
int numSettlersPlaced = settlersPlaced.split(" ").length;
|
int numSettlersPlaced = settlersPlaced.split(" ").length;
|
||||||
@ -617,12 +612,12 @@ public class BlueLagoon {
|
|||||||
|
|
||||||
// If the player has not placed all their settlers
|
// If the player has not placed all their settlers
|
||||||
if (hasSettler){
|
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);
|
allMoves.add("S " + cord);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (hasVillage && gamePhase == "E") {
|
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);
|
allMoves.add("T " + cord);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user