
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Random;
import java.util.*;

/**
 * JavaTrivia - a trivia game that assesses the user's knowledge on the fundamentals of Java programming
 * 
 * @author Chase Conner
 * @version JAVA SE 8
 * @course CSCI 1130-01
 * @date 11-15-15
 * @URL http://www.geocities.ws/chasec366/javaapplets/Final%20Project%20-%20Fundamentals%20of%20Java%20Programming%20Trivia%20Game/JavaTrivia.html
 * 
 * Note: Resource bibliography is at the bottom of the applet in a multiline comment 
 */
public class JavaTrivia extends JApplet implements ActionListener 
{
    //declares and initializes the Trivia buttons
    JButton[ ] JQuestion = 
    {
        new JButton("10"),
        new JButton("20"),
        new JButton("30"),
        new JButton("40"),
        new JButton("50")
    };
    
    
    //possible answers for questions 1 to 3  
    Object[] possibleAnswers1 = {"package", "method", "class"};
    Object[] possibleAnswers2 = {"x and y coordinates", "pixels", "clicking on the screen"};
    Object[] possibleAnswers3 = {"a header comment", "an inline comment", "an outline comment", "a secondary comment"};
    
    //possible answers for questions 4 to 6
    Object[] possibleAnswers4 = {"sets the color", "creates a new color", "randomly sets the color as red, green, or blue"};
    Object[] possibleAnswers5 = {"Integer", "Boolean", "String", "Character"};
    Object[] possibleAnswers6 = {"A security feature that restricts access to the source code.", "Sets the level of access to classes, variables, and methods by other classes."};
  
    //possible answers for questions 7 to 9
    Object[] possibleAnswers7 = {"10", "8", "9", "7"};
    Object[] possibleAnswers8 = {"An instance variable's values are shared among classes.", "Instance variables are stored inside static memory.", "Instance variables are declared outside of methods."};
    Object[] possibleAnswers9 = {"for", "while", "do-while"};
    
    //possible answers for questions 10 to 12
    Object[] possibleAnswers10 = {"Yes, they do", "No, they don't"};
    Object[] possibleAnswers11 = {"void employeeInfo(String name, String title, double salary)", "methodCall(employeeInfo)", "employeeInfo(name, title, salary);"};
    Object[] possibleAnswers12 = {"Yes, they can", "No, they can't"};
    
    //possible answers for questions 13 to 15
    Object[] possibleAnswers13 = {"0", "nothing", "null"};
    Object[] possibleAnswers14 = {"break;", "another bracket", "stop;"};
    Object[] possibleAnswers15 = {"A method that returns the value of a field.", "A method that sets the value of a field.", "A method that duplicates values of a field."};
     
   
    Random rand = new Random(); //allocates Random constructor to memory 
   
    String inputBox; //the String that references the user's input 
   
    int pointsEarned, score; //declares the score and the instance for the number of points earned 
    
   
    private Scoreboard scoreboard; //declaration to access methods from Scoreboard class 
    private AnswerAnalysis analysis; //declaration to access methods from AnalyzeAnswer class
   
    
    //array lists for the questions
    ArrayList<String> tenPointQuestion; 
    ArrayList<String> twentyPointQuestion;
    ArrayList<String> thirtyPointQuestion;
    ArrayList<String> fortyPointQuestion; 
    ArrayList<String> fiftyPointQuestion; 
   
    //array list for the answers
    ArrayList<Object[]> questionTenAnswers;
    ArrayList<Object[]> questionTwentyAnswers;
    ArrayList<Object[]> questionThirtyAnswers;
    ArrayList<Object[]> questionFortyAnswers;
    ArrayList<Object[]> questionFiftyAnswers;
    
    protected boolean attemptedQuestion = false; //boolean to execute the analysis class graphics once the user has attempted a question
  
