Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. /**
  2.  
  3. Program #6
  4.  
  5. Function: Java Interactive GUI Application for Number Guessing with Colored Hints
  6.  
  7. Programmed By: Justin Buys, student (July 23, 2017)
  8.  
  9. */
  10.  
  11. import java.awt.Color;
  12. import java.awt.event.ActionEvent;
  13. import java.awt.event.ActionListener;
  14.  
  15. public class Justin_P6
  16. {
  17.     public static void main(String[] args)
  18.     {
  19.         //*
  20.         Thread gameThread = new Thread(new Runnable() {
  21.             public void run() {
  22.                     Game rng = new Game();
  23.                    
  24.                     rng.getWindow().getButton().addActionListener(new ActionListener() {
  25.                         public void actionPerformed(ActionEvent arg0)
  26.                         {
  27.                             rng.getWindow().getLabel().setText(" I have a number between 1 and 1000 can you guess my number?\n" +  
  28.                                     "Please enter a number for your first guess and then hit Enter.");
  29.                             rng.newGame();
  30.                         }
  31.                     });
  32.                    
  33.                     rng.getWindow().getField().addActionListener(new ActionListener() {
  34.                         public void actionPerformed(ActionEvent arg0)
  35.                         {
  36.                             rng.setGuessNumber(Integer.parseInt(rng.getWindow().getField().getText()));
  37.                             rng.getWindow().getField().setText("");
  38.                            
  39.                             rng.logGuess();
  40.                             if(rng.checkGuess())
  41.                             {
  42.                                 rng.getWindow().getPanel().setBackground(Color.GRAY);
  43.                                 rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is Correct! Total Guesses: " +
  44.                                     rng.getNumGuess());
  45.                             }
  46.                            
  47.                             else
  48.                             {
  49.                                 if(rng.getStatus()==1)
  50.                                 {
  51.                                     if(rng.checkTemp()==1)
  52.                                     {
  53.                                         rng.getWindow().getPanel().setBackground(Color.RED);
  54.                                         rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO HIGH! But you are getting
  55.                                             WARMER. Enter your next guess.");
  56.                                     }
  57.                                    
  58.                                     else
  59.                                     {
  60.                                         rng.getWindow().getPanel().setBackground(Color.BLUE);
  61.                                         rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO HIGH! And you are getting         
  62.                                             COLDER. Enter your next guess.");
  63.                                     }
  64.                                 }
  65.                                
  66.                                 else
  67.                                 {
  68.                                     if(rng.checkTemp()==1)
  69.                                     {
  70.                                         rng.getWindow().getPanel().setBackground(Color.RED);
  71.                                         rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO LOW! But you are getting
  72.                                             WARMER. Enter your next guess.");
  73.                                     }
  74.                                    
  75.                                     else
  76.                                     {
  77.                                         rng.getWindow().getPanel().setBackground(Color.BLUE);
  78.                                         rng.getWindow().getLabel().setText(rng.getGuessNumber()+" is TOO LOW! And you are getting      
  79.                                             COLDER. Enter your next guess.");
  80.                                     }
  81.                                 }
  82.                             }
  83.                         }
  84.                     });
  85.             }
  86.         });
  87.         gameThread.start();
  88.         //*/
  89.     }
  90. }
  91.  
  92. //Game Class//
  93. import java.util.Random;
  94.  
  95. public class Game
  96. {
  97.     private int targetNumber;
  98.     private int guessNumber;
  99.     private Random rng;
  100.     private int status;
  101.     private GameWindow window;
  102.     private int numGuess;
  103.     private int guessDiff;
  104.    
  105.     public Game()
  106.     {
  107.         window = new GameWindow();
  108.         rng = new Random();
  109.     }
  110.    
  111.     public void setGuessNumber(int guess)
  112.     {
  113.         guessNumber = guess;
  114.     }
  115.    
  116.     private void setTargetNumber()
  117.     {
  118.         targetNumber = rng.nextInt(1000)+1;
  119.     }
  120.    
  121.     public int getTargetNumber()
  122.     {
  123.         return targetNumber;
  124.     }
  125.    
  126.     public int getGuessNumber()
  127.     {
  128.         return guessNumber;
  129.     }
  130.    
  131.     public int getStatus()
  132.     {
  133.         return status;
  134.     }
  135.    
  136.     public int getNumGuess()
  137.     {
  138.         return numGuess;
  139.     }
  140.    
  141.     public void logGuess()
  142.     {
  143.         ++numGuess;
  144.     }
  145.    
  146.     public GameWindow getWindow()
  147.     {
  148.         return window;
  149.     }
  150.    
  151.     public boolean checkGuess()
  152.     {
  153.         if(guessNumber == targetNumber)
  154.             return true;
  155.        
  156.         else
  157.         {
  158.             if(guessNumber > targetNumber)
  159.                 status = 1;
  160.            
  161.             else
  162.                 status = 0;
  163.            
  164.             return false;
  165.         }
  166.     }
  167.    
  168.     public int checkTemp()
  169.     {
  170.         int diff = Math.abs(targetNumber-guessNumber);
  171.        
  172.         if(diff < guessDiff)
  173.         {
  174.             guessDiff = diff;
  175.             return 1;
  176.         }
  177.        
  178.         else
  179.         {
  180.             guessDiff = diff;
  181.             return 0;
  182.         }
  183.     }
  184.    
  185.     public void newGame()
  186.     {
  187.         setTargetNumber();
  188.         setGuessNumber(0);
  189.         numGuess = 0;
  190.         guessDiff = 1000;
  191.     }
  192. }
  193.  
  194. //GameWindow Class//
  195. import java.awt.Color;
  196. import java.awt.GridLayout;
  197. import javax.swing.*;
  198.  
  199. public class GameWindow
  200. {
  201.     private JFrame container;
  202.     private JPanel window;
  203.     private JButton button;
  204.     private JTextField textField;
  205.     private JLabel label;
  206.    
  207.     public GameWindow()
  208.     {
  209.         container = new JFrame("Do You Want To Play A Guessing Game?");
  210.         window = new JPanel(new GridLayout(0,1));
  211.         container.getContentPane().add(window);
  212.        
  213.         textField = new JTextField(5);
  214.         window.add(textField);
  215.        
  216.         button = new JButton("New Game");
  217.         window.add(button);
  218.        
  219.         label = new JLabel();
  220.         label.setForeground(Color.WHITE);
  221.         window.add(label);
  222.        
  223.         container.setVisible(true);
  224.         container.setSize(800, 100);
  225.         window.setBackground(Color.GRAY);
  226.        
  227.         container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  228.     }
  229.    
  230.     public JButton getButton()
  231.     {
  232.         return button;
  233.     }
  234.    
  235.     public JTextField getField()
  236.     {
  237.         return textField;
  238.     }
  239.    
  240.     public JLabel getLabel()
  241.     {
  242.         return label;
  243.     }
  244.    
  245.     public JPanel getPanel()
  246.     {
  247.         return window;
  248.     }
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement