Merge branch 'fix-distribute'

This commit is contained in:
Nathan Woodburn 2023-03-27 16:22:25 +11:00
commit 69adaa296a
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -1,11 +1,10 @@
package comp1110.ass2;
import java.sql.Time;
import java.text.NumberFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;
import java.util.*;
import java.lang.*;
import java.util.stream.Stream;
public class BlueLagoon {
// The Game Strings for five maps have been created for you.
@ -41,13 +40,10 @@ public class BlueLagoon {
matchArray[1] = "c \\d{1,2} [E|S]; ";
// For the islandStatement use the following regex string
matchArray[2] = "(i \\d{1,2} (\\d{1,2},\\d{1,2} )*\\d{1,2},\\d{1,2}; )*";
// For the stonesStatement use the following regex string
matchArray[3] = "(s (\\d{1,2},\\d{1,2} )+\\d{1,2},\\d{1,2}; )";
// For the resources and statuettes use the following regex string
matchArray[4] = "r C (\\d{1,2},\\d{1,2} )*B (\\d{1,2},\\d{1,2} )*W (\\d{1,2},\\d{1,2} )*P (\\d{1,2},\\d{1,2} )*S( \\d{1,2},\\d{1,2})*;";
// For the playersStatement use the following regex string
matchArray[5] = "( p \\d \\d{1,3} \\d{1,2} \\d{1,2} \\d{1,2} \\d{1,2} \\d{1,2} S (\\d{1,2},\\d{1,2} )*T( (\\d{1,2},\\d{1,2} ?)*)?;)*";
@ -56,6 +52,7 @@ public class BlueLagoon {
for (String match:matchArray) {
matchString += match;
}
// Check if the state string matches the regex string
if (!stateString.matches(matchString)) return false;
@ -103,53 +100,83 @@ public class BlueLagoon {
* @return a string of the game state with resources randomly distributed
*/
public static String distributeResources(String stateString){
// For testing purposes
// System.out.println(stateString);
// 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
// Split the stone circles into an array of cords
String[] stoneCircleCords = stoneCircles.split(" ");
// Check if the stone circles are 32
// 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 a random number generator to randomly assign the resources
Random random = new Random();
// Randomly assign the resources
// 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 random stone circle
// Assign 6 to a stone circle
for (int i = 0; i < 6; i++){
int randomIndex = random.nextInt(32);
newResourcesState += " " + stoneCircleCords[randomIndex];
newResourcesState += " " + stoneCircleRandom[numSorted];
numSorted++;
}
}
// Assign 8 statuettes to a random stone circle
// Assign 8 statuettes to a stone circle
newResourcesState += " S";
for (int i = 0; i < 8; i++){
int randomIndex = random.nextInt(32);
newResourcesState += " " + stoneCircleCords[randomIndex];
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; //! Check this before due date
return stateString;
}
/**