summaryrefslogtreecommitdiffstats
path: root/src/de/fhswf/in/inf/java1/aufgabe07/Spiel.java
blob: 19e2998da089ddb8da81973bb0acb5d9923eb444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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);
   }

}