task8: Use ismovevalid

This commit is contained in:
Nathan Woodburn 2023-04-14 16:08:57 +10:00
parent 999177bc67
commit 8802c9545f
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -223,7 +223,7 @@ public class BlueLagoon {
String playerId = ""; // Player ID String playerId = ""; // Player ID
String pStatePlayerId = ""; // the current Player's move ID String pStatePlayerId = ""; // the current Player's move ID
ArrayList<String> settlerCoords = new ArrayList<>(); // Placed Settler Coordinates ArrayList<String> settlerCoords = new ArrayList<>(); // Placed Settler Coordinates
ArrayList<String> villageCoords = new ArrayList<>(); // Placed villags coordinates ArrayList<String> villageCoords = new ArrayList<>(); // Placed villages coordinates
ArrayList<String> playerSettlerCoords = new ArrayList<>(); // The current Player's settler coords ArrayList<String> playerSettlerCoords = new ArrayList<>(); // The current Player's settler coords
ArrayList<String> playerVillageCoords = new ArrayList<>(); // The current Player's Village coords ArrayList<String> playerVillageCoords = new ArrayList<>(); // The current Player's Village coords
@ -324,7 +324,6 @@ public class BlueLagoon {
} }
} }
} }
// }
break; break;
} }
} }
@ -335,9 +334,8 @@ public class BlueLagoon {
// if it's even rows, check the number of cols for out of bound (i.e. the width) // if it's even rows, check the number of cols for out of bound (i.e. the width)
if(yMoveCoords % 2 == 0) { if(yMoveCoords % 2 == 0) {
if(xMoveCoords > boardHeight - 2) return false; if(xMoveCoords > boardHeight - 2) return false;
} else if (yMoveCoords % 2 != 0) {
if(xMoveCoords > boardHeight - 1) return false;
} }
else if(xMoveCoords > boardHeight - 1) return false;
// For Exploration Phase and or Settlement Phase // For Exploration Phase and or Settlement Phase
switch(currentPhase){ switch(currentPhase){
@ -414,23 +412,17 @@ public class BlueLagoon {
// Get number of players // Get number of players
int numPlayers = Character.getNumericValue(stateString.charAt(stateString.indexOf(";") - 1)); int numPlayers = Character.getNumericValue(stateString.charAt(stateString.indexOf(";") - 1));
// Store the current game phase // Store the current game phase
int gamePhase = 0; int gamePhase = 0;
// If the game is not in the exploration phase use state 1 // If the game is not in the exploration phase use state 1
if (!stateString.contains("E")) gamePhase = 1; if (!stateString.contains("E")) gamePhase = 1;
// Get the current player // Get the current player
int currentPlayer = Character.getNumericValue(stateString.charAt(stateString.indexOf("c ") + 2)); int currentPlayer = Character.getNumericValue(stateString.charAt(stateString.indexOf("c ") + 2));
// Get the board size // Get the board size
int boardHeight = Integer.parseInt(stateString.substring(stateString.indexOf("a ") + 2, stateString.indexOf(";") - 2)); int boardHeight = Integer.parseInt(stateString.substring(stateString.indexOf("a ") + 2, stateString.indexOf(";") - 2));
// 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(";"));
// 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;
@ -442,10 +434,8 @@ public class BlueLagoon {
if (!villagesPlaced.contains(" ")){ if (!villagesPlaced.contains(" ")){
numVillagesPlaced = 0; numVillagesPlaced = 0;
} }
// Calculate number of pieces each player starts with // Calculate number of pieces each player starts with
int startNumSettlers = 0; int startNumSettlers = 0;
switch (numPlayers) { switch (numPlayers) {
case 2: case 2:
startNumSettlers = 30; startNumSettlers = 30;
@ -460,17 +450,11 @@ public class BlueLagoon {
// Check if the player has placed all their settlers or villages // Check if the player has placed all their settlers or villages
boolean hasSettler = (numSettlersPlaced < startNumSettlers); boolean hasSettler = (numSettlersPlaced < startNumSettlers);
boolean hasVillage = (numVillagesPlaced < 5); boolean hasVillage = (numVillagesPlaced < 6);
// Create a set to store all possible moves // Create a set to store all possible moves
Set<String> allMoves = new HashSet<>(); Set<String> allMoves = new HashSet<>();
// If the player has placed all their settlers and villages
if (!hasSettler && !hasVillage){
return allMoves;
}
// Generate all possible coordinates in an array // Generate all possible coordinates in an array
String[] coordinates = new String[boardHeight * boardHeight]; String[] coordinates = new String[boardHeight * boardHeight];
int index = 0; int index = 0;
@ -480,55 +464,24 @@ public class BlueLagoon {
index++; index++;
} }
} }
// For each coordinate // For each coordinate
for (String cord:coordinates) { for (String cord:coordinates) {
// If the player has not placed all their settlers // If the player has not placed all their settlers
if (hasSettler && gamePhase == 0) {
// Generate all possible settler moves
// If the coordinate is not occupied and is water
if (!stateString.contains(" " + cord + " ")) {
if (cord.equals("0,12")){
System.out.println("1");
}
allMoves.add("S " + cord);
}
}
// If the coordinate is not occupied
if (!allPlayerData.contains(" " + cord + " ")) {
// if the coordinate is adjacent to one of the player's pieces
int firstCord = Integer.parseInt(cord.substring(0, cord.indexOf(",")));
int secondCord = Integer.parseInt(cord.substring(cord.indexOf(",") + 1));
if (playerData.contains(" " + (firstCord - 1) + "," + (secondCord + 1) + " ") ||
playerData.contains(" " + (firstCord + 1) + "," + (secondCord + 1) + " ") ||
playerData.contains(" " + (firstCord - 1) + "," + (secondCord - 1) + " ") ||
playerData.contains(" " + (firstCord + 1) + "," + (secondCord - 1) + " ")) {
if (cord.equals("0,12")){
System.out.println("2");
}
if (hasSettler){ if (hasSettler){
if (isMoveValid(stateString, "S " + cord)) {
allMoves.add("S " + cord); allMoves.add("S " + cord);
} }
if (hasVillage) { }
if (hasVillage && gamePhase == 0) {
if (isMoveValid(stateString, "T " + cord)) {
allMoves.add("T " + cord); allMoves.add("T " + cord);
} }
}
}
} }
return allMoves; //! Test when Task 7 is done
} }
return allMoves;
}
/** /**
* Given a state string, determine whether it represents an end of phase state. * Given a state string, determine whether it represents an end of phase state.