Last active
September 5, 2022 22:00
-
-
Save speters33w/8de384ed3488db01dbd8cdc09d236208 to your computer and use it in GitHub Desktop.
Security - My solution to the SoloLearn "Security" code coach, allows for more than one thief
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Scanner; | |
public class Security { | |
public static void main(String[] args) { | |
//runServerTests(); | |
randomFloor(); | |
} | |
/** | |
* In a casino, given one money location, any number of thieves, and any number of guards, | |
* returns "ALARM" if there is no guard between a thief and the money. | |
* @param floorLayout A string of characters that includes $ (money), T (thief), and G (guard), | |
* that represents the layout of the casino floor. | |
* Space on the casino floor that is not occupied by either money, the thief, or a guard | |
* is represented by the character x.<br> | |
* example: xxxxx$xxGxxxT | |
* @return A string that says 'ALARM' if there is no guard between the money and a thief, | |
* or 'quiet' if the money is safe. | |
* @throws IllegalArgumentException if the input string is invalid. | |
*/ | |
public static String security(String floorLayout) throws IllegalArgumentException { | |
checkInput(floorLayout); | |
int money = floorLayout.indexOf('$'); | |
int thief = floorLayout.indexOf('T'); | |
String[] denOfThieves = {"G","G"}; //initializes to "G"; If there is no thief method will return "quiet". | |
if ((thief < money) && (thief != -1)) { | |
thief = floorLayout.substring(0,money).lastIndexOf('T'); | |
denOfThieves[0] = floorLayout.substring(thief, money); | |
} | |
if(floorLayout.substring(money).contains("T")){ | |
thief = floorLayout.substring(money).indexOf('T'); | |
denOfThieves[1] = floorLayout.substring(money,money + thief); | |
} | |
for(String thieve : denOfThieves){ | |
if (!thieve.contains("G")) { | |
return "ALARM"; | |
} | |
} | |
return "quiet"; | |
} | |
/** | |
* Checks if casino floor layout input is valid for this program | |
* @param floorLayout A string of characters that includes $ (money), T (thief), and G (guard). | |
*/ | |
private static void checkInput(String floorLayout){ | |
if(!floorLayout.matches("^[xGT$]+$")) | |
throw new IllegalArgumentException("Invalid casino floor layout"); | |
int money = floorLayout.indexOf('$'); | |
if ((money == -1) || (floorLayout.substring(money+1).contains("$"))) | |
throw new IllegalArgumentException("Too much or too little money to guard"); | |
} | |
/** | |
* Creates a random floor layout and uses security() to see if the money is in danger, | |
* then outputs the layout and result to the console. | |
*/ | |
static void randomFloor(){ | |
Random random = new Random(); | |
StringBuilder floorLayout = new StringBuilder(); | |
int floorLayoutLength = random.nextInt(11) + 11; | |
//System.out.println(floorLayoutLength); | |
for(int i = 1; i <= floorLayoutLength; i++){ | |
int thiefOrGuard = random.nextInt(11) + 1; | |
if(thiefOrGuard < 2) { | |
floorLayout.append("G"); | |
} else if(thiefOrGuard == 11) { | |
floorLayout.append("T"); | |
} else floorLayout.append("x"); | |
} | |
int theMoney = random.nextInt(floorLayoutLength-1); | |
floorLayout.replace(theMoney,theMoney,"$"); | |
System.out.println(floorLayout); | |
System.out.println(security(String.valueOf(floorLayout))); | |
} | |
/** | |
* Uses Scanner to accept input from test case servers and | |
* uses System.out to output the result of security(input). | |
*/ | |
static void runServerTests(){ | |
Scanner scanner = new Scanner(System.in); | |
String floorLayout = scanner.nextLine(); | |
System.out.println(security(floorLayout)); | |
} | |
/** | |
* Runs a series of String literal test cases on security(). | |
*/ | |
static void testCases(){ | |
//TEST CASES | |
System.out.print("\nTest Case: " + "xxTxx$xxGxx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("xxTxx$xxGxx")); //ALARM | |
System.out.print("\n\nTest Case: " + "xxGxx$xxTxx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("xxGxx$xxTxx")); //ALARM | |
System.out.print("\n\nTest Case: " + "xTxGx$xTxGx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("xTxGx$xTxGx")); //ALARM | |
System.out.print("\n\nTest Case: " + "x$xTxxGxxTx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("x$xTxxGxxTx")); //ALARM | |
System.out.print("\n\nTest Case: " + "xTxxGxxTx$x" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("xTxxGxxTx$x")); //ALARM | |
System.out.print("\n\nTest Case: " + "GTxxx$xxxGx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("GTxxx$xxxGx")); //ALARM | |
System.out.print("\n\nTest Case: " + "xxxxxxxGTx$" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("xxxxxxxGTx$")); //ALARM | |
System.out.print("\n\nTest Case: " + "$xxxxxxTGxx" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("$xxxxxxTGxx")); //ALARM | |
System.out.print("\n\nTest Case: " + "Txxxxxxxxx$" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("Txxxxxxxxx$")); //ALARM | |
System.out.print("\n\nTest Case: " + "$xxxxxxxxxT" + "\nExpect: " + "ALARM" + "\nActual: "); | |
System.out.print(security("$xxxxxxxxxT")); //ALARM | |
System.out.print("\n\nTest Case: " + "xxTxxGxx$xx" + "\nExpect: " + "quiet" + "\nActual: "); | |
System.out.print(security("xxTxxGxx$xx")); //quiet | |
System.out.print("\n\nTest Case: " + "xx$xxGxxTxx" + "\nExpect: " + "quiet" + "\nActual: "); | |
System.out.print(security("xx$xxGxxTxx")); //quiet | |
System.out.print("\n\nTest Case: " + "xTxGx$xGxTx" + "\nExpect: " + "quiet" + "\nActual: "); | |
System.out.print(security("xTxGx$xGxTx")); //quiet | |
System.out.print("\n\nTest Case: " + "x$xGxxGxxTx" + "\nExpect: " + "quiet" + "\nActual: "); | |
System.out.print(security("x$xGxxGxxTx")); //quiet | |
System.out.print("\n\nTest Case: " + "xxGxx$xxGxx" + "\nExpect: " + "quiet" + "\nActual: "); | |
System.out.print(security("xxGxx$xxGxx")); //quiet | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment