From 9acea903216dbe371dd7b41cbf23b46a5732bcb4 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Sun, 7 Dec 2014 19:15:58 +0100 Subject: Refactored the packagenames fo better sorting --- .../fhswf/in/inf/java1/aufgabe07/Simulation.java | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java (limited to 'src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java') 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, "%"); + } + } + +} -- cgit v1.2.3-70-g09d2