/** * */ package de.fhswf.in.inf.java1.aufgabe7; 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; 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; } /** * Simulates a complete game. * * @param spieler * The player who plays. * @return Boolean, if the player choose correct. */ public boolean spielDurchlauf(Spieler spieler) { 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(); } }