Task 4 made shorter

This commit is contained in:
Immanuel Alvaro Bhirawa 2023-03-24 11:46:44 +11:00
parent b2dc79dec2
commit 822f040609

View File

@ -115,37 +115,8 @@ public class BlueLagoon {
*
* coordinate = row , col (i.e. "0,1" means row 0 col 1)
*/
public static boolean isMoveStringWellFormed(String moveString) {
// Split the initial string onto 2 parts, before " " and after " "
// The string will be split onto pieceType and Coords parts
String[] parts = moveString.split(" ");
// if there's more than one " " after a split return false
if (parts.length != 2) return false;
// Before " " (i.e. checking the pieceType. pieceType must be "S" or "T")
String pieceType = parts[0];
if (!pieceType.equals("S") && !pieceType.equals("T")) return false;
// After " ", (checking the coords)
// The coords array will be split onto checking the row (before ",") and
// col cords (after ",")
String[] coords = parts[1].split(",");
// Check if there's multiples ","
if (coords.length != 2) return false;
// Checks whether the row cords are digits or not and whether the digits
// fit into the interval ( i.e. row and coll cannot be below 0 )
// will catch an error and return false if either row coords or col coords are not digits
try {
int row = Integer.parseInt(coords[0]);
int col = Integer.parseInt(coords[1]);
if (row < 0 || col < 0) return false;
} catch (NumberFormatException e) {
return false;
}
public static boolean isMoveStringWellFormed(String moveString){
if(!moveString.matches("[ST] \\d{1,2},\\d{1,2}"))return false;
return true;
}