/** * */ 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); } }