/* * This file is part of the Echo Web Application Framework (hereinafter "Echo"). * Copyright (C) 2002 NextApp, Inc. * * Echo is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Echo is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with Echo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ import nextapp.echo.Button; import nextapp.echo.Color; import nextapp.echo.ContentPane; import nextapp.echo.EchoInstance; import nextapp.echo.Filler; import nextapp.echo.Font; import nextapp.echo.HttpImageReference; import nextapp.echo.Label; import nextapp.echo.TextField; import nextapp.echo.Window; import nextapp.echo.event.ActionListener; import nextapp.echo.event.ActionEvent; import nextapp.echoservlet.EchoServer; public class NumberGuessServlet extends EchoServer { public EchoInstance newInstance() { return new NumberGuess(); } } class NumberGuess extends EchoInstance { private Window window; public Window init() { window = new Window("Number Guessing Game"); startNewGame(); return window; } public void startNewGame() { // Set the content to be a new GamePane, so the window.setContent(new GamePane(this)); } public void congratulate(int numberOfTries) { window.setContent(new CongratulationsPane(this, numberOfTries)); } } // The GamePane class extends ContentPane. The // startNewGame() method in the NumberGuess class sets the // content of the browser's window to be a new GamePane. // The GamePane's instance variables contain information // about the state of the game, such as what the random // number is and how many guesses have been made. class GamePane extends ContentPane implements ActionListener { private NumberGuess numberGuess; private int randomNumber = ((int) Math.floor(Math.random() * 100)) + 1; private int lowerBound = 1; private int upperBound = 100; private int numberOfTries = 0; private TextField guessEntryField; private Label statusLabel = new Label(); private Label countLabel = new Label("You have made no guesses."); private Label promptLabel= new Label("Guess a number " + "between 1 and 100: "); private int guess; public GamePane(NumberGuess numberGuess) { super(); this.numberGuess = numberGuess; // An example of a label that contains an image. Label titleLabel = new Label(new HttpImageReference( "images/guess_a_number.png")); add(titleLabel); // Adding a vertical strut will cause a 10 pixel // vertical margin to appear between the titleLabel // added above and the statusLabel added below it. add(Filler.createVerticalStrut(10)); add(statusLabel); add(Filler.createVerticalStrut(10)); add(countLabel); add(Filler.createVerticalStrut(10)); add(promptLabel); // Creates a new empty text field // that displays 3 characters of data. guessEntryField = new TextField(3); guessEntryField.setForeground(Color.WHITE); guessEntryField.setBackground(Color.BLUE); add(guessEntryField); add(Filler.createVerticalStrut(10)); Button submitButton = new Button("Submit Your Guess"); submitButton.setActionCommand("submit guess"); submitButton.setForeground(Color.BLACK); submitButton.setBackground(Color.GREEN); submitButton.addActionListener(this); add(submitButton); add(Filler.createVerticalStrut(10)); Button newGameButton = new Button("Start a New Game"); newGameButton.setActionCommand("new game"); newGameButton.setForeground(Color.WHITE); newGameButton.setBackground(Color.RED); newGameButton.addActionListener(this); add(newGameButton); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("new game")) { numberGuess.startNewGame(); } else if (e.getActionCommand() .equals("submit guess")) { ++numberOfTries; if (numberOfTries == 1) { countLabel.setText("You have made 1 guess."); } else { countLabel.setText("You have made " + numberOfTries + " guesses."); } try { guess = Integer.parseInt( guessEntryField.getText()); } catch (NumberFormatException ex) { statusLabel.setText( "Your guess was not valid."); return; } if (guess == randomNumber) { numberGuess.congratulate(numberOfTries); } else if (guess < 1 || guess > 100) { statusLabel.setText("Your guess, " + guess + " was not between 1 and 100."); } else if (guess < randomNumber) { if (guess >= lowerBound) { lowerBound = guess + 1; } statusLabel.setText("Your guess, " + guess + " was too low. Try again:"); } else if (guess > randomNumber) { statusLabel.setText("Your guess, " + guess + " was too high. Try again:"); if (guess <= upperBound) { upperBound = guess - 1; } } promptLabel.setText("Guess a number between " + lowerBound + " and " + upperBound + ": "); } } } // Like the GamePane class, the CongratulationsPane extends // ContentPane. The game will replace the GamePane being // held in its window with a CongratulationsPane when the // user guesses the number correctly, by calling the // congratulate() method in the NumberGuess class. class CongratulationsPane extends ContentPane implements ActionListener { private NumberGuess numberGuess; public CongratulationsPane(NumberGuess numberGuess, int numberOfTries) { this.numberGuess = numberGuess; Label label = new Label(new HttpImageReference( "images/congratulations.png")); add(label); add(Filler.createVerticalStrut(20)); add(new Label("You got the correct answer in " + numberOfTries + (numberOfTries == 1 ? " try." : " tries."))); add(Filler.createVerticalStrut(40)); Button button = new Button("Play Again"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent e) { numberGuess.startNewGame(); } }