Guess
// Todd Percival
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class guess extends Applet implements ActionListener {
Label prompt1;
TextField guess;
TextField reply;
int number;
int value;
int flag;
int answer;
int last;
Button check;
Button reset;
public void init()
{
prompt1 = new Label ( "I have a number between 1 and 1000 can you guess it?: ");
add (prompt1);
guess = new TextField (4);
add (guess);
reply = new TextField (40);
reply.setEditable(false);
add(reply);
check=new Button("Check Guess");
check.addActionListener(this);
add(check);
generate();
reset=new Button("Play Again?");
reset.addActionListener(this);
add(reset);
}
public void generate()
{
value = 1 + (int) (Math.random() * 1000);
flag = 1;
}
public void actionPerformed (ActionEvent e)
{
if(e.getSource()==reset){
generate();
guess.setText("");
reply.setText("");
setBackground(Color.green);
}
else{
number=Integer.parseInt(guess.getText() );
if (flag==1) {
if (number < value)
reply.setText ("Guess is too low!");
if (number > value)
reply.setText ("Guess is too high!");
if (number==value) {
reply.setText ("You must be a genius! You guessed it the first try!");
setBackground(Color.yellow);
}
flag=0;
last=number;
}
if (flag==0) {
if (number < value){
if (number > last){
reply.setText ("Your guess is too low");
}
if (number < last){
reply.setText ("Your guess is too low");
setBackground(Color.blue);
}
}
if (number > value){
if (number > last){
reply.setText("Your guess is too high");
setBackground(Color.grey);
}
if (number < last){
reply.setText("Your guess is too high");
setBackground(Color.purple);
}
}
if (number == value){
reply.setText("WOOHOO, YOU WIN!!!!");
setBackground(Color.white);
}
last=number;
}
guess.setText("");
}
}
}