Last active
December 10, 2015 22:58
-
-
Save rokon12/4505768 to your computer and use it in GitHub Desktop.
The Quick And Dirty RPG
This file contains hidden or 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.Arrays; | |
| import java.util.Collections; | |
| import java.util.List; | |
| import java.util.Random; | |
| /** | |
| * Created with IntelliJ IDEA. | |
| * User: Bazlur Rahman Rokon | |
| * Date: 1/10/13 | |
| * Time: 11:28 PM | |
| */ | |
| public class RPG { | |
| public static String generate(String allowedCharacters, | |
| boolean mustHaveUppercase, boolean mustHaveDigit, | |
| int minLength, int maxLength) { | |
| if (minLength > maxLength || minLength < 0 || maxLength < 2) return null; | |
| if (allowedCharacters.isEmpty()) return null; | |
| String exclusion = "I1/\\\\"; | |
| String characters = exclude(allowedCharacters, exclusion); | |
| String chars = removeDuplicateUpercaseLowerCaseSameCharacter(characters); | |
| String rp = ""; | |
| Random random = new Random(); | |
| int length = random.nextInt(maxLength) + minLength; | |
| String upperCaseLetter = findUpperCaseLetters(allowedCharacters); | |
| String digits = findDigits(allowedCharacters); | |
| if (!mustHaveDigit && !mustHaveUppercase) | |
| for (int i = 0; i < length; i++) { | |
| rp += characters.charAt(random.nextInt(chars.length())); | |
| } | |
| else if (mustHaveDigit & !mustHaveUppercase) { | |
| int a = random.nextInt(chars.length() - (length * 2 / 3)); | |
| if (a == 0) ++a; | |
| for (int i = 0; i < a; i++) { | |
| rp += digits.charAt(random.nextInt(digits.length())); | |
| } | |
| String rest = exclude(chars, digits); | |
| int b = chars.length() - a; | |
| for (int i = 0; i < b; i++) { | |
| rp += rest.charAt(random.nextInt(rest.length())); | |
| } | |
| } else if (!mustHaveDigit & mustHaveUppercase) { | |
| int a = random.nextInt(chars.length() - (length * 2 / 3)); | |
| if (a == 0) ++a; | |
| for (int i = 0; i < a; i++) { | |
| rp += upperCaseLetter.charAt(random.nextInt(upperCaseLetter.length())); | |
| } | |
| String rest = exclude(chars, upperCaseLetter); | |
| int b = chars.length() - a; | |
| for (int i = 0; i < b; i++) { | |
| rp += rest.charAt(random.nextInt(rest.length())); | |
| } | |
| } else if (mustHaveDigit & mustHaveUppercase) { | |
| int a = random.nextInt(length - (length * 2 / 3)); | |
| if (a == 0) ++a; | |
| for (int i = 0; i < a; i++) { | |
| rp += digits.charAt(random.nextInt(digits.length())); | |
| } | |
| int b = random.nextInt(length - 2 * a); | |
| if (b == 0) ++b; | |
| for (int i = 0; i < b; i++) { | |
| rp += upperCaseLetter.charAt(random.nextInt(upperCaseLetter.length())); | |
| } | |
| String rest = exclude(chars, upperCaseLetter); | |
| rest = exclude(rest, digits); | |
| int c = length - (a + b); | |
| for (int i = 0; i < c; i++) { | |
| rp += rest.charAt(random.nextInt(rest.length())); | |
| } | |
| } | |
| String pass = removeDuplicateUpercaseLowerCaseSameCharacter(rp); | |
| return shuffle(pass); | |
| } | |
| public static String exclude(String allowedCharacters, String exclusion) { | |
| return allowedCharacters.replaceAll("[" + exclusion + "]", ""); | |
| } | |
| private static String findUpperCaseLetters(String allowedCharacters) { | |
| String letters = ""; | |
| for (int i = 0; i < allowedCharacters.length(); i++) { | |
| if (Character.isUpperCase(allowedCharacters.charAt(i))) { | |
| letters += allowedCharacters.charAt(i); | |
| } | |
| } | |
| return letters; | |
| } | |
| private static String findDigits(String allowedCharacters) { | |
| String letters = ""; | |
| for (int i = 0; i < allowedCharacters.length(); i++) { | |
| if (Character.isDigit(allowedCharacters.charAt(i))) { | |
| letters += allowedCharacters.charAt(i); | |
| } | |
| } | |
| return letters; | |
| } | |
| public static void main(String[] args) { | |
| String allowedCharacters = "weakPasSWorD1234$*"; | |
| boolean mustHaveUppercase = false; | |
| boolean mustHaveDigit = false; | |
| int minLength = 3; | |
| int maxLength = 20; | |
| String randomPassword = generate(allowedCharacters, | |
| mustHaveUppercase, mustHaveDigit, minLength, | |
| maxLength); | |
| System.out.println("Random password: " + randomPassword); | |
| } | |
| public static String removeDuplicateUpercaseLowerCaseSameCharacter(String s) { | |
| if (s.length() <= 1) return s; | |
| if (s.substring(0, s.length() - 1).toLowerCase().equals(s.substring(s.length() - 1, s.length()).toLowerCase())) | |
| return removeDuplicateUpercaseLowerCaseSameCharacter(s.substring(0, s.length() - 1)); | |
| else | |
| return removeDuplicateUpercaseLowerCaseSameCharacter(s.substring(0, s.length() - 1)) + s.substring(s.length() - 1, s.length()); | |
| } | |
| public static String shuffle(String string) { | |
| List<String> letters = Arrays.asList(string.split("")); | |
| Collections.shuffle(letters); | |
| String shuffled = ""; | |
| for (String letter : letters) { | |
| shuffled += letter; | |
| } | |
| return shuffled; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment