summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java')
-rw-r--r--src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java86
1 files changed, 86 insertions, 0 deletions
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);
+ }
+
+}