blueLagoon: pointed endPhase to state object

This commit is contained in:
Nathan Woodburn 2023-04-24 18:10:57 +10:00
parent 70f3b3c707
commit bfc7070b78
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
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
*/
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
* @param numResource int number of the resource to be removed
* @param resourceType char resource type
* Remove all resources from the player's resources
*/
public void removeResource(int numResource, char resourceType) {
switch (resourceType) {
case 'C':
numCoconuts -= numResource;
break;
case 'B':
numBamboo -= numResource;
break;
case 'W':
numWater -= numResource;
break;
case 'P':
numPreciousStones -= numResource;
break;
case 'S':
numStatuette -= numResource;
break;
}
public void removeResources() {
numCoconuts = 0;
numBamboo = 0;
numWater = 0;
numPreciousStones = 0;
numStatuette = 0;
}
/**
@ -180,30 +168,28 @@ 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
* Delete all settlers
*/
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;
public void clearSettlers() {
settlers = new Coord[0];
}
/**
* 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
* @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
public String toString() {

View File

@ -23,8 +23,6 @@ public class State {
private Coord[] stonesCoords;
private Resource[] resources;
private Player[] players;
private boolean distributedResources;
// endregion
// region Setup methods/constructors
@ -34,7 +32,6 @@ public class State {
* @param stateString String containing the state of the game
*/
public State(String stateString) {
distributedResources = false;
// Split the state string into its components
String[] components = stateString.split(";");
@ -103,8 +100,6 @@ public class State {
type = resourcesComponents[i].charAt(0);
continue;
}
distributedResources = true;
String[] coordComponents = resourcesComponents[i].split(",");
Coord tmpCoord = new Coord(Integer.parseInt(coordComponents[0]), Integer.parseInt(coordComponents[1]));
resources[i-1-currentResource] = new Resource(type, tmpCoord);
@ -144,8 +139,6 @@ public class State {
}
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
@ -399,6 +392,20 @@ public class State {
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