    public void init()
    {
        setLayout (new FlowLayout( ) ); //changes the layout to Flow Layout
    
        
        
             //adds the JButtons to the applet window 
            for (int i=0; i<JQuestion.length; i++)
            {
                JQuestion[i].addActionListener(this);
                add( JQuestion[i] );
            }
                                                                 
            scoreboard = new Scoreboard(200, 250); //scoreboard coordinates
            
            analysis = new AnswerAnalysis(); //initializes the class in the main applet
            
          
            //adds all the questions to the ArrayList 
            tenPointQuestion = new ArrayList<String>(); 
             
            tenPointQuestion.add("What is Graphics");
            tenPointQuestion.add("For drawing methods, how do you specify where to draw?");
            tenPointQuestion.add("What do two forward slashes // create?");
             
            twentyPointQuestion = new ArrayList<String>();
            
            twentyPointQuestion.add("Color mycolor = new Color(red, green, blue) does what?");
            twentyPointQuestion.add("What data type would you use to tell the program to determine if an answer is correct or incorrect?");
            twentyPointQuestion.add("What is an access modifier? (Hint: public, private, and protected)");
             
            thirtyPointQuestion = new ArrayList<String>();
            
            thirtyPointQuestion.add("If an array begins at an index of 0 and ends at an index of 9, what index is the last element of the array located in?");
            thirtyPointQuestion.add("Which of the following is true about instance variables?");
            thirtyPointQuestion.add("What loop do you use to execute code a specified number of times?");
             
            fortyPointQuestion = new ArrayList<String>(); 
             
            fortyPointQuestion.add("In an enhanced for loop, does the element type have to match the data type of the array?");
            fortyPointQuestion.add("What would the method call look like for public void employeeInfo(String name, String title, double salary)?");
            fortyPointQuestion.add("Can fields and methods be inherited from a parent class to a subclass if their access modifiers are set to private?");
             
            fiftyPointQuestion = new ArrayList<String>();
            
            fiftyPointQuestion.add("What is the default value for constructors?)");
            fiftyPointQuestion.add("while (runAgain){ init()  start() };: the following loop executes even if the user wants it to stop by making the boolean value (runAgain) false. What is missing inside of the code?");
            fiftyPointQuestion.add("What are mutator methods?");
          
          
            //commits the question to memory
            questionTenAnswers = new ArrayList<Object[]>();
            
            //adds possible answers to array list
            questionTenAnswers.add(possibleAnswers1);
            questionTenAnswers.add(possibleAnswers2);
            questionTenAnswers.add(possibleAnswers3);
            
            questionTwentyAnswers = new ArrayList<Object[]>();
            
            questionTwentyAnswers.add(possibleAnswers4);
            questionTwentyAnswers.add(possibleAnswers5);
            questionTwentyAnswers.add(possibleAnswers6);
            
            questionThirtyAnswers = new ArrayList<Object[]>();
            
            questionThirtyAnswers.add(possibleAnswers7);
            questionThirtyAnswers.add(possibleAnswers8);
            questionThirtyAnswers.add(possibleAnswers9);
            
            questionFortyAnswers = new ArrayList<Object[]>();
            
            questionFortyAnswers.add(possibleAnswers10);
            questionFortyAnswers.add(possibleAnswers11);
            questionFortyAnswers.add(possibleAnswers12);
            
            questionFiftyAnswers = new ArrayList<Object[]>();
            
            questionFiftyAnswers.add(possibleAnswers13);
            questionFiftyAnswers.add(possibleAnswers14);
            questionFiftyAnswers.add(possibleAnswers15);
          
    }
    
    
    public void actionPerformed (ActionEvent event){    
        Object buttonClicked = event.getSource( );
        
        //converts all the array lists back to arrays 
        Object[] convertedTenAnswers = questionTenAnswers.toArray(new Object[questionTenAnswers.size()]);
        Object[] convertedTwentyAnswers = questionTwentyAnswers.toArray(new Object[questionTwentyAnswers.size()]);
        Object[] convertedThirtyAnswers = questionThirtyAnswers.toArray(new Object[questionThirtyAnswers.size()]); 
        Object[] convertedFortyAnswers = questionFortyAnswers.toArray(new Object[questionFortyAnswers.size()]);
        Object[] convertedFiftyAnswers = questionFiftyAnswers.toArray(new Object[questionFiftyAnswers.size()]);
        
        //Displays the randomized question after a button is clicked 
                if (buttonClicked == JQuestion[0]){
                        int t = rand.nextInt(tenPointQuestion.size()); //picks an index to use as a random number from the array list 
                     
                        //input window 
                        inputBox = (String)JOptionPane.showInputDialog(this, tenPointQuestion.get(t), 
                                                         "Please choose the best possible answer", JOptionPane.QUESTION_MESSAGE, 
                                                          null, (Object[])convertedTenAnswers[t], null);            
       
                        //removes the question from the array 
                        tenPointQuestion.remove(t);
                   
                        //removes the list of possible answers to a question
                        questionTenAnswers.remove(t);
                   
                    
                    if (tenPointQuestion.isEmpty()){  
                            JQuestion[0].setEnabled(false);
                        } 
                        
                        attemptedQuestion = true;
                                     }
                                                             
                else  if (buttonClicked == JQuestion[1]){
                        int m = rand.nextInt(twentyPointQuestion.size()); 
                    
                        inputBox = (String)JOptionPane.showInputDialog(this, twentyPointQuestion.get(m), 
                                                         "Please choose the best possible answer", JOptionPane.QUESTION_MESSAGE, 
                                                          null, (Object[])convertedTwentyAnswers[m], null);              
           
                        twentyPointQuestion.remove(m);
                  
                        questionTwentyAnswers.remove(m);
                  
                    if (twentyPointQuestion.isEmpty()){
                        JQuestion[1].setEnabled(false);
                    }
                    
                        attemptedQuestion = true;
                                    }
                
                else if (buttonClicked == JQuestion[2]){
                        int w = rand.nextInt(thirtyPointQuestion.size()); 
                 
                        inputBox = (String)JOptionPane.showInputDialog(this, thirtyPointQuestion.get(w), 
                                                         "Please choose the best possible answer", JOptionPane.QUESTION_MESSAGE, 
                                                          null, (Object[])convertedThirtyAnswers[w], null); 
                                                     
                        thirtyPointQuestion.remove(w);
                   
                        questionThirtyAnswers.remove(w);
            
                    if (thirtyPointQuestion.isEmpty()){
                        JQuestion[2].setEnabled(false);
                    }
                    
                        attemptedQuestion = true; 
                                                                        }
                
                else if (buttonClicked == JQuestion[3]){
                        int n = rand.nextInt(fortyPointQuestion.size()); 
          
                        inputBox = (String)JOptionPane.showInputDialog(this, fortyPointQuestion.get(n), 
                                                         "Please choose the best possible answer", JOptionPane.QUESTION_MESSAGE, 
                                                          null, (Object[])convertedFortyAnswers[n], null);                             
                  
                        fortyPointQuestion.remove(n);
                    
                        questionFortyAnswers.remove(n);
                        
                    if (fortyPointQuestion.isEmpty()){
                        JQuestion[3].setEnabled(false);
                    }
                    
                        attemptedQuestion = true; 
                                                                        }
                
                else if (buttonClicked == JQuestion[4]){
                        int q = rand.nextInt(fiftyPointQuestion.size());
            
                        inputBox = (String)JOptionPane.showInputDialog(this, fiftyPointQuestion.get(q), 
                                                         "Please choose the best possible answer", JOptionPane.QUESTION_MESSAGE, 
                                                          null, (Object[])convertedFiftyAnswers[q], null); 
                
                        fiftyPointQuestion.remove(q);

                        questionFiftyAnswers.remove(q);
               
                    
                    if (fiftyPointQuestion.isEmpty()){
                        JQuestion[4].setEnabled(false); 
                    }  
                    
                        attemptedQuestion = true; 
                }
                               
                
               analysis.analyzeAnswer(inputBox); //method call to determine if the answer is correct or incorrect. Then displays the appropriate message box                                                                                       
                  
               scoreboard.getPoints(inputBox); //parses the input to determine if the answer is correct or incorrect, and if correct, determines the amount of points the user earns
               scoreboard.calculateScore(); //calculates and updates the score
                                                            
               repaint(); //refreshes the graphics in the applet window 
    
    }   
    
