Results 1 to 4 of 4
  1. #1
    Only1Balto is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Please help me with a simple guessing game

    Hello all. I can't seem to limit the number of guesses the user tries. I get no errors, but the program continues to allow guessing after 10 tries. I'm currently in a Java programming class so I'm trying to learn all I can. If you see I'm doing anything wrong, please feel free to voice your opinions or concerns!


    ///CODE BEGINS HERE///



    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.util.Random;
    import java.util.Scanner;

    import static javafx.scene.text.Font.font;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;

    public class Final extends JFrame {
    //This constructs all the components needed to run the program

    JPanel[] jPanels = new JPanel[1];
    JPanel bigPanel;
    int numberToGuess = 0;
    JTextField textField;
    int numberOfTries = 0;
    public Final() {

    setTitle("GUESS THE NUMBER");
    setSize(600, 200);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    int guess;
    bigPanel = new JPanel();
    bigPanel.setLayout(new BorderLayout());
    makePanels();
    for (int i = 0; i < jPanels.length; i++) {
    bigPanel.add(jPanels[i]);
    }
    add(bigPanel, BorderLayout.WEST);
    JPanel smallPanel = new JPanel();

    //START// This creates the button and performs the action of starting a game
    JButton start = new JButton("Start a New Game");
    start.addActionListener(new ActionListener() {


    @Override
    public void actionPerformed(ActionEvent event) {
    Random rand = new Random();
    numberToGuess = rand.nextInt(100);
    System.out.println(numberToGuess);

    JOptionPane.showMessageDialog(null, "Guess a number between 1-100! You only have 10 guesses!", "Lets play a game...",
    JOptionPane.PLAIN_MESSAGE);

    }

    });

    //GUESS// This creates the guess button and assigns it operations
    JButton give = new JButton("Guess");
    give.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent event) {

    //comparing number guessed to number u want
    String name = textField.getText();
    int numberGuessed = Integer.parseInt(name);

    // java.awt.Font font = new java.awt.Font("Dialog",Font.BOLD,14);
    // textField.setFont(font);
    if (numberOfTries > 10)
    {
    JOptionPane.showMessageDialog(null, "Your gueeses have exceeded 10~ Please Try Again!",
    "Game Over", JOptionPane.PLAIN_MESSAGE);
    System.exit(0);
    }

    //Displays if the number guessed was correct
    if (numberGuessed == numberToGuess) {
    JOptionPane.showMessageDialog(null, "Congrats! You guessed the correct number!!!",
    "You Win!", JOptionPane.PLAIN_MESSAGE);

    } //Displays if the number guessed was too low
    else if (numberGuessed < numberToGuess) {
    JOptionPane.showMessageDialog(null, "Your guess is too LOW!",
    "Please try again", JOptionPane.PLAIN_MESSAGE);

    //Displays if the number guessed was too high
    } else if (numberGuessed > numberToGuess) {
    JOptionPane.showMessageDialog(null, "Your guess is too HIGH!",
    "Please try again", JOptionPane.PLAIN_MESSAGE);

    }

    numberOfTries++;
    }

    });




    //QUIT//This operates the quit button
    JButton quitButton = new JButton("Quit");
    quitButton.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent event) {
    System.exit(0);
    }
    });

    setVisible(true);

    smallPanel.add(give);
    smallPanel.add(start);
    smallPanel.add(quitButton);
    smallPanel.setLayout(new GridLayout(5, 1));
    add(smallPanel, BorderLayout.EAST);

    // Display the window
    setVisible(true);

    }

    private void makePanels() {

    // create the JPanels using new
    // could specify to use FlowManager, but that is the default
    for (int i = 0; i < jPanels.length; i++) {
    jPanels[i] = new JPanel();
    }

    // put stuff in each JPanel
    //0th panel: contains label and text box
    JLabel label = new JLabel("Guess A Number Between 1-100");
    // label.setBackground(Color.yellow);
    label.setOpaque(true);
    textField = new JTextField(20);
    //Scanner input = new Scanner(System.in);

    jPanels[0].add(label);
    jPanels[0].add(textField);

    }

    //MAIN BODY
    public static void main(String[] args) {
    //Initializing Parameters
    Final f = new Final();

    }
    }
    Last edited by Only1Balto; 05-20-2014 at 09:47 PM.

  2. #2
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Please help me with a simple guessing game

    Your numberOfTries is only a local variable which is always set to 1 at a button click.

    Make that a field of your class and set it to 0 when you generate a new number to guess. (So put the variable where you have the number to guess.)

    With kind regards,

    Konrad

  3. #3
    Only1Balto is offline Member
    Join Date
    May 2014
    Posts
    2
    Rep Power
    0

    Default Re: Please help me with a simple guessing game

    Haha. Thank you Konrad for the quick response. I appreciate it! I've updated the code and it works great!

    I need to incorporate a try-catch to ensure the user enters the correct response, this would need to go under the guess button correct?

    Also, I'm a little confused as to why I need the main body for this program to run. Isn't the program already self sustaining?

  4. #4
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    1

    Default Re: Please help me with a simple guessing game

    Hi,

    with try/catch you encapsulate the code that might fail. So in your case it is parsing the input of the user. So just that code should be placed in the try block.

    And when you have a program there must be starting point. When you start the application, the java virtual machine needs to know what part of your code should be executed. So when you start an application with the java virtual machine you always tell it, where the main method is located that you want to start.

    Konrad

Similar Threads

  1. Simple number guessing game but something's wrong???
    By JohnPringle83 in forum New To Java
    Replies: 11
    Last Post: 05-29-2011, 08:43 PM
  2. Guessing Game
    By rose in forum Java Gaming
    Replies: 4
    Last Post: 10-27-2010, 08:00 PM
  3. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 05:37 PM
  4. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 12:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •