BlueLagoon: Added isStateStringWellFormed

This commit is contained in:
Nathan Woodburn 2023-03-23 12:22:32 +11:00
parent 1984883997
commit dc45c15788
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -26,7 +26,78 @@ public class BlueLagoon {
* @return true if stateString is well-formed and false otherwise
*/
public static boolean isStateStringWellFormed(String stateString){
return false; // FIXME Task 3
// Check if the string is empty
if (stateString == ""){
return false;
}
// Check if the last character is a semicolon
if (stateString.charAt(stateString.length()-1) != ';'){
return false;
}
// Split string by semicolon
String[] splitStateString = stateString.split("; ");
for (String sub: splitStateString){
switch (sub.substring(0,1)) {
case "a": {
// Check if the string is well formed for game arrangement
// string should be a [0-9] [0-9] with each number being 1 or 2 digits
if (!sub.matches("a \\d{1,2} \\d{1,2}")) {
return false;
}
break;
}
case "c": {
// Check if the string is well formed for current state
// string should be c [0-9] E/S or c [0-9] E/S
if (!sub.matches("c \\d{1,2} [E|S]")) {
return false;
}
break;
}
case "i": {
// Check if the string is well formed for island
if (!sub.matches("i \\d{1,2} (\\d{1,2},\\d{1,2} )*\\d{1,2},\\d{1,2}")) {
return false;
}
break;
}
case "s": {
// Check if the string is well formed for stones
if (!sub.matches("s (\\d{1,2},\\d{1,2} )+\\d{1,2},\\d{1,2}")) {
return false;
}
break;
}
case "r":{
// Check if the string is well formed for the unclaimed resources and statuettes
if (!sub.matches("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})*")){
return false;
}
break;
}
case "p": {
// Check if the string is well formed for the player info
if (!sub.matches("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} ?)*)?;?")){
return false;
}
break;
}
default: {
// If the string does not start with any of the above characters, it is not well formed
return false;
}
}
}
return true;
}
/**