summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:15:58 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:18:03 +0100
commit9acea903216dbe371dd7b41cbf23b46a5732bcb4 (patch)
tree5a61491ad22e470db8151d2e07f163b011c72d6e /src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
parent8a53a8fca255e4a84ca0e35dac925a223738b98a (diff)
downloadJava1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.tar.gz
Java1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.zip
Refactored the packagenames fo better sorting
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, "%");
+ }
+ }
+
+}