summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Simulation.java
blob: 1c6622c91a6b38579239ca0d9e0f3110710a9599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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, "%");
      }
   }

}