Hi Lo guessing game

Page 1 of 1

2 Replies - 426 Views - Last Post: 18 May 2016 - 10:31 AM Rate Topic: -----

#1 ggwat   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-May 16

Hi Lo guessing game

Posted 18 May 2016 - 09:48 AM

I need help with my code I cant get it to work and am not sure what im doing. The game is suppose to work as if someone puts in a higher number than the random number they loss vise versa with a lower number.
The game must include -

title panel

Choice of size

Button to play

TextField for guessing

Panel for determining winner

BONUS - Add a button to play again

import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class FieldListener extends JPanel
    {
         public boolean checkGameOver()
                 {
                     int limit = (int)( Math.log(size)/Math.log(2));
                     //System.out.println(limit);
                     if (guessCount > limit)
                     {   
                         outputLabel.setText("The target was: " + target);
                         resultLabel.setText("Game Over");
                         resultLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
                         resultLabel.setForeground(Color.red);
                         return true;
                        }
                        return false;
                 }
        public FieldListener()
        {
            
        }
    private class TempListener implements ActionListener
    {
   
        public void actionPerformed(ActionEvent event)
        {
                
            String text = guessField.getText();
            guess = Integer.parseInt(text);
            
            if (guess > target)
            {
                outputLabel.setText("Your guess is too high. ");
                if (!checkGameOver())
                {
                    resultLabel.setText("Try Again");
                    guessField.addMouseListener(new MouseAdapter(){
                        public void mouseClicked(MouseEvent e)
                        {
                            guessField.setText("");
                            outputLabel.setText("");
                        }
                    });
                }
                guessCount++;
            }
            else if (guess < target)
            {
                outputLabel.setText("Your guess is too low. ");
                if (!checkGameOver())
                {
                    resultLabel.setText("Try Again");
                    guessField.addMouseListener(new MouseAdapter(){
                        public void mouseClicked(MouseEvent e)
                        {
                            guessField.setText("");
                            outputLabel.setText("");
                        }
                    });
                }
                guessCount++;
            }
            else
            {
                outputLabel.setText("");
                resultLabel.setFont(new Font("Helvetica", Font.BOLD, 24));
                resultLabel.setForeground(Color.blue);
                resultLabel.setText("You win!\n The number was " + target);
            }
        }
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Hi Lo guessing game

#2 xiledcore   User is offline

  • D.I.C Head

Reputation: 23
  • View blog
  • Posts: 62
  • Joined: 20-March 16

Re: Hi Lo guessing game

Posted 18 May 2016 - 10:08 AM

Why is it not working? As far as I can see, you haven't even got a main class with a main method. You've created a class called FieldListener which is kind of confusing since it extends JPanel. I think you've forgotten to include all of the classes, as well as your errors.
Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12305
  • View blog
  • Posts: 45,401
  • Joined: 27-December 08

Re: Hi Lo guessing game

Posted 18 May 2016 - 10:31 AM

Please do not duplicate post.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1