Code: Select all
public class DiceController {
@FXML
Label rollResult, separatedDice;
@FXML
TextField diceNotationField;
@FXML
Button diceRollButton;
public void roll() {
try {
// get text from field
String diceNotation = diceNotationField.getText();
// Use a regex tp detect dice notation
Pattern regex = Pattern.compile("(\\d+)?d(\\d+)(?:([\\+\\*\\/\\-])(\\d+))?");
Matcher m = regex.matcher(diceNotation);
String diceQuantity = "";
String diceSize = "";
String diceSign = "";
String diceModifier = "";
// Call Random to create a random number
Random rand = new Random();
// if the dice quantity is null is set to 1
if (diceQuantity == null || diceQuantity.isEmpty()) {
diceQuantity = "1";
}
// regex matcher check
if (!m.matches()) {
rollResult.setText("Intruduce a valid dice notation");
} else if (m.matches()) {
diceQuantity = m.group(1);
diceSize = m.group(2);
// Create an array to store the result of each dice
int diceResultArray[] = new int[Integer.parseInt(diceQuantity)];
// this variable will generate a random number rolling the dice
int randomResultInDice = 0;
// this variable stores the total result
int total = 0;
for (int i = 0; i < Integer.parseInt(diceQuantity); i++) {
// generates a random number on the dice,
randomResultInDice = rand.nextInt(1, Integer.parseInt(diceSize) + 1);
// stores the separate values of each dice's results in an array
diceResultArray[i] = randomResultInDice;
// sums the result of the separate dice
total += randomResultInDice;
}
// if there's no sign the string stays empty, else, the regex group is
// recognized and the corresponding operation is performed
if (diceSign == null || diceSign.isEmpty() && diceModifier == null || diceModifier.isEmpty()) {
diceSign = "";
diceModifier = "";
} else {
diceSign = m.group(3);
diceModifier = m.group(4);
// stores the total result of the dice plus the operation corresponding to the
// modifier
int totalMod = 0;
switch (diceSign) {
case "+":
totalMod = total + Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "-":
totalMod = total - Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "*":
totalMod = total * Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
case "/":
if (Integer.parseInt(diceModifier) != 0) {
totalMod = total / Integer.parseInt(diceModifier);
rollResult.setText(String.valueOf(totalMod));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
} else {
totalMod = total;
rollResult.setText(String.valueOf(total));
}
break;
default:
rollResult.setText(String.valueOf(total));
separatedDice.setText(String.valueOf(Arrays.toString(diceResultArray)));
break;
}
}
}
} catch (Exception e) {
rollResult.setText(e.toString());
System.out.println(e);
}
}
}
Code: Select all
Code: Select all
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("mainView.fxml"));
Scene scene = new Scene(root);
primaryStage.setTitle("TTRPG Toolbox");
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Code: Select all
Mobile version