0

I am trying to make a game where you have to guess a number, i decided to add easter eggs to the game, but whenever i try to check for the easter egg it only works with the last one

public class Guess extends Applet implements ActionListener
{
    util u = new util(); //Utility class
    int answer = u.rand(1,100); //Randomize answer
    String ansWord = Integer.toString(answer); //Convert Integer to String
    int winloss = 1; //Set winloss to display startup message
    TextField input; //Declare TextField
    boolean pizza,blazeIt; //Easter Eggs
    String response; //Declare Strings
    public void init()
    {
        input = new TextField(5);
        add(input);
        input.addActionListener(this);
    }

    public void paint(Graphics g)
    {
        g.drawString(""+winloss,10,20);
        switch(winloss)
        {
            case 1:g.drawString("Guess What Number I Am Thinking Of",getWidth()/2-100,50);
            break;
            case 2:g.drawString("How did you know?!?!?!",getWidth()/2-50,50);
            break;
            case 3:g.drawString("Nope",getWidth()/2-5,50);
            break;
            case 4:pizza = true;
            break;
            case 5:blazeIt = true;
            break;
            default:g.drawString("Broken",10,10);
            break;
        }
        easterCheck(g);
    }

    public void actionPerformed(ActionEvent e)
    {
        response = String.valueOf(input.getText());
        if(response.equals(ansWord))
        {
            winloss = 2;
        }

        else
        {
            easter("420",5); //Does not work
            easter("Pizza",4); //Works
        }
        repaint();
    }

    public void easter(String wordInput, int output)
    {
        if(response.equals(wordInput))
        {
            winloss = output;
        }
        else
        {
            winloss = 3;
        }
    }

    public void easterCheck(Graphics g)
    {
        if(pizza)
        {
            g.drawString("Delicious!",10,getHeight()-10);
        }
        if(blazeIt)
        {
            g.drawString("Blaze it!",10,getHeight()-20);
        }
    }

}

How do i fix this?

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • I've posted my analysis. And please learn how to use debug tool, it's very useful. – waltersu Jun 16 '16 at 05:48
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) See [Java Plugin support deprecated](http://www.gizmodo.com.au/2016/01/rest-in-hell-java-plug-in/) and [Moving to a Plugin-Free Web](https://blogs.oracle.com/java-platform-group/entry/moving_to_a_plugin_free). .. – Andrew Thompson Jun 16 '16 at 07:09
  • .. 3) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. – Andrew Thompson Jun 16 '16 at 07:10

1 Answers1

0

Because everytime after you call easter("420",5), you will always call easter("Pizza",4) in the next. So if the input is "420", you know it's scecial string and you set winloss=4. But after that, inside easter("Pizza",4), you compare your input (which is "420") to "Pizza", and you reset winloss=3. That's how your code doesn't work.

public class Guess extends Applet implements ActionListener
{
  util u = new util(); //Utility class
  int answer = u.rand(1,100); //Randomize answer
  String ansWord = Integer.toString(answer); //Convert Integer to String
  int winloss = 1; //Set winloss to display startup message
  TextField input; //Declare TextField
  boolean pizza,blazeIt; //Easter Eggs
  String response; //Declare Strings
  public void init()
  {
    input = new TextField(5);
    add(input);
    input.addActionListener(this);
  }

  public void paint(Graphics g)
  {
    g.drawString(""+winloss,10,20);
    switch(winloss)
    {
      case 1:g.drawString("Guess What Number I Am Thinking Of",getWidth()/2-100,50);
        break;
      case 2:g.drawString("How did you know?!?!?!",getWidth()/2-50,50);
        break;
      case 3:g.drawString("Nope",getWidth()/2-5,50);
        break;
      case 4:pizza = true;
        break;
      case 5:blazeIt = true;
        break;
      default:g.drawString("Broken",10,10);
        break;
    }
    easterCheck(g);
  }

  public void actionPerformed(ActionEvent e)
  {
    response = String.valueOf(input.getText());
    if(response.equals(ansWord))
    {
      winloss = 2;
    } else if (response.equals("420")) {
      winloss = 5;
    } else if (response.equals("Pizza")) {
      winloss = 4;
    } else {
      winloss = 3;
    }
    repaint();
  }

  public void easterCheck(Graphics g)
  {
    if(pizza)
    {
      g.drawString("Delicious!",10,getHeight()-10);
    }
    if(blazeIt)
    {
      g.drawString("Blaze it!",10,getHeight()-20);
    }
  }

}
waltersu
  • 1,191
  • 8
  • 20