From cb1a475bd5a2993425ba4b5233ab83a2f70a54bc Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 23 Mar 2023 17:41:13 +1100 Subject: [PATCH 1/2] task6: Initial add --- src/comp1110/ass2/BlueLagoon.java | 52 ++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/comp1110/ass2/BlueLagoon.java b/src/comp1110/ass2/BlueLagoon.java index 01b62a0..d8c5c34 100644 --- a/src/comp1110/ass2/BlueLagoon.java +++ b/src/comp1110/ass2/BlueLagoon.java @@ -1,7 +1,9 @@ package comp1110.ass2; import java.text.NumberFormat; +import java.util.Arrays; import java.util.HashSet; +import java.util.Random; import java.util.Set; import java.lang.*; @@ -166,7 +168,55 @@ public class BlueLagoon { * @return a string of the game state with resources randomly distributed */ public static String distributeResources(String stateString){ - return ""; // FIXME Task 6 + // 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 + + String[] stoneCircleCords = stoneCircles.split(" "); + + // Check if the stone circles are 32 + if (stoneCircleCords.length != 32) return stateString; + + // 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 + char[] resources = {'C', 'B', 'W', 'P'}; + + + // For each resource type + for (char r:resources){ + newResourcesState += " " + r; + // Assign 6 to a random stone circle + for (int i = 0; i < 6; i++){ + int randomIndex = random.nextInt(32); + newResourcesState += " " + stoneCircleCords[randomIndex]; + } + } + + // Assign 8 statuettes to a random stone circle + newResourcesState += " S"; + for (int i = 0; i < 8; i++){ + int randomIndex = random.nextInt(32); + newResourcesState += " " + stoneCircleCords[randomIndex]; + } + +// System.out.println(newResourcesState); + + // Replace the old resources state with the new one + stateString = stateString.replace("r C B W P S", newResourcesState); + + + return stateString; // FIXME Task 6 } /** From ff4b5d3e8d1978a36864d117ff730c282fdcdc94 Mon Sep 17 00:00:00 2001 From: Nathan Woodburn Date: Thu, 23 Mar 2023 17:51:30 +1100 Subject: [PATCH 2/2] added: Check message --- src/comp1110/ass2/BlueLagoon.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/comp1110/ass2/BlueLagoon.java b/src/comp1110/ass2/BlueLagoon.java index d8c5c34..0d7604a 100644 --- a/src/comp1110/ass2/BlueLagoon.java +++ b/src/comp1110/ass2/BlueLagoon.java @@ -210,13 +210,11 @@ public class BlueLagoon { newResourcesState += " " + stoneCircleCords[randomIndex]; } -// System.out.println(newResourcesState); - // Replace the old resources state with the new one stateString = stateString.replace("r C B W P S", newResourcesState); - return stateString; // FIXME Task 6 + return stateString; //! Check this before due date } /**