summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java
blob: 210e4ae2857fed424594062b2cb6ff9b185a5016 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
 * 
 */
package de.fhswf.in.inf.java1.aufgabe7;

/**
 * Calculates the the success rate of different players.
 *
 * @author $Author: $
 * @version $Revision: $, $Date: $ UTC
 */
public final class Simulation
{

   private static Spieler[] spieler;

   private static Spiel spiel;

   private static int[] spielerProp;

   /**
    * Prevents instantiation of the utility class.
    *
    */
   private 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.
    */
   private static void nDurchlaufeAusfuehren(int anzahl)
   {
      aufsetzen();

      boolean[][] erfolgreich = new boolean[spieler.length][anzahl];
      for (int i = 0; i < spieler.length; i++)
      {
         for (int j = 0; j < anzahl; j++)
         {
            erfolgreich[i][j] = spiel.spielDurchlauf(spieler[i]);
         }
      }

      wahrscheinlichkeitErrechnen(erfolgreich);

   }

   /**
    * Calculates the success rate of every player.
    *
    * @param erfolg
    *           An array with the last game stats.
    */
   private static void wahrscheinlichkeitErrechnen(boolean[][] erfolg)
   {
      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);
      }
   }

   /**
    * Main method of the package.
    *
    * @param args
    *           Command line arguments
    */
   public static void main(String[] args)
   {
      nDurchlaufeAusfuehren(100000);
      for (int i = 0; i < spielerProp.length; i++)
      {
         System.out.println("Spieler " + spieler[i].getClass().getName()
               + ". war " + spielerProp[i] + "% erfolgreich.");
      }
   }

}