diff options
| author | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-12-07 19:15:58 +0100 |
|---|---|---|
| committer | Stefan Suhren <suhren.stefan@fh-swf.de> | 2014-12-07 19:18:03 +0100 |
| commit | 9acea903216dbe371dd7b41cbf23b46a5732bcb4 (patch) | |
| tree | 5a61491ad22e470db8151d2e07f163b011c72d6e /src/de/fhswf/in/inf/java1/aufgabe07 | |
| parent | 8a53a8fca255e4a84ca0e35dac925a223738b98a (diff) | |
| download | Java1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.tar.gz Java1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.zip | |
Refactored the packagenames fo better sorting
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe07')
6 files changed, 316 insertions, 0 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java new file mode 100644 index 0000000..be592cd --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/GleichSpieler.java @@ -0,0 +1,25 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * This player always chooses the same shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class GleichSpieler extends Spieler +{ + /* + * (non-Javadoc) + * + * @see de.fhswf.in.inf.java1.aufgabe07.Spieler#zweiterTipp(int) + */ + @Override + public final int zweiterTipp(int leeresHuetchen) + { + return getErsterTipp(); + } + +} 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, "%"); + } + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java b/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java new file mode 100644 index 0000000..19e2998 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java @@ -0,0 +1,86 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +import java.util.Random; + +/** + * Simulates a shell game. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class Spiel +{ + private int nussPlatz = -1; + + public static final int MAXHUET = 3; + + public static final Random RAND = new Random(); + + /** + * Places the pea under a shell. + * + */ + private void nussVerstecken() + { + nussPlatz = RAND.nextInt(MAXHUET); + } + + /** + * Returns an empty shell, which the user didn't choose. + * + * @param ersterTipp + * The first choice of the player. + * @return An empty shell number of the remaining shells + */ + private int zeigeHuetchen(int ersterTipp) + { + if (ersterTipp < 0 || ersterTipp > MAXHUET) + { + throw new IllegalArgumentException("Tip outside of boundaries."); + } + int erg = -1; + if (ersterTipp != nussPlatz) + { + erg = MAXHUET - nussPlatz - ersterTipp; + } + else + { + erg = (ersterTipp + RAND.nextInt(MAXHUET - 1)) % MAXHUET; + } + return erg; + } + + /** + * Returns the shell number with the pea. + * + * @return The shell number + */ + private boolean aufloesung(int zweiterTipp) + { + return nussPlatz == zweiterTipp; + } + + /** + * Simulates a complete game. + * + * @param spieler + * The player who plays. + * @return Boolean, if the player choose correct. + */ + public final boolean spielDurchlauf(Spieler spieler) + { + if (spieler == null) + { + throw new IllegalArgumentException("A player hast to play."); + } + int tipp = -1; + nussVerstecken(); + tipp = spieler.ersterTipp(); + tipp = spieler.zweiterTipp(zeigeHuetchen(tipp)); + return aufloesung(tipp); + } + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java new file mode 100644 index 0000000..14d17a8 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/Spieler.java @@ -0,0 +1,47 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * Represents a player that chooses a shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public abstract class Spieler +{ + + private int ersterTipp = -1; + + /** + * The first shell the player chooses. + * + * @return The first guess for a shell + */ + public final int ersterTipp() + { + ersterTipp = Spiel.RAND.nextInt(Spiel.MAXHUET); + return ersterTipp; + } + + /** + * Getter for the first choice. + * + * @return The first choice + */ + public final int getErsterTipp() + { + return ersterTipp; + } + + /** + * After the first guess, the player must choose a second shell. + * + * @param leeresHuetchen + * The shell, the game showed as empty + * @return The second guess for the right shell + */ + public abstract int zweiterTipp(int leeresHuetchen); + +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java new file mode 100644 index 0000000..5c6ec55 --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/WechselSpieler.java @@ -0,0 +1,22 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * This player always changes the shell. + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class WechselSpieler extends Spieler +{ + /* (non-Javadoc) + * @see de.fhswf.in.inf.java1.aufgabe07.Spieler#zweiterTipp(int) + */ + @Override + public int zweiterTipp(int leeresHuetchen) + { + return Spiel.MAXHUET - getErsterTipp() - leeresHuetchen; + } +} diff --git a/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java new file mode 100644 index 0000000..29a8aba --- /dev/null +++ b/src/de/fhswf/in/inf/java1/aufgabe07/ZufallsSpieler.java @@ -0,0 +1,52 @@ +/** + * + */ +package de.fhswf.in.inf.java1.aufgabe07; + +/** + * 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.aufgabe07.WechselSpieler#zweiterTipp(int) + */ + @Override + public final int zweiterTipp(int leeresHuetchen) + { + if (Spiel.RAND.nextDouble() < wahrscheinlichkeit) + { + return getErsterTipp(); + } + else + { + return super.zweiterTipp(leeresHuetchen); + } + } + +} |
