blueLagoon: Pointed scoring to state object

This commit is contained in:
Nathan Woodburn 2023-04-24 17:53:07 +10:00
parent 899497d037
commit da04d53092
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1
3 changed files with 48 additions and 125 deletions

View File

@ -112,88 +112,6 @@ public class BlueLagoon {
state.distributeResources();
return state.toString();
}
// Old distributeResources method
/*
// Check if the stateString is well-formed
if (!isStateStringWellFormed(stateString)) return stateString;
// Grab the stone circles from the stateString
String stoneCircles = stateString.substring(stateString.indexOf("s") + 2, stateString.indexOf("r") - 2);
// Split the stone circles into an array of cords
String[] stoneCircleCords = stoneCircles.split(" ");
// Check if there are 32 stone circles
if (stoneCircleCords.length != 32) return stateString;
// Create a random object and an arrays and list to shuffle the stone circles
Random rand = new Random();
// Number of times to shuffle the stone circles (can be changed)
int shuffle_number = 3;
// Create a copy of the stone circle array to shuffle
String[] stoneCircleRandom = stoneCircleCords;
// Shuffle the stone circles the specified number of times
for (int i = 0; i < shuffle_number; i++) {
// Create a temporary array to store the shuffled stone circles
String[] tempStoneCircleRandom = new String[32];
// Create a list to store the used cords (to avoid duplicates)
List<String> usedCords = new ArrayList<String>();
// Shuffle the array
for (int j = 0; j < 32; j++) {
// For 0-31 generate a random cord from the stone circle array and check if it has been used
int randomIndex = rand.nextInt(31);
while (usedCords.contains(stoneCircleCords[randomIndex])) {
// If it has been used, try the next in line
if (randomIndex == 31) {
randomIndex = 0;
}
else randomIndex++;
}
// If it hasn't been used, add it to the new array
tempStoneCircleRandom[j] = stoneCircleRandom[randomIndex];
usedCords.add(stoneCircleCords[randomIndex]);
}
// Replace the old array with the new one
stoneCircleRandom = tempStoneCircleRandom;
}
// Create a string to store the new resources state
String newResourcesState = "r";
// Create an array for each resource type
char[] resources = {'C', 'B', 'W', 'P'};
// Create a variable to keep track of how many resources have been sorted
int numSorted = 0;
// For each resource type
for (char r:resources){
newResourcesState += " " + r;
// Assign 6 to a stone circle
for (int i = 0; i < 6; i++){
newResourcesState += " " + stoneCircleRandom[numSorted];
numSorted++;
}
}
// Assign 8 statuettes to a stone circle
newResourcesState += " S";
for (int i = 0; i < 8; i++){
newResourcesState += " " + stoneCircleRandom[numSorted];
numSorted++;
}
// Replace the old resources state with the new one
stateString = stateString.replace("r C B W P S", newResourcesState);
return stateString;
}
*/
// endregion
@ -687,7 +605,15 @@ public class BlueLagoon {
// "p 1 42 1 2 3 4 5 S 5,6 8,7 T 1,2;"
public static int[] calculateTotalIslandsScore(String stateString) {
// ArrayList<Integer> islandCoords = islandCoords(stateString);
State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.scoreTotalIslands(i);
}
return scores;
/*// ArrayList<Integer> islandCoords = islandCoords(stateString);
String[] parts = stateString.split("; ?");
@ -747,7 +673,7 @@ public class BlueLagoon {
// check the size of the arrayList to set the size for the int[]
}
return new int[]{0, 0}; // FIXME Task 11
return new int[]{0, 0}; // FIXME Task 11*/
}
// for (int i = 9; i < parseSplit.length; i++) {
@ -791,7 +717,12 @@ public class BlueLagoon {
* the score for each player
*/
public static int[] calculateIslandLinksScore(String stateString){
return new int[]{0, 0}; // FIXME Task 11
State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.scoreLinks(i);
}
return scores;
}
/**
@ -814,7 +745,12 @@ public class BlueLagoon {
* of the score for each player
*/
public static int[] calculateIslandMajoritiesScore(String stateString){
return new int[]{0, 0}; // FIXME Task 11
State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.scoreMajorities(i);
}
return scores;
}
/**
@ -841,7 +777,12 @@ public class BlueLagoon {
* portions of the score for each player
*/
public static int[] calculateResourcesAndStatuettesScore(String stateString){
return new int[]{0, 0}; // FIXME Task 11
State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.scoreResources(i)+state.scoreStatuettes(i);
}
return scores;
}
/**
@ -856,7 +797,12 @@ public class BlueLagoon {
* @return an integer array containing the calculated scores for each player
*/
public static int[] calculateScores(String stateString){
return new int[]{0, 0}; // FIXME Task 11
State state = new State(stateString);
int[] scores = new int[state.getNumPlayers()];
for (int i = 0; i < state.getNumPlayers(); i++) {
scores[i] = state.createScore(i);
}
return scores;
}
/**

View File

@ -272,17 +272,6 @@ public class Player {
i++;
}
}
private int maxRow(Coord[] coords){
int maxRow = 0;
for (Coord coord : coords) {
if (coord.getY() > maxRow) {
maxRow = coord.getY();
}
}
return maxRow;
}
private int maxCol(Coord[] coords){
int maxCol = 0;
for (Coord coord : coords) {
@ -307,7 +296,6 @@ public class Player {
int row = 0;
int col = 0;
int i = 0;
int maxrow = maxRow(settlers);
int maxcol = maxCol(settlers);
while (settlersCoords[settlers.length - 1] == null) {
@ -330,7 +318,6 @@ public class Player {
int row = 0;
int col = 0;
int i = 0;
int maxrow = maxRow(villages);
int maxcol = maxCol(villages);
while (villagesCoords[villages.length-1] == null){

View File

@ -350,6 +350,13 @@ public class State {
if (currentPlayer >= numPlayers) currentPlayer = 0;
}
/**
* Start next phase
*/
public void nextPhase() {
currentPhase++;
}
/**
* Place a piece on the board. Uses current turn's player
* @param coord Coord coordinate to place piece
@ -400,28 +407,14 @@ public class State {
/**
* Score for that phase
* This does not automatically move to the next phase
*/
public void scorePhase() {
for (Player p: players) {
p.addScore(createScore(p.getPlayerID()));
}
if (getCurrentPhase() == 'E') {
currentPhase++;
}
else {
// Game over
finalScore();
currentPhase++;
}
}
/**
* Final score
*
*/
public void finalScore() {
}
/**
* Get score of player ID based on current phase and game state
* @param playerID int player ID base 0
@ -429,14 +422,11 @@ public class State {
*/
public int createScore(int playerID) {
int score = 0;
if (getCurrentPhase() == 'E') {
// Score exploration phase
score += scoreTotalIslands(playerID);
score += scoreMajorities(playerID);
}
else {
}
score += scoreTotalIslands(playerID);
score += scoreLinks(playerID);
score += scoreMajorities(playerID);
score += scoreResources(playerID);
score += scoreStatuettes(playerID);
return score;
}
@ -484,7 +474,7 @@ public class State {
for (Island island:islands){
int[] playerPieces = new int[getNumPlayers()];
for (int i = 0; i < getNumPlayers()-1; i++){
for (int i = 0; i < getNumPlayers(); i++){
playerPieces[i] = players[i].getNumPiecesOnIsland(island);
}
boolean ishighest = true;