summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/java1')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java24
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java83
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java69
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java68
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java22
5 files changed, 247 insertions, 19 deletions
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java
new file mode 100644
index 0000000..5e52f35
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe7/GleichSpieler.java
@@ -0,0 +1,24 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe7;
+
+/**
+ * This player always chooses the same shell.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class GleichSpieler extends Spieler
+{
+
+ /**
+ * Constructs a player that doesn't switch shell.
+ *
+ */
+ public GleichSpieler()
+ {
+ super(false, 0);
+ }
+
+}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java
index 59152a0..210e4ae 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe7/Simulation.java
@@ -4,7 +4,7 @@
package de.fhswf.in.inf.java1.aufgabe7;
/**
- * TODO Add comment here
+ * Calculates the the success rate of different players.
*
* @author $Author: $
* @version $Revision: $, $Date: $ UTC
@@ -12,25 +12,96 @@ package de.fhswf.in.inf.java1.aufgabe7;
public final class Simulation
{
+ private static Spieler[] spieler;
+
+ private static Spiel spiel;
+
+ private static int[] spielerProp;
+
/**
- * TODO Add constructor comment here
+ * Prevents instantiation of the utility class.
*
*/
private Simulation()
{
- // TODO Auto-generated constructor stub
}
/**
- * TODO Add method comment here
+ * 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)
{
- // TODO Auto-generated method stub
-
+ nDurchlaufeAusfuehren(100000);
+ for (int i = 0; i < spielerProp.length; i++)
+ {
+ System.out.println("Spieler " + spieler[i].getClass().getName()
+ + ". war " + spielerProp[i] + "% erfolgreich.");
+ }
}
}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java
index 0628d8c..7dacee8 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe7/Spiel.java
@@ -6,22 +6,79 @@ package de.fhswf.in.inf.java1.aufgabe7;
import java.util.Random;
/**
- * TODO Add comment here
+ * Simulates a shell game.
*
- * @author $Author: $
- * @version $Revision: $, $Date: $ UTC
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
*/
public class Spiel
{
private int nussPlatz = -1;
+
+ public static final int MAXHUET = 3;
+
private Random rnd = new Random();
+
+ /**
+ * Places the pea under a shell.
+ *
+ */
+ private void nussVerstecken()
+ {
+ nussPlatz = rnd.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)
+ {
+ int erg = -1;
+ if (ersterTipp != nussPlatz)
+ {
+ erg = MAXHUET - nussPlatz - ersterTipp;
+ }
+ else
+ {
+ erg = (ersterTipp + rnd.nextInt(MAXHUET - 1)) % MAXHUET;
+ }
+ return erg;
+ }
+
+ /**
+ * Returns the shell number with the pea.
+ *
+ * @return The shell number
+ */
+ private int aufloesung()
+ {
+ return nussPlatz;
+ }
+
/**
- * TODO Add constructor comment here
+ * Simulates a complete game.
*
+ * @param spieler
+ * The player who plays.
+ * @return Boolean, if the player choose correct.
*/
- public Spiel()
+ public boolean spielDurchlauf(Spieler spieler)
{
- // TODO Auto-generated constructor stub
+ if (spieler == null)
+ {
+ throw new IllegalArgumentException("A player hast to play.");
+ }
+ 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();
}
}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java
index 1bc86bb..23f2e7c 100644
--- a/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java
+++ b/src/de/fhswf/in/inf/java1/aufgabe7/Spieler.java
@@ -3,23 +3,77 @@
*/
package de.fhswf.in.inf.java1.aufgabe7;
+import java.util.Random;
+
/**
- * TODO Add comment here
+ * Represents a player that chooses a shell.
*
- * @author $Author: $
- * @version $Revision: $, $Date: $ UTC
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
*/
public 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.
+ *
+ * @param nutztWechselStrategy
+ * Choose initial strategy.
+ * @param wahrscheinlichkeit
+ * The probability of switching strategy.
+ */
+ public Spieler(boolean nutztWechselStrategy, int wahrscheinlichkeit)
+ {
+ if (wahrscheinlichkeit < 0 | wahrscheinlichkeit > 100)
+ {
+ throw new IllegalArgumentException(
+ "Only a probability between 0 and 100 is allowed.");
+ }
+
+ this.wahrscheinlichkeit = wahrscheinlichkeit;
+ this.nutztWechselStrategy = nutztWechselStrategy;
+ }
/**
- * TODO Add constructor comment here
+ * The first shell the player chooses.
*
+ * @return The first guess for a shell
*/
- public Spieler()
+ public int ersterTipp()
{
- // TODO Auto-generated constructor stub
-
+ ersterTipp = rnd.nextInt(Spiel.MAXHUET);
+ 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 int zweiterTipp(int leeresHuetchen)
+ {
+ if (rnd.nextInt(100) < wahrscheinlichkeit)
+ {
+ nutztWechselStrategy = !nutztWechselStrategy;
+ }
+
+ if (nutztWechselStrategy)
+ {
+ return Spiel.MAXHUET - ersterTipp - leeresHuetchen;
+ }
+ else
+ {
+ return ersterTipp;
+ }
}
}
diff --git a/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java b/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java
new file mode 100644
index 0000000..2d45c4d
--- /dev/null
+++ b/src/de/fhswf/in/inf/java1/aufgabe7/WechselSpieler.java
@@ -0,0 +1,22 @@
+/**
+ *
+ */
+package de.fhswf.in.inf.java1.aufgabe7;
+
+/**
+ * This player always changes the shell.
+ *
+ * @author $Author: $
+ * @version $Revision: $, $Date: $ UTC
+ */
+public class WechselSpieler extends Spieler
+{
+ /**
+ * Constructs a player that will always switch shell.
+ *
+ */
+ public WechselSpieler()
+ {
+ super(true, 0);
+ }
+}