summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java84
1 files changed, 84 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
new file mode 100644
index 0000000..1c6622c
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
@@ -0,0 +1,84 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe07;
+
+/**
+ * Calculates the the success rate of different players.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public final class Simulation
+{
+
+ private static final int VERSUCHE = 100000;
+
+ /**
+ * Prevents instantiation of the utility class.
+ *
+ */
+ private Simulation()
+ {
+ }
+
+ /**
+ * Makes anzahl games.
+ *
+ * @param anzahl
+ * How often the game will be run.
+ * @return Victory count
+ */
+ private static int nDurchlaufeAusfuehren(Spiel spiel, Spieler spieler,
+ int anzahl)
+ {
+
+ int erfolge = 0;
+ for (int i = 0; i < anzahl; i++)
+ {
+ if (spiel.spielDurchlauf(spieler))
+ {
+ erfolge++;
+ }
+ }
+
+ return erfolge;
+ }
+
+ /**
+ * Calculates the success rate of every player.
+ *
+ * @param erfolg
+ * An array with the last game stats.
+ */
+ private static double wahrscheinlichkeitErrechnen(int erfolge, int versuche)
+ {
+ return (double) erfolge / versuche;
+ }
+
+ /**
+ * Main method of the package.
+ *
+ * @param args
+ * Command line arguments
+ */
+ public static void main(String[] args)
+ {
+
+ Spieler[] spieler = {new GleichSpieler(), new WechselSpieler(),
+ new ZufallsSpieler(0.5)};
+
+ Spiel spiel = new Spiel();
+
+ for (int i = 0; i < spieler.length; i++)
+ {
+ 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, "%");
+ }
+ }
+
+}