Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package calc;
- import java.util.concurrent.ThreadLocalRandom;
- public abstract class Dice {
- public static int roll(String s) {
- //1d8+20
- //Teilt den String "1d8+20" in ein Array mit "1" und "8+20" ein
- String[] arr = s.split("(d)");
- //teilt den zweiten Teil des Arrays "8+20" in ein neues Array mit "8", "+" und "20" ein
- String diceMaxOperatorBonus = arr[1];
- String[] diceMaxOperatorBonusSplit = splitKeep(diceMaxOperatorBonus);
- //Speicherung aller einzelnen Werte in Variablen
- int amountRoll = Integer.parseInt(arr[0]);
- int diceMax = Integer.parseInt(diceMaxOperatorBonusSplit[0]);
- char operator = '.';
- int bonus = 0;
- try {
- operator = diceMaxOperatorBonusSplit[1].charAt(0);
- bonus = Integer.parseInt(diceMaxOperatorBonusSplit[2]);
- } catch(Exception e) {}
- /*
- * generiert die eine/mehrere Zufallszahlen mit diceMax als Maximum und amountRoll der Anzahl
- * der Würfe und addiert Speicherung der Summe in sum
- * Gibt aus, welche Würfel welche Zahl gewürfelt hat
- */
- int sum = 0;
- for(int i = 0; i < amountRoll; i++) {
- int randomNum = ThreadLocalRandom.current().nextInt(1, diceMax+1);
- System.out.println("Zufallszahl " + (i+1) + ": " + randomNum);
- sum += randomNum;
- }
- System.out.println("Bonus: " + bonus);
- //verrechnet den Bonus mit der Summe
- int result = math(sum, operator, bonus);
- System.out.println("Gewürfelte Zahl: " + result);
- return result;
- }
- public static String[] splitKeep(String s) {
- //Gewöhnliche Split-Funktion, bei dem die Selektion im Array gespeichert wird
- return s.split("((?<=[+,-])|(?=[+,-]))");
- }
- public static int math(int sum, char operator, int bonus) {
- switch(operator) {
- case '+':
- sum += bonus;
- break;
- case '-':
- sum -= bonus;
- break;
- case '*':
- sum *= bonus;
- break;
- case '/':
- sum /= bonus;
- break;
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement