Lösung Übung 15: Guess the number
// GuessTheNumber.java
import java.awt.*;
import java.applet.Applet;
import java.util.*;
public class GuessTheNumber extends Applet {
protected TextField tippEingabeFeld;
protected TextField meldungAusgabeFeld;
protected Button tippButton;
protected Button resetButton;
protected Label[] meldungLabels;
protected Panel[] panels;
protected Random numberGenerator;
protected int theNumber;
protected int counter;
protected int lastTry;
protected int currentTry;
protected String[] initMeldung = {
"Ich habe eine Nummer zwischen 1 und 1000 ausgewählt",
"Können Sie die Nummer erraten?",
"Geben Sie Ihren Rateversuch ein!"
};
public void init() {
createGUI();
initGame();
}
// Methode erzeugt Benutzeroberflaeche
private void createGUI() {
this.setLayout( new GridLayout(3,1) );
meldungAusgabeFeld = new TextField("");
meldungAusgabeFeld.setEditable( false );
panels = new Panel[2];
panels[0] = new Panel();
panels[1] = new Panel();
meldungLabels = new Label[3];
meldungLabels[0] = new Label();
meldungLabels[1] = new Label();
meldungLabels[2] = new Label();
for(int i = 0; i < 3; i++ ) {
panels[0].add( meldungLabels[i] );
}
tippEingabeFeld = new TextField(20);
tippButton = new Button("Tipp absenden");
TippButtonListener tpl = new TippButtonListener( this );
tippButton.addActionListener( tpl );
resetButton = new Button("Neues Spiel");
ResetButtonListener rbl = new ResetButtonListener( this );
resetButton.addActionListener( rbl );
panels[1].add( tippEingabeFeld );
panels[1].add( tippButton );
panels[1].add( resetButton );
this.add( panels[0] );
this.add( meldungAusgabeFeld );
this.add( panels[1] );
}
/**
* Methode initialisiert Spiel. Wird vom ResetButtonListener aufgerufen.
*/
public void initGame() {
theNumber = getRandomNumber();
counter = 0;
lastTry = -1;
for(int i=0; i < 3; i++ ) {
meldungLabels[i].setText( initMeldung[i] );
}
setPanelsBackground( Color.white );
meldungAusgabeFeld.setText("");
tippEingabeFeld.setText("");
tippEingabeFeld.setEditable(true);
}
/**
* Methode wertet Rateversuch aus. Wird vom TippButtonListener aufgerufen.
*/
public void evaluateTry() {
String ergebnis = null;
tippEingabeFeld.setEditable(false);
counter++;
try {
currentTry = Integer.parseInt( tippEingabeFeld.getText() );
}
catch( NumberFormatException e ) {
meldungAusgabeFeld.setText( "Fehler bei der Eingabe..." );
tippEingabeFeld.setText("");
tippEingabeFeld.setEditable(true);
return;
}
if( currentTry == theNumber ) {
// Volltreffer
ergebnis = " ist richtig!!!!";
}
else {
if( currentTry < theNumber ) {
ergebnis = " ist zu tief...";
}
else{
ergebnis = " ist zu hoch...";
}
if( currentTry < theNumber ) {
ergebnis = " ist zu tief...";
}
else{
ergebnis = " ist zu hoch...";
}
if( lastTry > 0 ) {
int lastDiff = theNumber - lastTry;
if( lastDiff < 0 ) {
lastDiff *= -1;
}
int currentDiff = theNumber - currentTry;
if( currentDiff < 0 ) {
currentDiff *= -1;
}
if( currentDiff < lastDiff )
setPanelsBackground( Color.blue );
else
setPanelsBackground( Color.red );
}
tippEingabeFeld.setEditable(true);
}
lastTry = currentTry;
tippEingabeFeld.setText("");
meldungAusgabeFeld.setText( "Versuch " + counter + ": " + lastTry + ergebnis );
}
// Methode setzt Hintergrundfarben fuer relevante GUI-Elemente
private void setPanelsBackground( Color c ) {
setBackground( c );
for(int i = 0; i < panels.length; i++ )
panels[i].setBackground( c );
for(int i = 0; i < meldungLabels.length; i++ )
meldungLabels[i].setBackground( c );
tippEingabeFeld.setBackground( Color.white );
}
// Methode gibt Zufallszahl zwischen 1 und 1000 (einschliesslich) zurueck
private int getRandomNumber() {
if( numberGenerator == null ) {
numberGenerator = new Random();
}
// Ab Version JDK 1.2 steht mit Random.nextInt( int n ) eine
// komfortable Methode zur Verfuegung. Hier folgt der weniger
// elegante alte Weg.
int cInt = numberGenerator.nextInt();
if( cInt < 0 ) cInt *= -1;
cInt %= 1000;
cInt++;
return( cInt );
}
}
// Datei TippButton.java - ActionListener, der die Auswertung eines Rateversuchs initiiert
import java.awt.event.*;
public class TippButtonListener implements ActionListener {
GuessTheNumber parent;
public TippButtonListener( GuessTheNumber p ) {
parent = p;
}
public void actionPerformed( ActionEvent e ) {
parent.evaluateTry();
}
}
// Datei ResetButtonListener.java - ActionListener, der das Spiel neu initialisiert
import java.awt.event.*;
public class ResetButtonListener implements ActionListener {
GuessTheNumber parent;
public ResetButtonListener( GuessTheNumber p ) {
parent = p;
}
public void actionPerformed( ActionEvent e ) {
parent.initGame();
}
}
[Index]