diff options
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java')
| -rw-r--r-- | src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java | 71 |
1 files changed, 24 insertions, 47 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java index 210e4ae..1b42f69 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java @@ -12,11 +12,7 @@ package de.fhswf.in.inf.java1.aufgabe7; public final class Simulation { - private static Spieler[] spieler; - - private static Spiel spiel; - - private static int[] spielerProp; + private static final int VERSUCHE = 100000; /** * Prevents instantiation of the utility class. @@ -27,42 +23,26 @@ public final class Simulation } /** - * Initializes everything. - * - */ - private static void aufsetzen() - { - spieler = new Spieler[3]; - - spieler[0] = new GleichSpieler(); - spieler[1] = new WechselSpieler(); - spieler[2] = new Spieler(true, 50); - - spiel = new Spiel(); - - } - - /** * Makes anzahl games. * * @param anzahl * How often the game will be run. + * @return Gewonnene Spiele. */ - private static void nDurchlaufeAusfuehren(int anzahl) + private static int nDurchlaufeAusfuehren(Spiel spiel, Spieler spieler, + int anzahl) { - aufsetzen(); - boolean[][] erfolgreich = new boolean[spieler.length][anzahl]; - for (int i = 0; i < spieler.length; i++) + int erfolge = 0; + for (int i = 0; i < anzahl; i++) { - for (int j = 0; j < anzahl; j++) + if (spiel.spielDurchlauf(spieler)) { - erfolgreich[i][j] = spiel.spielDurchlauf(spieler[i]); + erfolge++; } } - wahrscheinlichkeitErrechnen(erfolgreich); - + return erfolge; } /** @@ -71,21 +51,9 @@ public final class Simulation * @param erfolg * An array with the last game stats. */ - private static void wahrscheinlichkeitErrechnen(boolean[][] erfolg) + private static double wahrscheinlichkeitErrechnen(int erfolge, int versuche) { - spielerProp = new int[erfolg.length]; - for (int i = 0; i < spielerProp.length; i++) - { - spielerProp[i] = 0; - for (int j = 0; j < erfolg[i].length; j++) - { - if (erfolg[i][j]) - { - spielerProp[i]++; - } - } - spielerProp[i] = (int) (((double) spielerProp[i] / erfolg[i].length) * 100); - } + return (double) erfolge / versuche; } /** @@ -96,11 +64,20 @@ public final class Simulation */ public static void main(String[] args) { - nDurchlaufeAusfuehren(100000); - for (int i = 0; i < spielerProp.length; i++) + + Spieler[] spieler = { new GleichSpieler(), new WechselSpieler(), + new ZufallsSpieler(0.5) }; + + Spiel spiel = new Spiel(); + + for (int i = 0; i < spieler.length; i++) { - System.out.println("Spieler " + spieler[i].getClass().getName() - + ". war " + spielerProp[i] + "% erfolgreich."); + int spielerGewinne = nDurchlaufeAusfuehren(spiel, spieler[i], + VERSUCHE); + double spielerProb = wahrscheinlichkeitErrechnen(spielerGewinne, + VERSUCHE); + System.out.printf("%17s war %05.2f%s erfolgreich\n", spieler[i] + .getClass().getSimpleName(), spielerProb * 100, "%"); } } |
