Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- Program #6
- Function: Java Interactive GUI Application for Number Guessing with Colored Hints
- Programmed By: Justin Buys, student (July 23, 2017)
- */
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- public class Justin_P6
- {
- public static void main(String[] args)
- {
- //*
- Thread gameThread = new Thread(new Runnable() {
- public void run() {
- Game rng = new Game();
- rng.getWindow().getButton().addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0)
- {
- rng.getWindow().getLabel().setText(" I have a number between 1 and 1000 can you guess my number?\n" +
- "Please enter a number for your first guess and then hit Enter.");
- rng.newGame();
- }
- });
- rng.getWindow().getField().addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent arg0)
- {
- rng.setGuessNumber(Integer.parseInt(rng.getWindow().getField().getText()));
- rng.getWindow().getField().setText("");
- rng.logGuess();
- if(rng.checkGuess())
- {
- rng.getWindow().getPanel().setBackground(Color.GRAY);
- rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is Correct! Total Guesses: " +
- rng.getNumGuess());
- }
- else
- {
- if(rng.getStatus()==1)
- {
- if(rng.checkTemp()==1)
- {
- rng.getWindow().getPanel().setBackground(Color.RED);
- rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO HIGH! But you are getting
- WARMER. Enter your next guess.");
- }
- else
- {
- rng.getWindow().getPanel().setBackground(Color.BLUE);
- rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO HIGH! And you are getting
- COLDER. Enter your next guess.");
- }
- }
- else
- {
- if(rng.checkTemp()==1)
- {
- rng.getWindow().getPanel().setBackground(Color.RED);
- rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO LOW! But you are getting
- WARMER. Enter your next guess.");
- }
- else
- {
- rng.getWindow().getPanel().setBackground(Color.BLUE);
- rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO LOW! And you are getting
- COLDER. Enter your next guess.");
- }
- }
- }
- }
- });
- }
- });
- gameThread.start();
- //*/
- }
- }
- //Game Class//
- import java.util.Random;
- public class Game
- {
- private int targetNumber;
- private int guessNumber;
- private Random rng;
- private int status;
- private GameWindow window;
- private int numGuess;
- private int guessDiff;
- public Game()
- {
- window = new GameWindow();
- rng = new Random();
- }
- public void setGuessNumber(int guess)
- {
- guessNumber = guess;
- }
- private void setTargetNumber()
- {
- targetNumber = rng.nextInt(1000)+1;
- }
- public int getTargetNumber()
- {
- return targetNumber;
- }
- public int getGuessNumber()
- {
- return guessNumber;
- }
- public int getStatus()
- {
- return status;
- }
- public int getNumGuess()
- {
- return numGuess;
- }
- public void logGuess()
- {
- ++numGuess;
- }
- public GameWindow getWindow()
- {
- return window;
- }
- public boolean checkGuess()
- {
- if(guessNumber == targetNumber)
- return true;
- else
- {
- if(guessNumber > targetNumber)
- status = 1;
- else
- status = 0;
- return false;
- }
- }
- public int checkTemp()
- {
- int diff = Math.abs(targetNumber-guessNumber);
- if(diff < guessDiff)
- {
- guessDiff = diff;
- return 1;
- }
- else
- {
- guessDiff = diff;
- return 0;
- }
- }
- public void newGame()
- {
- setTargetNumber();
- setGuessNumber(0);
- numGuess = 0;
- guessDiff = 1000;
- }
- }
- //GameWindow Class//
- import java.awt.Color;
- import java.awt.GridLayout;
- import javax.swing.*;
- public class GameWindow
- {
- private JFrame container;
- private JPanel window;
- private JButton button;
- private JTextField textField;
- private JLabel label;
- public GameWindow()
- {
- container = new JFrame("Do You Want To Play A Guessing Game?");
- window = new JPanel(new GridLayout(0,1));
- container.getContentPane().add(window);
- textField = new JTextField(5);
- window.add(textField);
- button = new JButton("New Game");
- window.add(button);
- label = new JLabel();
- label.setForeground(Color.WHITE);
- window.add(label);
- container.setVisible(true);
- container.setSize(800, 100);
- window.setBackground(Color.GRAY);
- container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public JButton getButton()
- {
- return button;
- }
- public JTextField getField()
- {
- return textField;
- }
- public JLabel getLabel()
- {
- return label;
- }
- public JPanel getPanel()
- {
- return window;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement