Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.10 KB | None | 0 0
  1. /*
  2. *Java Version:      1.8.0_25
  3. *Author:            Peadar Ó Duinnín
  4. *Class:             COM3-A
  5. *Student Number:    R00095488
  6. */
  7.  
  8. package Assignment1;
  9.  
  10. import java.awt.BorderLayout;
  11. import java.awt.CardLayout;
  12. import java.awt.Choice;
  13. import java.awt.Font;
  14. import java.awt.GridLayout;
  15. import java.awt.Image;
  16. import java.awt.Insets;
  17. import java.awt.event.ActionEvent;
  18. import java.awt.event.ItemEvent;
  19. import java.util.Random;
  20. import javax.swing.JApplet;
  21. import javax.swing.JButton;
  22. import javax.swing.JLabel;
  23. import javax.swing.JPanel;
  24. import javax.swing.JTextField;
  25.  
  26.  
  27. public class AUIApplet extends JApplet {
  28.     private final int APPLET_WIDTH = 600;
  29.     private final int APPLET_HEIGHT = 400;
  30.  
  31.     //private enum ThreadStatus {NOT_RUNNING, RUNNING, WAITING};
  32.     private int[] highScores = {0, 0, 0, 0};
  33.     private int[] currentNumbers;
  34.     private String numsDisplayed;
  35.     private int currentScore = 0;
  36.     //private ThreadStatus threadStatus;
  37.    
  38.     final Font normalFont = new Font("Cambria", Font.PLAIN, 18);
  39.     final Font headingFont = new Font("Cambria", Font.BOLD, 24);
  40.     JPanel contPan, startPan, setupPan, gamePan;
  41.     JPanel scorePan, startInfoPan, rulePan, setupInfoPan, setupMidPan, gameOptPan, gameInfoPan, userInputPan;
  42.     JButton setupGameBut, cancelBut, endGameBut, startGameBut, checkAnswerBut;
  43.     JLabel highScoreLab, currentScoreLab, headingLab, difficultyLab, difficultyScoreLab, currentNumberLab;
  44.     JTextField answerField;
  45.     Choice difficulty;
  46.     CardLayout cl = new CardLayout();
  47.     Image waitingPic, correctPic, incorrectPic, highScorePic;
  48.    
  49.     @Override
  50.     public void init() {
  51.         //set applet size
  52.         setSize(APPLET_WIDTH, APPLET_HEIGHT);
  53.        
  54.         //set thread status to not running
  55.         //threadStatus = ThreadStatus.NOT_RUNNING;
  56.        
  57.         //start screen
  58.         startPan = new JPanel();
  59.         startPan.setLayout(new BorderLayout());
  60.        
  61.         //setup screen
  62.         setupPan = new JPanel();
  63.         setupPan.setLayout(new BorderLayout());
  64.        
  65.         setupGameBut = new JButton("New Game");
  66.         cancelBut = new JButton("Cancel");
  67.        
  68.         //game screen
  69.         gamePan = new JPanel();
  70.         gamePan.setLayout(new BorderLayout());
  71.        
  72.         endGameBut = new JButton("Quit Game");
  73.        
  74.         //heading label
  75.         headingLab = new JLabel("Number Memory");
  76.         headingLab.setFont(headingFont);
  77.        
  78.         //rule panel
  79.         rulePan = new JPanel();
  80.         rulePan.setLayout(new GridLayout(8,1));
  81.         rulePan.add(new JLabel("Rules:"));
  82.         rulePan.add(new JLabel("1. You will be shown a series of numbers, one at a time."));
  83.         rulePan.add(new JLabel("2. You must recite the series of numbers after the last number has been displayed."));
  84.         rulePan.add(new JLabel("3. After each correct recitation of the sequence, another sequence will play with one extra number."));
  85.         rulePan.add(new JLabel("Note: You can decrease/increase the time each number displays for by changing the difficulty."));
  86.        
  87.         //difficulty selection
  88.         difficulty = new Choice();
  89.         difficulty.add("Easy");
  90.         difficulty.add("Normal");
  91.         difficulty.add("Hard");
  92.         difficulty.add("Extra Hard");
  93.         difficulty.select(1);
  94.        
  95.         difficultyScoreLab = new JLabel("" + highScores[difficulty.getSelectedIndex()] + "");
  96.        
  97.         //game option panel
  98.         gameOptPan = new JPanel();
  99.         gameOptPan.setLayout(new GridLayout(1,2));
  100.         startGameBut = new JButton("Start Game");
  101.         gameOptPan.add(startGameBut);
  102.         gameOptPan.add(difficulty);
  103.        
  104.         //start info panel
  105.         startInfoPan = new JPanel();
  106.         startInfoPan.setLayout(new BorderLayout());
  107.         startInfoPan.add(rulePan, BorderLayout.CENTER);
  108.        
  109.         //set up high score display panel
  110.         setupMidPan = new JPanel();
  111.         setupMidPan.setLayout(new GridLayout(1,8));
  112.         difficultyLab = new JLabel();
  113.         difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  114.         setupMidPan.add(difficultyLab);
  115.         setupMidPan.add(difficultyScoreLab);
  116.        
  117.         //setup info panel
  118.         setupInfoPan = new JPanel();
  119.         setupInfoPan.setLayout(new BorderLayout());
  120.         setupInfoPan.add(gameOptPan, BorderLayout.SOUTH);
  121.         setupInfoPan.add(setupMidPan, BorderLayout.CENTER);
  122.        
  123.         //user input panel
  124.         userInputPan = new JPanel();
  125.         userInputPan.setLayout(new GridLayout(2,1));
  126.         answerField = new JTextField(30);
  127.         userInputPan.add(answerField);
  128.         checkAnswerBut = new JButton("Check Answer");
  129.         userInputPan.add(checkAnswerBut);
  130.        
  131.         //game info panel
  132.         gameInfoPan = new JPanel();
  133.         gameInfoPan.setLayout(new GridLayout(5,1));
  134.         currentNumberLab = new JLabel("");
  135.         gameInfoPan.add(currentNumberLab);
  136.         gameInfoPan.add(userInputPan);
  137.         gameInfoPan.add(new JLabel("Enter each number followed by a space."));
  138.        
  139.         //score panel
  140.         scorePan = new JPanel();
  141.         scorePan.setLayout(new GridLayout(10,1));
  142.         highScoreLab = new JLabel("High Score: " + highScores[difficulty.getSelectedIndex()] + "  ");
  143.         currentScoreLab = new JLabel("Current Score: " + currentScore + "  ");
  144.         scorePan.add(highScoreLab);
  145.         scorePan.add(currentScoreLab);
  146.        
  147.         //adding to start panel
  148.         startPan.add(setupGameBut, BorderLayout.SOUTH);
  149.         startPan.add(headingLab, BorderLayout.NORTH);
  150.         startPan.add(startInfoPan, BorderLayout.CENTER);
  151.        
  152.         //adding to setup panel
  153.         setupPan.add(cancelBut, BorderLayout.SOUTH);
  154.         setupPan.add(headingLab, BorderLayout.NORTH);
  155.         setupPan.add(setupInfoPan, BorderLayout.CENTER);
  156.        
  157.         //adding to game panel
  158.         gamePan.add(endGameBut, BorderLayout.SOUTH);
  159.         gamePan.add(headingLab, BorderLayout.NORTH);
  160.         gamePan.add(gameInfoPan, BorderLayout.CENTER);
  161.         gamePan.add(scorePan, BorderLayout.EAST);
  162.        
  163.         //setting up container panel and adding each screen to it
  164.         contPan = new JPanel();
  165.         contPan.setLayout(cl);
  166.         contPan.add(startPan, "Start Applet Screen");
  167.         contPan.add(setupPan, "Setup Game Screen");
  168.         contPan.add(gamePan, "New Game Screen");
  169.        
  170.         //action listeners
  171.         setupGameBut.addActionListener((ActionEvent e) -> {
  172.             newGame();
  173.         });
  174.        
  175.         startGameBut.addActionListener((ActionEvent e) -> {
  176.             //startGame(0);
  177.         });
  178.        
  179.         cancelBut.addActionListener((ActionEvent e) -> {
  180.             quitGame();
  181.         });
  182.        
  183.         endGameBut.addActionListener((ActionEvent e) -> {
  184.             quitGame();
  185.         });
  186.        
  187.         difficulty.addItemListener((ItemEvent e) -> {
  188.             switch(difficulty.getSelectedIndex()) {
  189.                 case 0:
  190.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  191.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  192.                     break;
  193.                 case 1:
  194.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  195.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  196.                     break;
  197.                 case 2:
  198.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  199.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  200.                     break;
  201.                 case 3:
  202.                     difficultyLab.setText("High Score for " + difficulty.getSelectedItem() + " Difficulty:");
  203.                     difficultyScoreLab.setText(Integer.toString(highScores[difficulty.getSelectedIndex()]));
  204.                     break;
  205.             }
  206.         });
  207.        
  208.         //add container panel
  209.         this.add(contPan);
  210.     }
  211.    
  212.     private void newGame() {
  213.         cl.show(contPan, "Setup Game Screen");
  214.     }
  215.    
  216.     /* not completed
  217.     private void startGame(int currentScore) {
  218.         cl.show(contPan, "New Game Screen");
  219.         changeThreadStatus(ThreadStatus.RUNNING);
  220.         currentNumbers = getRandomNumbers(currentScore);
  221.         printNumbers(currentNumbers);
  222.     }
  223.     */
  224.    
  225.     /* not completed
  226.     private int[] getRandomNumbers(int currentScore) {
  227.         int[] randomNums = new int[BASE_AMOUNT + currentScore];
  228.        
  229.         for (int i = 0; i < randomNums.length; i++) {
  230.             randomNums[i] = getRandomNumber();
  231.         }
  232.         return randomNums;
  233.     }
  234.     */
  235.    
  236.     private int getRandomNumber() {
  237.         Random rand = new Random();
  238.         int max = 99;
  239.         int min = 0;
  240.         int randomNum = rand.nextInt((max-min) + 1) + min;
  241.         return randomNum;
  242.     }
  243.     /*
  244.     private void printNumbers(int[] randomNumbers) {
  245.         int diffChoiceIndex = difficulty.getSelectedIndex();
  246.         int currentAmount = BASE_AMOUNT + currentScore;
  247.         new NumberThread(diffChoiceIndex, currentAmount);
  248.         for
  249.         changeThreadStatus(ThreadStatus.WAITING);
  250.     }
  251.     */
  252.    
  253.     private void quitGame() {
  254.         cl.show(contPan, "Start Applet Screen");
  255.         if (currentScore > highScores[difficulty.getSelectedIndex()]) {
  256.             highScores[difficulty.getSelectedIndex()] = currentScore;
  257.         }
  258.         currentScore = 0;
  259.     }
  260.    
  261.     //get back to this
  262.     private void checkAnswer(String answer) {
  263.         String numbersAsString = "";
  264.         for (int number : currentNumbers) {
  265.             numbersAsString += number + " ";
  266.         }
  267.     }
  268.    
  269.     private void answerCorrect() {
  270.         //unimplemented
  271.     }
  272.    
  273.     private void answerIncorrect() {
  274.         //unimplemented
  275.     }
  276.    
  277.     @Override
  278.     public Insets getInsets() {
  279.         return new Insets(10, 10, 10, 10);
  280.     }
  281. }
  282.  
  283. /*
  284. *This class is a mess. I'm not entirely sure what I'm doing. Don't mind it.
  285. */
  286. package Assignment1;
  287.  
  288. public class NumberThread implements Runnable {
  289.     private final int BASE_AMOUNT = 4;
  290.     private final int[] DIFF_TIMES = {2000,1800,1600,1400};
  291.     private final int CURRENT_SCORE;
  292.     private final int NUM_OF_RUNS;
  293.     int speed;
  294.    
  295.     public NumberThread(int difficulty, int currentScore) {
  296.         this.speed = DIFF_TIMES[difficulty];
  297.         this.CURRENT_SCORE = currentScore;
  298.         this.NUM_OF_RUNS = BASE_AMOUNT + CURRENT_SCORE;
  299.     }
  300.    
  301.     @Override
  302.     public void run() {
  303.         try {
  304.             for (int i = 0; i < NUM_OF_RUNS; i++) {
  305.                 currentNumberLab.setText(Integer.toString(randomNumbers[i]));
  306.                 Thread.sleep(speed);
  307.             }
  308.         }
  309.         catch (InterruptedException e) {
  310.            
  311.         }
  312.         currentNumberLab.setText(Integer.toString(randomNumbers[i]));
  313.         Thread.sleep(speed);
  314.         currentNumberLab.setText("");
  315.     }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement