task6: Updated random generator and test
This commit is contained in:
@@ -1,10 +1,7 @@
|
|||||||
package comp1110.ass2;
|
package comp1110.ass2;
|
||||||
|
|
||||||
import java.text.NumberFormat;
|
import java.text.NumberFormat;
|
||||||
import java.util.Arrays;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.lang.*;
|
import java.lang.*;
|
||||||
|
|
||||||
public class BlueLagoon {
|
public class BlueLagoon {
|
||||||
@@ -101,8 +98,6 @@ public class BlueLagoon {
|
|||||||
* @return a string of the game state with resources randomly distributed
|
* @return a string of the game state with resources randomly distributed
|
||||||
*/
|
*/
|
||||||
public static String distributeResources(String stateString){
|
public static String distributeResources(String stateString){
|
||||||
// For testing purposes
|
|
||||||
|
|
||||||
// Check if the stateString is well-formed
|
// Check if the stateString is well-formed
|
||||||
if (!isStateStringWellFormed(stateString)) return stateString;
|
if (!isStateStringWellFormed(stateString)) return stateString;
|
||||||
|
|
||||||
@@ -112,21 +107,32 @@ public class BlueLagoon {
|
|||||||
// Split the stone circles into an array of cords
|
// Split the stone circles into an array of cords
|
||||||
String[] stoneCircleCords = stoneCircles.split(" ");
|
String[] stoneCircleCords = stoneCircles.split(" ");
|
||||||
|
|
||||||
// Create a random object to shuffle the stone circles
|
// Check if there are 32 stone circles
|
||||||
Random rand = new Random();
|
if (stoneCircleCords.length != 32) return stateString;
|
||||||
|
|
||||||
// For each stone circle
|
// Create a random object and an arrays and list to shuffle the stone circles
|
||||||
for (int i = 0; i < stoneCircleCords.length; i++) {
|
Random rand = new Random();
|
||||||
// Get a random index to swap with
|
String[] stoneCircleRandom = new String[32];
|
||||||
int randomIndexToSwap = rand.nextInt(stoneCircleCords.length);
|
List<String> usedCords = new ArrayList<String>();
|
||||||
// Swap the stone circles
|
// Generate random array
|
||||||
String temp = stoneCircleCords[randomIndexToSwap];
|
for (int i = 0; i < 32; i++) {
|
||||||
stoneCircleCords[randomIndexToSwap] = stoneCircleCords[i];
|
// For 0-31 generate a random cord from the stone circle array and check if it has been used
|
||||||
stoneCircleCords[i] = temp;
|
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
|
||||||
|
stoneCircleRandom[i] = stoneCircleCords[randomIndex];
|
||||||
|
usedCords.add(stoneCircleCords[randomIndex]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there isn't 32 stone circles
|
// Check if there isn't 32 stone circles
|
||||||
if (stoneCircleCords.length != 32) return stateString;
|
|
||||||
|
|
||||||
// Create a string to store the new resources state
|
// Create a string to store the new resources state
|
||||||
String newResourcesState = "r";
|
String newResourcesState = "r";
|
||||||
@@ -141,7 +147,7 @@ public class BlueLagoon {
|
|||||||
newResourcesState += " " + r;
|
newResourcesState += " " + r;
|
||||||
// Assign 6 to a stone circle
|
// Assign 6 to a stone circle
|
||||||
for (int i = 0; i < 6; i++){
|
for (int i = 0; i < 6; i++){
|
||||||
newResourcesState += " " + stoneCircleCords[numSorted];
|
newResourcesState += " " + stoneCircleRandom[numSorted];
|
||||||
numSorted++;
|
numSorted++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -149,7 +155,7 @@ public class BlueLagoon {
|
|||||||
// Assign 8 statuettes to a stone circle
|
// Assign 8 statuettes to a stone circle
|
||||||
newResourcesState += " S";
|
newResourcesState += " S";
|
||||||
for (int i = 0; i < 8; i++){
|
for (int i = 0; i < 8; i++){
|
||||||
newResourcesState += " " + stoneCircleCords[numSorted];
|
newResourcesState += " " + stoneCircleRandom[numSorted];
|
||||||
numSorted++;
|
numSorted++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,6 +85,7 @@ public class DistributeResourcesTest {
|
|||||||
|
|
||||||
private static void testSufficientlyRandom(HashMap<String, HashMap<Character, Integer>> distribution){
|
private static void testSufficientlyRandom(HashMap<String, HashMap<Character, Integer>> distribution){
|
||||||
for (String coordinate : distribution.keySet()){
|
for (String coordinate : distribution.keySet()){
|
||||||
|
System.out.println(distribution.get(coordinate));
|
||||||
int total = 0;
|
int total = 0;
|
||||||
for(Character resource : resourceCharacters){
|
for(Character resource : resourceCharacters){
|
||||||
int amount = distribution.get(coordinate).get(resource);
|
int amount = distribution.get(coordinate).get(resource);
|
||||||
|
|||||||
Reference in New Issue
Block a user