blueLagoon: pointed endPhase to state object

This commit is contained in:
2023-04-24 18:10:57 +10:00
parent 70f3b3c707
commit bfc7070b78
3 changed files with 72 additions and 50 deletions

View File

@@ -823,7 +823,13 @@ public class BlueLagoon {
* @return a string representing the new state achieved by following the end of phase rules * @return a string representing the new state achieved by following the end of phase rules
*/ */
public static String endPhase(String stateString){ public static String endPhase(String stateString){
return ""; // FIXME Task 12 State state = new State(stateString);
state.scorePhase();
if (state.getCurrentPhase() == 'E') {
state.cleanBoard();
state.distributeResources();
}
return state.toString();
} }
/** /**

View File

@@ -108,28 +108,16 @@ public class Player {
} }
/** /**
* Remove a resource from the player's resources * Remove all resources from the player's resources
* @param numResource int number of the resource to be removed
* @param resourceType char resource type
*/ */
public void removeResource(int numResource, char resourceType) { public void removeResources() {
switch (resourceType) { numCoconuts = 0;
case 'C': numBamboo = 0;
numCoconuts -= numResource; numWater = 0;
break; numPreciousStones = 0;
case 'B': numStatuette = 0;
numBamboo -= numResource;
break;
case 'W':
numWater -= numResource;
break;
case 'P':
numPreciousStones -= numResource;
break;
case 'S':
numStatuette -= numResource;
break;
}
} }
/** /**
@@ -180,30 +168,28 @@ public class Player {
} }
/** /**
* Check if player is equal to another player * Delete all settlers
* @param player Player player to be compared to
* @return true if the players are equal, false otherwise
*/ */
public boolean equals(Player player) { public void clearSettlers() {
if (player.getPlayerID() != playerID) return false; settlers = new Coord[0];
if (player.getScore() != score) return false;
if (player.getNumResource('C') != numCoconuts) return false;
if (player.getNumResource('B') != numBamboo) return false;
if (player.getNumResource('W') != numWater) return false;
if (player.getNumResource('P') != numPreciousStones) return false;
if (player.getNumResource('S') != numStatuette) return false;
if (player.getSettlers().length != settlers.length) return false;
if (player.getVillages().length != villages.length) return false;
for (int i = 0; i < settlers.length; i++) {
if (!player.getSettlers()[i].equals(settlers[i])) return false;
}
for (int i = 0; i < villages.length; i++) {
if (!player.getVillages()[i].equals(villages[i])) return false;
}
return true;
} }
/**
* Delete village
*/
public void removeVillage(Coord coord) {
Coord[] newVillages = new Coord[villages.length - 1];
int j = 0;
for (int i = 0; i < villages.length; i++) {
if (villages[i] != coord) {
newVillages[j] = villages[i];
j++;
}
}
villages = newVillages;
}
/** /**
* Get all the player's piece's coords * Get all the player's piece's coords
* @return coord[] list of all the player's piece's coords * @return coord[] list of all the player's piece's coords
@@ -283,7 +269,30 @@ public class Player {
} }
/**
* Check if player is equal to another player
* @param player Player player to be compared to
* @return true if the players are equal, false otherwise
*/
public boolean equals(Player player) {
if (player.getPlayerID() != playerID) return false;
if (player.getScore() != score) return false;
if (player.getNumResource('C') != numCoconuts) return false;
if (player.getNumResource('B') != numBamboo) return false;
if (player.getNumResource('W') != numWater) return false;
if (player.getNumResource('P') != numPreciousStones) return false;
if (player.getNumResource('S') != numStatuette) return false;
if (player.getSettlers().length != settlers.length) return false;
if (player.getVillages().length != villages.length) return false;
for (int i = 0; i < settlers.length; i++) {
if (!player.getSettlers()[i].equals(settlers[i])) return false;
}
for (int i = 0; i < villages.length; i++) {
if (!player.getVillages()[i].equals(villages[i])) return false;
}
return true;
}
@Override @Override
public String toString() { public String toString() {

View File

@@ -23,8 +23,6 @@ public class State {
private Coord[] stonesCoords; private Coord[] stonesCoords;
private Resource[] resources; private Resource[] resources;
private Player[] players; private Player[] players;
private boolean distributedResources;
// endregion // endregion
// region Setup methods/constructors // region Setup methods/constructors
@@ -34,7 +32,6 @@ public class State {
* @param stateString String containing the state of the game * @param stateString String containing the state of the game
*/ */
public State(String stateString) { public State(String stateString) {
distributedResources = false;
// Split the state string into its components // Split the state string into its components
String[] components = stateString.split(";"); String[] components = stateString.split(";");
@@ -103,8 +100,6 @@ public class State {
type = resourcesComponents[i].charAt(0); type = resourcesComponents[i].charAt(0);
continue; continue;
} }
distributedResources = true;
String[] coordComponents = resourcesComponents[i].split(","); String[] coordComponents = resourcesComponents[i].split(",");
Coord tmpCoord = new Coord(Integer.parseInt(coordComponents[0]), Integer.parseInt(coordComponents[1])); Coord tmpCoord = new Coord(Integer.parseInt(coordComponents[0]), Integer.parseInt(coordComponents[1]));
resources[i-1-currentResource] = new Resource(type, tmpCoord); resources[i-1-currentResource] = new Resource(type, tmpCoord);
@@ -144,8 +139,6 @@ public class State {
} }
public void distributeResources() { public void distributeResources() {
// Do some checks
if (distributedResources) return; // Resources have already been distributed
if (stonesCoords.length != 32) return; // There are not enough stones to distribute resources if (stonesCoords.length != 32) return; // There are not enough stones to distribute resources
@@ -399,6 +392,20 @@ public class State {
return !resourcesLeft || !moveLeft; return !resourcesLeft || !moveLeft;
} }
/**
* Clean board for next phase
*/
public void cleanBoard(){
for (Player player : players) {
player.clearSettlers();
player.removeResources();
for (Coord coord : player.getVillages()) {
if (isStone(coord)) {
player.removeVillage(coord);
}
}
}
}
// endregion // endregion