From 0a17d5dd687ced4dd501097e6d15376ccbb8b281 Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Wed, 12 Nov 2014 15:29:01 +0100 Subject: Assignment No.7 after correction --- .../fhswf/in/inf/java1/aufgabe7/GleichSpieler.java | 13 ++-- src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java | 71 ++++++++-------------- src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java | 18 +++--- src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java | 52 +++------------- .../in/inf/java1/aufgabe7/WechselSpieler.java | 12 ++-- .../in/inf/java1/aufgabe7/ZufallsSpieler.java | 52 ++++++++++++++++ 6 files changed, 109 insertions(+), 109 deletions(-) create mode 100644 src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java (limited to 'src/de/fhswf/in/inf') diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java index 5e52f35..f5bf021 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java @@ -11,14 +11,15 @@ package de.fhswf.in.inf.java1.aufgabe7; */ public class GleichSpieler extends Spieler { - - /** - * Constructs a player that doesn't switch shell. - * + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe7.Spieler#zweiterTipp(int) */ - public GleichSpieler() + @Override + public final int zweiterTipp(int leeresHuetchen) { - super(false, 0); + return getErsterTipp(); } } diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java index 210e4ae..1b42f69 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java @@ -12,11 +12,7 @@ package de.fhswf.in.inf.java1.aufgabe7; public final class Simulation { - private static Spieler[] spieler; - - private static Spiel spiel; - - private static int[] spielerProp; + private static final int VERSUCHE = 100000; /** * Prevents instantiation of the utility class. @@ -26,43 +22,27 @@ public final class 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. + * @return Gewonnene Spiele. */ - private static void nDurchlaufeAusfuehren(int anzahl) + private static int nDurchlaufeAusfuehren(Spiel spiel, Spieler spieler, + int anzahl) { - aufsetzen(); - boolean[][] erfolgreich = new boolean[spieler.length][anzahl]; - for (int i = 0; i < spieler.length; i++) + int erfolge = 0; + for (int i = 0; i < anzahl; i++) { - for (int j = 0; j < anzahl; j++) + if (spiel.spielDurchlauf(spieler)) { - erfolgreich[i][j] = spiel.spielDurchlauf(spieler[i]); + erfolge++; } } - wahrscheinlichkeitErrechnen(erfolgreich); - + return erfolge; } /** @@ -71,21 +51,9 @@ public final class Simulation * @param erfolg * An array with the last game stats. */ - private static void wahrscheinlichkeitErrechnen(boolean[][] erfolg) + private static double wahrscheinlichkeitErrechnen(int erfolge, int versuche) { - 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); - } + return (double) erfolge / versuche; } /** @@ -96,11 +64,20 @@ public final class Simulation */ public static void main(String[] args) { - nDurchlaufeAusfuehren(100000); - for (int i = 0; i < spielerProp.length; i++) + + Spieler[] spieler = { new GleichSpieler(), new WechselSpieler(), + new ZufallsSpieler(0.5) }; + + Spiel spiel = new Spiel(); + + for (int i = 0; i < spieler.length; i++) { - System.out.println("Spieler " + spieler[i].getClass().getName() - + ". war " + spielerProp[i] + "% erfolgreich."); + 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, "%"); } } diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java index 7dacee8..1954a8c 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java @@ -17,7 +17,7 @@ public class Spiel public static final int MAXHUET = 3; - private Random rnd = new Random(); + public static final Random RAND = new Random(); /** * Places the pea under a shell. @@ -25,7 +25,7 @@ public class Spiel */ private void nussVerstecken() { - nussPlatz = rnd.nextInt(MAXHUET); + nussPlatz = RAND.nextInt(MAXHUET); } /** @@ -37,6 +37,10 @@ public class Spiel */ private int zeigeHuetchen(int ersterTipp) { + if (ersterTipp < 0 || ersterTipp > MAXHUET) + { + throw new IllegalArgumentException("Tip liegt nicht im Bereich."); + } int erg = -1; if (ersterTipp != nussPlatz) { @@ -44,7 +48,7 @@ public class Spiel } else { - erg = (ersterTipp + rnd.nextInt(MAXHUET - 1)) % MAXHUET; + erg = (ersterTipp + RAND.nextInt(MAXHUET - 1)) % MAXHUET; } return erg; } @@ -54,9 +58,9 @@ public class Spiel * * @return The shell number */ - private int aufloesung() + private boolean aufloesung(int zweiterTipp) { - return nussPlatz; + return nussPlatz == zweiterTipp; } /** @@ -75,10 +79,8 @@ public class Spiel int tipp = -1; nussVerstecken(); tipp = spieler.ersterTipp(); - assert tipp >= 0 && tipp <= MAXHUET; tipp = spieler.zweiterTipp(zeigeHuetchen(tipp)); - assert tipp >= 0 && tipp <= MAXHUET; - return tipp == aufloesung(); + return aufloesung(tipp); } } diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java index 23f2e7c..40c7254 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java @@ -3,52 +3,35 @@ */ package de.fhswf.in.inf.java1.aufgabe7; -import java.util.Random; - /** * Represents a player that chooses a shell. * * @author $Author: $ * @version $Revision: $, $Date: $ UTC */ -public class Spieler +public abstract class Spieler { - private Random rnd = new Random(); private int ersterTipp = -1; - private int wahrscheinlichkeit = 0; - - private boolean nutztWechselStrategy = false; - /** - * A player with a certain probability of switching its strategy. + * The first shell the player chooses. * - * @param nutztWechselStrategy - * Choose initial strategy. - * @param wahrscheinlichkeit - * The probability of switching strategy. + * @return The first guess for a shell */ - public Spieler(boolean nutztWechselStrategy, int wahrscheinlichkeit) + public final int ersterTipp() { - if (wahrscheinlichkeit < 0 | wahrscheinlichkeit > 100) - { - throw new IllegalArgumentException( - "Only a probability between 0 and 100 is allowed."); - } - - this.wahrscheinlichkeit = wahrscheinlichkeit; - this.nutztWechselStrategy = nutztWechselStrategy; + ersterTipp = Spiel.RAND.nextInt(Spiel.MAXHUET); + return ersterTipp; } /** - * The first shell the player chooses. + * Getter for the first choice. * - * @return The first guess for a shell + * @return The first choice */ - public int ersterTipp() + public final int getErsterTipp() { - ersterTipp = rnd.nextInt(Spiel.MAXHUET); return ersterTipp; } @@ -59,21 +42,6 @@ public class Spieler * The shell, the game showed as empty * @return The second guess for the right shell */ - public int zweiterTipp(int leeresHuetchen) - { - if (rnd.nextInt(100) < wahrscheinlichkeit) - { - nutztWechselStrategy = !nutztWechselStrategy; - } - - if (nutztWechselStrategy) - { - return Spiel.MAXHUET - ersterTipp - leeresHuetchen; - } - else - { - return ersterTipp; - } - } + public abstract int zweiterTipp(int leeresHuetchen); } diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java index 2d45c4d..5d9b976 100644 --- a/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java +++ b/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java @@ -10,13 +10,13 @@ package de.fhswf.in.inf.java1.aufgabe7; * @version $Revision: $, $Date: $ UTC */ public class WechselSpieler extends Spieler -{ - /** - * Constructs a player that will always switch shell. - * +{ + /* (non-Javadoc) + * @see de.fhswf.in.inf.java1.aufgabe7.Spieler#zweiterTipp(int) */ - public WechselSpieler() + @Override + public int zweiterTipp(int leeresHuetchen) { - super(true, 0); + return Spiel.MAXHUET - getErsterTipp() - leeresHuetchen; } } diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java new file mode 100644 index 0000000..f633d31 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe7/ZufallsSpieler.java @@ -0,0 +1,52 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe7; + +/** + * A player that switches strategy with a certain probability. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class ZufallsSpieler extends WechselSpieler +{ + + private double wahrscheinlichkeit = 0; + + /** + * Initializes a player which switches strategies. + * + * @param wahrscheinlichkeit + * Probability of switching strategy. + */ + public ZufallsSpieler(double wahrscheinlichkeit) + { + if (wahrscheinlichkeit < 0.0 | wahrscheinlichkeit > 1.0) + { + throw new IllegalArgumentException( + "Only a probability between 0 and 100 is allowed."); + } + + this.wahrscheinlichkeit = wahrscheinlichkeit; + } + + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe7.WechselSpieler#zweiterTipp(int) + */ + @Override + public final int zweiterTipp(int leeresHuetchen) + { + if (Spiel.RAND.nextDouble() < wahrscheinlichkeit) + { + return getErsterTipp(); + } + else + { + return super.zweiterTipp(leeresHuetchen); + } + } + +} -- cgit v1.2.3-70-g09d2