first commit
This commit is contained in:
11
RollTheDice/RollTheDice.iml
Normal file
11
RollTheDice/RollTheDice.iml
Normal file
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
77
RollTheDice/src/NumberPicker.java
Normal file
77
RollTheDice/src/NumberPicker.java
Normal file
@@ -0,0 +1,77 @@
|
||||
import javax.swing.*;
|
||||
|
||||
public class NumberPicker {
|
||||
|
||||
private String input = null;
|
||||
|
||||
public int chooseNums(){
|
||||
String[] choices = {"100", "1.000", "10.000", "100.000", "1.000.000", "10.000.000", "100.000.000", "1.000.000.000", "Eigener Wert"};
|
||||
input = (String) JOptionPane.showInputDialog(null, "How many times do you want to roll the dice?",
|
||||
"Choose a Number", JOptionPane.QUESTION_MESSAGE, null, // Use default icon
|
||||
choices, // Initial choice
|
||||
"100.000");
|
||||
|
||||
switch (input){
|
||||
case "100":
|
||||
return 100;
|
||||
case "1.000":
|
||||
return 1_000;
|
||||
case "10.000":
|
||||
return 10_000;
|
||||
case "100.000":
|
||||
return 100_000;
|
||||
case "1.000.000":
|
||||
return 1_000_000;
|
||||
case "10.000.000":
|
||||
return 10_000_000;
|
||||
case "100.000.000":
|
||||
return 100_000_000;
|
||||
case "1.000.000.000":
|
||||
return 1_000_000_000;
|
||||
case "Choose Number yourself":
|
||||
int n = Integer.parseInt(JOptionPane.showInputDialog("Set your custom value"));
|
||||
return n;
|
||||
default:
|
||||
throw new IllegalStateException("Unexpected value: " + input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public int chooseDices(){
|
||||
String[] choices = {"1", "2", "3", "4", "5", "6", "7", "8","9","10", "Custom value"};
|
||||
input = (String) JOptionPane.showInputDialog(null, "How many dices do you want to use?",
|
||||
"Choose a Number", JOptionPane.QUESTION_MESSAGE, null, // Use default icon
|
||||
choices, // Initial choice
|
||||
"2");
|
||||
|
||||
switch (input){
|
||||
case "1":
|
||||
return 1;
|
||||
case "2":
|
||||
return 2;
|
||||
case "3":
|
||||
return 3;
|
||||
case "4":
|
||||
return 4;
|
||||
case "5":
|
||||
return 5;
|
||||
case "6":
|
||||
return 6;
|
||||
case "7":
|
||||
return 7;
|
||||
case "8":
|
||||
return 8;
|
||||
case "9":
|
||||
return 9;
|
||||
case "10":
|
||||
return 10;
|
||||
case "Choose Number yourself":
|
||||
int n = Integer.parseInt(JOptionPane.showInputDialog("Set your custom value"));
|
||||
return n;
|
||||
default:
|
||||
throw new IllegalStateException("Unexpected value: " + input);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
93
RollTheDice/src/Output.java
Normal file
93
RollTheDice/src/Output.java
Normal file
@@ -0,0 +1,93 @@
|
||||
import basis.*;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class Output {
|
||||
private Fenster frame;
|
||||
private Stift pen;
|
||||
private int values;
|
||||
private int nullSlots;
|
||||
private double YSCALE;
|
||||
private double MAXIMUM;
|
||||
private double GRIDSIZE;
|
||||
|
||||
public Output() {
|
||||
frame = new Fenster("Gamble Results",
|
||||
Hilfe.monitorBreite() / 2 - 400,
|
||||
Hilfe.monitorHoehe() / 2 - 400,
|
||||
800, 800);
|
||||
frame.setzeSichtbar(false);
|
||||
pen = new Stift();
|
||||
pen.setzeFarbe(Farbe.ROT);
|
||||
pen.setzeFuellMuster(Muster.GEFUELLT);
|
||||
}
|
||||
|
||||
public void setVisibility(boolean visible) {
|
||||
frame.setzeSichtbar(visible);
|
||||
}
|
||||
|
||||
public int[] trimArray(int[] array) {
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] != 0) {
|
||||
values++;
|
||||
} else if (values == 0) {
|
||||
nullSlots++;
|
||||
}
|
||||
}
|
||||
int[] trimmedArray = new int[values];
|
||||
for (int i = 0; i < trimmedArray.length; i++) {
|
||||
trimmedArray[i] = array[i + nullSlots];
|
||||
}
|
||||
return trimmedArray;
|
||||
}
|
||||
|
||||
private void makeScala(int[] array, long total) {
|
||||
int max = 0;
|
||||
for (int value : array
|
||||
) {
|
||||
if (max < value) {
|
||||
max = value;
|
||||
}
|
||||
}
|
||||
|
||||
MAXIMUM = max;
|
||||
|
||||
double average = max / array.length;
|
||||
YSCALE = frame.hoehe() / average;
|
||||
|
||||
GRIDSIZE = (frame.breite()-50) / array.length;
|
||||
|
||||
double zahl = 1;
|
||||
for (double i = 0; i < 0.8; i+=0.1) {
|
||||
TextFeld scalaY = new TextFeld(0, frame.hoehe()*i,50,20);
|
||||
scalaY.setzeHintergrundFarbe(Farbe.DURCHSICHTIG);
|
||||
scalaY.setzeEditierbar(false);
|
||||
scalaY.entferneRand();
|
||||
DecimalFormat df = new DecimalFormat("0");
|
||||
String partNumber = df.format(max*zahl);
|
||||
zahl-=0.1;
|
||||
Hilfe.pause(10);
|
||||
scalaY.setzeText(partNumber);
|
||||
}
|
||||
}
|
||||
|
||||
public void drawGraph(int[] array, long total) {
|
||||
makeScala(array, total);
|
||||
Hilfe.kurzePause();
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
double yValue = ((double) array[i] / MAXIMUM) * (frame.hoehe() - 100);
|
||||
pen.rechteck(50+GRIDSIZE * i + GRIDSIZE / 4, frame.breite() - yValue - GRIDSIZE, GRIDSIZE / 2, yValue);
|
||||
Hilfe.kurzePause();
|
||||
|
||||
Hilfe.kurzePause();
|
||||
ZahlenFeld scalaX = new ZahlenFeld(50+GRIDSIZE * i + GRIDSIZE / 2 - 10, frame.breite() - GRIDSIZE, 20, 20);
|
||||
scalaX.setzeGroesse(GRIDSIZE, GRIDSIZE);
|
||||
scalaX.setzeHintergrundFarbe(Farbe.DURCHSICHTIG);
|
||||
scalaX.setzeEditierbar(false);
|
||||
scalaX.entferneRand();
|
||||
scalaX.setzeZahl(i + nullSlots + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
134
RollTheDice/src/rollThem.java
Normal file
134
RollTheDice/src/rollThem.java
Normal file
@@ -0,0 +1,134 @@
|
||||
import basis.Hilfe;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
public class rollThem {
|
||||
|
||||
NumberPicker numberPicker;
|
||||
Output output;
|
||||
|
||||
int values[];
|
||||
int pairValues[];
|
||||
int times;
|
||||
int dices;
|
||||
int pairs;
|
||||
|
||||
public rollThem(){
|
||||
numberPicker = new NumberPicker();
|
||||
output = new Output();
|
||||
pairValues = new int[6];
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
rollThem roll;
|
||||
roll = new rollThem();
|
||||
roll.rollIt();
|
||||
}
|
||||
|
||||
private int scanNums() {
|
||||
try {
|
||||
this.times = numberPicker.chooseNums();
|
||||
this.dices = numberPicker.chooseDices();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private String pairProbsForOne(int pairs){
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
return df.format(((double)pairs/(double)times)*100) + "%";
|
||||
}
|
||||
|
||||
private String pairProbsForAll(){
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
return df.format(((double)pairs/(double)times)*100) + "%";
|
||||
}
|
||||
|
||||
private boolean allEqual(int[] array){
|
||||
int num1 = array[0];
|
||||
for (int i = 0; i < array.length; i++){
|
||||
if (num1 == (array[i])){
|
||||
continue;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pairValues[num1-1] += 1;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void startRolling(int times, int dices) {
|
||||
if (dices == 0 || times == 0) {
|
||||
System.err.println("Value of times or dices cant be 0");
|
||||
System.exit(1);
|
||||
}
|
||||
this.values = new int[dices * 6];
|
||||
for (int i = 0; i < times; i++) {
|
||||
int[] tempVals = new int[dices];
|
||||
int total = 0;
|
||||
for (int j = 0; j < dices; j++) {
|
||||
int value = roll();
|
||||
tempVals[j] = value;
|
||||
total += value;
|
||||
}
|
||||
if (allEqual(tempVals)){
|
||||
pairs++;
|
||||
}
|
||||
values[total - 1] += 1;
|
||||
}
|
||||
print();
|
||||
}
|
||||
|
||||
private void print(){
|
||||
System.out.println("---------- RESULTS -----------");
|
||||
System.out.println("NUMBERS:");
|
||||
for (int i = dices - 1; i < values.length; i++) {
|
||||
System.out.printf("%d: %s | %d%n", i + 1, probabilities()[i], values[i]);
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("PAIRS:");
|
||||
for (int i = 0; i < pairValues.length; i++){
|
||||
System.out.printf("%d: %d | %s%n", i+1, pairValues[i], pairProbsForOne(pairValues[i]));
|
||||
}
|
||||
System.out.println();
|
||||
System.out.println("STATS:");
|
||||
System.out.printf("Total Pairs: %d | %s%n", pairs, pairProbsForAll());
|
||||
System.out.printf("EyeSum: %d%n", totalEyeSum());
|
||||
System.out.printf("Average Number: %s", averageNum());
|
||||
output.setVisibility(true);
|
||||
output.drawGraph(output.trimArray(values), totalEyeSum());
|
||||
}
|
||||
|
||||
private long totalEyeSum() {
|
||||
long total = 0;
|
||||
for (int i = 0; i < values.length; i++) {
|
||||
total += values[i] * i + i;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
private String averageNum() {
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
return df.format((double) totalEyeSum() / (double) times);
|
||||
}
|
||||
|
||||
private String[] probabilities() {
|
||||
DecimalFormat df = new DecimalFormat("0.00");
|
||||
String[] probs = new String[values.length];
|
||||
for (int i = 0; i < probs.length; i++) {
|
||||
probs[i] = df.format(((double) values[i] / (double) times) * 100) + "%";
|
||||
}
|
||||
return probs;
|
||||
}
|
||||
|
||||
private int roll() {
|
||||
return Hilfe.zufall(1, 6);
|
||||
}
|
||||
|
||||
public void rollIt() {
|
||||
scanNums();
|
||||
startRolling(times, dices);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user