Question

Create a JavaFX game: Guess the Number create a random # between 1 and 1000 Ask...

Create a JavaFX game: Guess the Number

create a random # between 1 and 1000

Ask user for a guess; possible answers

TOO LOW

TOO HIGH

WINNER!

print guess to screen

if the user wins, write the random number, and all the guesses to a file.

If the user doesn't guess in 10 turns, display the number.

NOTES:

You may want to implement:

Restart option

Best Guess statistic (game 1 took 8 tries, game 2 took 5 - 5 is the best guess statistic)

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

public class GuessGame extends JFrame 
{
        private JFrame guessFrame;
        private JLabel starter;
        private JLabel userPrompt;
        private JTextField input;
        private JLabel correct;
        private JButton guesser;
        private JLabel tooHigh;
        private JLabel tooLow;
        private JButton startOver;

        public GuessGame()
        {
                guessFrame = new JFrame("Guessing Game!");
                starter = new JLabel("I have a number between 1 and 1000.  Can you guess what it is?");
                userPrompt = new JLabel("Please enter your guess.");
                input = new JTextField(5);
                guesser = new JButton("Guess");
                correct = new JLabel("Correct!!");
                tooHigh = new JLabel("Too High");
                tooLow = new JLabel("Too Low");
                startOver = new JButton("Play again");

                Container c = guessFrame.getContentPane();
                c.setLayout(new FlowLayout());

                c.add(starter);
                c.add(userPrompt);
                c.add(input);
                c.add(guesser);

                guessFrame.setSize(250,250);

                guessFrame.addWindowListener(new WindowAdapter()
                {
                        public void windowClosing(WindowEvent e) {System.exit(0);}
                });

                guesserButtonHandler ghandler = new guesserButtonHandler();
                guesserButtonHandler.addActionListener(ghandler);

                guessFrame.show();
        }

        class guesserButtonHandler implements ActionListener
        {
                public void actionPerformed(ActionEvent e)
                {
                        double random = Math.random()*1000;
                        int number = Math.round(random);
                        String triedNum;
                        int tested;

                        triedNum = input.getText();
                        tested = Integer.parseInt(triedNum);
                        if (tested > number)
                        {
                                c.addJLabel(tooHigh);
                        }
                        if (tested < number)
                        {
                                c.addJLabel(tooLow);
                        }
                        else (tested == number)
                        {
                                c.addJLabel(correct);
                        }
                }
        }

        public static void main(String[] args)
        {
                new GuessGame();
        }
}
Add a comment
Know the answer?
Add Answer to:
Create a JavaFX game: Guess the Number create a random # between 1 and 1000 Ask...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • help me do flowchart please Project Design: Project Scop To create a "guess the number" game...

    help me do flowchart please Project Design: Project Scop To create a "guess the number" game that challenges the user to see how quickly they can "guess the number" using a combination of luck and logic. The user is given an initial guess, and if the guess is wrong, the user must solve a riddle to find out if his/her guess was too high or too low. Project Specifics/Rules: The number must fall between 1 and 20. The user is...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly...

    newd in C++ Exercise #3 Guess the Number Write a "Guess the Number" game that randomly assigns a secret number [1,20] for the user to guess. I recommend hard coding your secret number at first to make testing your code easier. You can write this a number of ways, but try to stick to the instructions outlined here. Write three methods to make your game work. The first method should generate a random number and return the value to main...

  • In this activity you will create a game call Guess That Number! The program has an...

    In this activity you will create a game call Guess That Number! The program has an unlimited number of rounds. In each round the program selects a random number that the user has to guess. In Round 1 the user has to guess a number less than 5 within 5 tries. If the correct number is not guessed in 5 tries than the game is over. If the correct number is guessed than the user moves onto the next round...

  • 7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number”...

    7.30 - Guess-the-Number Game (Project Name: GuessTheNumber) - Write an app that plays “Guess the Number” as follows: Your app chooses the number to be guessed by selecting a random integer in the range 1 to 1000. The app displays the prompt "Guess a number between 1 and 1000: ". The player inputs a first guess. If the player’s guess is incorrect, your app should display Too high. Try again. or Too low. Try again. to help the player “zero...

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT