/** * */ 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."); } } }