summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java
diff options
context:
space:
mode:
authorStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:15:58 +0100
committerStefan Suhren <suhren.stefan@fh-swf.de>2014-12-07 19:18:03 +0100
commit9acea903216dbe371dd7b41cbf23b46a5732bcb4 (patch)
tree5a61491ad22e470db8151d2e07f163b011c72d6e /src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java
parent8a53a8fca255e4a84ca0e35dac925a223738b98a (diff)
downloadJava1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.tar.gz
Java1-9acea903216dbe371dd7b41cbf23b46a5732bcb4.zip
Refactored the packagenames fo better sorting
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);
+ }
+
+}