       public void paint (Graphics g)
    {        
       super.paint(g);
      
       
       scoreboard.display(g); //displays the scoreboard
       if (attemptedQuestion == true){
       analysis.display(g); //displays the drawString that informs the user if they answered correctly or incorrectly
    }
       
       g.setFont(new Font("Century Schoolbook", Font.BOLD, 35));
       g.setColor(Color.orange);
       
       //draws the game's title
       g.drawString("Java Fundamentals Trivia", 315, 100);
       
       //changes the color and the font
       g.setColor(Color.black);
       g.setFont(new Font("Calibri", Font.BOLD, 30));
       
       //displays the title for directions
       g.drawString("Directions", 550, 200);
       
       g.setFont(new Font("Calibri", Font.PLAIN, 20)); //decreases font size and changes the style to plain
       
       //displays the directions    
       g.drawString("This is a trivia game that tests your knowledge on", 400, 265);
       g.drawString("the fundamentals of Java programming. There are five buttons containing three", 400, 285);
       g.drawString("questions that will be randomized and removed after an attempt. When all", 400, 305);
       g.drawString("three questions of a button have been attempted, the button is", 400, 325);
       g.drawString("is disabled. The numbers on the buttons represent the difficulty", 400, 345);
       g.drawString("of the question and the number of points earned upon answering", 400, 365);
       g.drawString("correctly. The game is over once you've attempted all of the questions.", 400, 385);
       g.drawString("Compare your scores from game to game as a way to", 400, 405); 
       g.drawString("measure your improvement. Do your best!", 400, 425); 
       
    }
    
    
/*
Final Project Resources
1.  Oracle Java Documentation: The Java Tutorials
        Access Tutorial: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html
        Arrays: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html 
        Inheritance: https://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html 
2.  TutorialPoint
        Variable Types: http://www.tutorialspoint.com/java/java_variable_types.htm 
        ArrayList: http://www.tutorialspoint.com/java/java_arraylist_class.htm 
3.  Abouttech
        Accessors and Mutators: http://java.about.com/od/workingwithobjects/a/accessormutator.htm 
4.  Java Beginners Tutorial 
        Instance Variable in Java: http://javabeginnerstutorial.com/core-java-tutorial/instance-variable-in-java/ 
    Introduction to Programming with Java Applets
        Boese, Elizabeth Sugar. An Introduction to Programming with Java Applets. 
            Sudbury, MA: Jones and Bartlett, 2010. Print.

*/
}



 
        

