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,12 +421,9 @@ 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];
|
||||||
|
|
||||||
@ -437,23 +434,23 @@ public class BlueLagoon {
|
|||||||
|
|
||||||
// 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
|
// If the current player ID is the same as the placed settler's player ID
|
||||||
// iterate the settlerCounter
|
// iterate the settlerCounter
|
||||||
if(pStatePlayerId.equals(playerId)) settlerCounter = playerSettlerCoords.size();
|
if (pStatePlayerId.equals(playerId)) settlerCounter = playerSettlerCoords.size();
|
||||||
i++;
|
i++;
|
||||||
|
|
||||||
// Collecting the village coords that has been placed
|
// Collecting the village coords that has been placed
|
||||||
while (i < parseSplit.length) {
|
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
|
villageCoords.add(parseSplit[i]); // Store all the village Coords
|
||||||
|
|
||||||
// If the current player ID is the same as the placed Village's player ID
|
// If the current player ID is the same as the placed Village's player ID
|
||||||
// Store it into array
|
// Store it into array
|
||||||
if(pStatePlayerId.equals(playerId)) playerVillageCoords.add(parseSplit[i]);
|
if (pStatePlayerId.equals(playerId)) playerVillageCoords.add(parseSplit[i]);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -483,12 +480,7 @@ public class BlueLagoon {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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