Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
S-Quiz - SQuiz.java

SQuiz.java

Caricato da: Black Shadow
Scarica il programma completo

  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.event.*;
  5. import java.util.StringTokenizer;
  6. import java.io.*;
  7. /**
  8.  *The S-Quiz main class
  9.  *@author Black Shadow (Giovanni Casinelli)
  10.  */
  11. class SQuiz extends JFrame implements ItemListener {
  12.     /*Variabili*/
  13.     Player player;          //The player
  14.     Questions questions;    //The questions
  15.     JLabel question;        //The question
  16.     JRadioButton ans1, ans2, ans3, ans4;    //The answers
  17.     ButtonGroup answers;                    //The Group of answers
  18.     JButton Answer;          //The Button to Answer at the question.
  19.     JPanel qPanel, aPanel, APanel;      //The Panels for the Game.
  20.     JRadioButtonMenuItem settingsNormalRBMI, settingsFastRBMI;
  21.     FileInputStream ladder;
  22.     ActionListener questionsListeners = new ActionListener() {
  23.         public void actionPerformed(ActionEvent ae) {
  24.             try {
  25.                 JOptionPane.showMessageDialog(null,
  26.                         "In totale ci sono " +
  27.                         getTotQ() +
  28.                         " domande.",
  29.                         "Domande Totali.",
  30.                         JOptionPane.INFORMATION_MESSAGE);
  31.             } catch (HeadlessException he) {}
  32.         }
  33.     };
  34.     int Num;                            //Register the last num param of MakeGUI();
  35.     long start, end;        //Used for currentTimeMillis() function.
  36.     String topTen[] = new String[10];   //The TopTen ladder
  37.     String topTenPoints[] = new String[10];   //The TopTen points
  38.     /**
  39.      *S-Quiz constructor
  40.      */
  41.     SQuiz() {
  42.         setTitle("S-Quiz! Riuscirai ad arrivare alla fine?");
  43.         setSize(640, 320);
  44.         setResizable(false);
  45.         addWindowListener(new WindowAdapter() {
  46.             public void windowClosing(WindowEvent we) {
  47.                 JOptionPane.showMessageDialog(null, "Arrivederci!",
  48.                         "S-Quiz", JOptionPane.INFORMATION_MESSAGE);
  49.                 System.exit(0);
  50.             }
  51.         });
  52.         JMenuBar menuBar = new JMenuBar();
  53.         JMenu fileMenu = new JMenu("File");
  54.         fileMenu.setMnemonic(KeyEvent.VK_F);
  55.         JMenuItem fileNewMenuItem = new JMenuItem("Nuova Partita", KeyEvent.VK_N);
  56.         fileNewMenuItem.addActionListener(new ActionListener() {
  57.             public void actionPerformed(ActionEvent ae) {
  58.                 newGame();
  59.             }
  60.         });
  61.         fileMenu.add(fileNewMenuItem);
  62.         JMenuItem fileOptionMenuItem = new JMenuItem("Opzioni", KeyEvent.VK_O);
  63.         JMenuItem fileExitMenuItem = new JMenuItem("Esci", KeyEvent.VK_E);
  64.         fileExitMenuItem.addActionListener(new ActionListener() {
  65.             public void actionPerformed(ActionEvent ae) {
  66.                 JOptionPane.showMessageDialog(null, "Arrivederci!",
  67.                         "S-Quiz", JOptionPane.INFORMATION_MESSAGE);
  68.                 System.exit(0);
  69.             }
  70.         });
  71.         fileMenu.add(fileExitMenuItem);
  72.         JMenu settingsMenu = new JMenu("Impostazioni");
  73.         settingsMenu.setMnemonic(KeyEvent.VK_I);
  74.         settingsNormalRBMI = new JRadioButtonMenuItem("Gioco Normale", true);
  75.         settingsNormalRBMI.addActionListener(new ActionListener() {
  76.             public void actionPerformed(ActionEvent ae) {
  77.                 settingsFastRBMI.setSelected(false);
  78.                 settingsNormalRBMI.setSelected(true);
  79.             }
  80.         });
  81.         settingsNormalRBMI.setMnemonic(KeyEvent.VK_N);
  82.         settingsFastRBMI = new JRadioButtonMenuItem("Gioco Veloce", false);
  83.         settingsFastRBMI.setMnemonic(KeyEvent.VK_V);
  84.         settingsFastRBMI.addActionListener(new ActionListener() {
  85.             public void actionPerformed(ActionEvent ae) {
  86.                 settingsNormalRBMI.setSelected(false);
  87.                 settingsFastRBMI.setSelected(true);
  88.             }
  89.         });
  90.         settingsMenu.add(settingsNormalRBMI);
  91.         settingsMenu.add(settingsFastRBMI);
  92.         JMenu ladderMenu = new JMenu("Classifica");
  93.         ladderMenu.setMnemonic(KeyEvent.VK_C);
  94.         JMenuItem ladderLadderMenuItem = new JMenuItem("Classifica", KeyEvent.VK_L);
  95.         ladderLadderMenuItem.addActionListener(new ActionListener() {
  96.             public void actionPerformed(ActionEvent ae) {
  97.                 try {
  98.                     JOptionPane.showMessageDialog(null,
  99.                             "  1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
  100.                             "  2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
  101.                             "  3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
  102.                             "  4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
  103.                             "  5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
  104.                             "  6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
  105.                             "  7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
  106.                             "  8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
  107.                             "  9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
  108.                             "10- " + topTen[9] + ": " + topTenPoints[9]
  109.                             , "Classifica"
  110.                             ,JOptionPane.INFORMATION_MESSAGE);
  111.                 } catch (HeadlessException he) {}
  112.             }
  113.         });
  114.         ladderMenu.add(ladderLadderMenuItem);
  115.         JMenu helpMenu = new JMenu("Help");
  116.         helpMenu.setMnemonic(KeyEvent.VK_H);
  117.         JMenuItem helpStatMenuItem = new JMenuItem("Statistiche", KeyEvent.VK_S);
  118.         helpStatMenuItem.addActionListener(questionsListeners);
  119.         helpMenu.add(helpStatMenuItem);
  120.         JMenuItem helpAboutMenuItem = new JMenuItem("About", KeyEvent.VK_A);
  121.         helpAboutMenuItem.addActionListener(new ActionListener() {
  122.             public void actionPerformed(ActionEvent ae) {
  123.                 try {
  124.                     JOptionPane.showMessageDialog(null,
  125.                             "S-Quiz, sviluppato da Black Shadow " +
  126.                             "(Giovanni Casinelli).\n\n" +
  127.                             "Editor: NetBeans 5.5 Beta 2.\n\n" +
  128.                             "Per critiche, suggerimenti o Bug potete contattarmi " +
  129.                             "alla mail: blackshadow.gc@libero.it",
  130.                             "About",
  131.                             JOptionPane.INFORMATION_MESSAGE);
  132.                 } catch (HeadlessException he) {}
  133.             }
  134.          });
  135.         helpMenu.add(helpAboutMenuItem);
  136.         menuBar.add(fileMenu);
  137.         menuBar.add(settingsMenu);
  138.         menuBar.add(ladderMenu);
  139.         menuBar.add(helpMenu);
  140.         setJMenuBar(menuBar);
  141.         initLadder();
  142.     }
  143.     /**
  144.      *The main class that Start the Game
  145.      */
  146.     public static void main(String args[]) {
  147.         SQuiz squiz = new SQuiz();
  148.         squiz.setVisible(true);
  149.     }
  150.     /*Methods*/
  151.     public void newGame() {
  152.         player = new Player("Unidentified-Error");
  153.         try {
  154.             String tempName;
  155.             tempName = JOptionPane.showInputDialog("Inserisci il tuo nome:");
  156.             player.setName(tempName);
  157.         } catch (HeadlessException he) {
  158.         } finally {
  159.             questions = new Questions(player);
  160.             GameStart();
  161.         }
  162.     }
  163.     public void GameStart() {
  164.         questions.MakeQandA(questions);
  165.         MakeGUI((int) questions.getCasualNumber());
  166.     }
  167.     public void  MakeGUI(final int num) {
  168.         destroyGUI();
  169.         if (num != -1) {
  170.         Num = num;
  171.         setLayout(new BorderLayout());
  172.         question = new JLabel(questions.questions[player.getCurrentQ()-1][num]);
  173.         qPanel = new JPanel();
  174.         qPanel.setBorder(
  175.                 BorderFactory.createTitledBorder(player.getName() +
  176.                 " - Punteggio + " + player.getPoints() +
  177.                 " - Domanda " + player.getCurrentQ()));
  178.         qPanel.add(question);
  179.         ans1 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4]);
  180.         ans2 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 1]);
  181.         ans3 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 2]);
  182.         ans4 = new JRadioButton(questions.answer[player.getCurrentQ()-1][num * 4 + 3]);
  183.         ans1.addItemListener(this);
  184.         ans2.addItemListener(this);
  185.         ans3.addItemListener(this);
  186.         ans4.addItemListener(this);
  187.         aPanel = new JPanel();
  188.         aPanel.setBorder(BorderFactory.createTitledBorder("Risposte:"));
  189.         aPanel.add(ans1);
  190.         aPanel.add(ans2);
  191.         aPanel.add(ans3);
  192.         aPanel.add(ans4);
  193.         answers = new ButtonGroup();
  194.         answers.add(ans1); answers.add(ans2); answers.add(ans3); answers.add(ans4);
  195.         Answer = new JButton("Rispondi!");
  196.         Answer.addActionListener(new ActionListener() {
  197.             public void actionPerformed(ActionEvent ae) {
  198.                 if (settingsNormalRBMI.isSelected())
  199.                    isExactly(num);
  200.             }
  201.         });
  202.         APanel = new JPanel();
  203.         APanel.setBorder(BorderFactory.createTitledBorder("Rispondi!"));
  204.         APanel.add(Answer);
  205.         add(qPanel, BorderLayout.NORTH);
  206.         add(aPanel, BorderLayout.CENTER);
  207.         add(APanel, BorderLayout.SOUTH);
  208.         qPanel.repaint();
  209.         aPanel.repaint();
  210.         APanel.repaint();
  211.         repaint();
  212.         setVisible(true);
  213.         start = System.currentTimeMillis();
  214.         }
  215.     }
  216.     private void isExactly(int num) {
  217.         end = System.currentTimeMillis();
  218.         if (questions.exactly[player.getCurrentQ()-1][num].equals
  219.             (ans1.getText()) && ans1.isSelected())
  220.                 Continue();
  221.         else if (questions.exactly[player.getCurrentQ()-1][num].equals
  222.             (ans2.getText()) && ans2.isSelected())
  223.                 Continue();
  224.         else if (questions.exactly[player.getCurrentQ()-1][num].equals
  225.             (ans3.getText()) && ans3.isSelected())
  226.                 Continue();
  227.         else if (questions.exactly[player.getCurrentQ()-1][num].equals
  228.             (ans4.getText()) && ans4.isSelected())
  229.                 Continue();
  230.         else if (!ans1.isSelected() && !ans2.isSelected() && !ans3.isSelected() &&
  231.                 !ans4.isSelected()) {
  232.             try {
  233.                 JOptionPane.showMessageDialog(null, "Devi selezionare una risposta!",
  234.                         "Attenzione!", JOptionPane.WARNING_MESSAGE);
  235.             } catch (HeadlessException he) {}
  236.         }
  237.         else Break();
  238.     }
  239.     private void Continue() {
  240.         player.addPoints((int)((1000000+((end-start)*500))/((end - start)+1000)));
  241.         try {
  242.             if (player.getCurrentQ() < 15)
  243.                 JOptionPane.showMessageDialog(this, "Risposta Esatta!\n" +
  244.                     "Hai totalizzato " + (int)((1000000+((end-start)*500))/((end - start)+1000)) + " punti!\n" +
  245.                     "Adesso hai: " + player.getPoints() + " punti!\n" +
  246.                     "Passiamo alla Domanda " + (player.getCurrentQ() + 1) + "!",
  247.                     "Risposta Esatta!", JOptionPane.INFORMATION_MESSAGE);
  248.             else {
  249.                 JOptionPane.showMessageDialog(this, "Risposta Esatta! Complimenti! " +
  250.                         "Hai finito il gioco! Hai vinto! Tu si che sei un vero " +
  251.                         "intelligente!\n\n" +
  252.                         "Hai totalizzato " + player.getPoints() + " punti!",
  253.                         "Complimenti! Hai VINTO!",
  254.                         JOptionPane.INFORMATION_MESSAGE);
  255.                 destroyGUI();
  256.                 updateLadder();
  257.                 initLadder();
  258.                 try {
  259.                     JOptionPane.showMessageDialog(null,
  260.                             "  1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
  261.                             "  2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
  262.                             "  3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
  263.                             "  4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
  264.                             "  5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
  265.                             "  6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
  266.                             "  7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
  267.                             "  8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
  268.                             "  9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
  269.                             "10  - " + topTen[9] + ": " + topTenPoints[9]
  270.                             , "Classifica"
  271.                             ,JOptionPane.INFORMATION_MESSAGE);
  272.                 } catch (HeadlessException he) {}
  273.             }
  274.         } catch (HeadlessException he) {
  275.         } finally {
  276.             player.addCurrentQ();
  277.             MakeGUI((int) questions.getCasualNumber());
  278.         }
  279.     }
  280.     private void Break() {
  281.         try {
  282.             JOptionPane.showMessageDialog(this, "La risposta è sbagliata!" +
  283.                     " Mi dispiace!", "Errore!", JOptionPane.ERROR_MESSAGE);
  284.         } catch (HeadlessException he) {
  285.         } finally {
  286.             destroyGUI();
  287.             updateLadder();
  288.             initLadder();
  289.             try {
  290.                     JOptionPane.showMessageDialog(null,
  291.                             "  1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
  292.                             "  2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
  293.                             "  3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
  294.                             "  4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
  295.                             "  5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
  296.                             "  6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
  297.                             "  7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
  298.                             "  8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
  299.                             "  9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
  300.                             "10- " + topTen[9] + ": " + topTenPoints[9]
  301.                             , "Classifica"
  302.                             ,JOptionPane.INFORMATION_MESSAGE);
  303.                 } catch (HeadlessException he) {}
  304.         }
  305.     }
  306.     private void destroyGUI() {
  307.         try {
  308.             qPanel.removeAll();
  309.             aPanel.removeAll();
  310.             APanel.removeAll();
  311.             remove(qPanel);
  312.             remove(aPanel);
  313.             remove(APanel);
  314.             repaint();
  315.             setVisible(true);
  316.         } catch (NullPointerException npe) {
  317.         }
  318.     }
  319.     public int getTotQ() {
  320.         Questions testQ = new Questions();
  321.         return testQ.getTotQ();
  322.     }
  323.     private void initLadder() {
  324.         int i = 0, a = 0;
  325.         int pnt = 0;
  326.         String Str = "";
  327.         StringTokenizer seq;
  328.         try {
  329.             ladder = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
  330.             //Read the file and copy it into Str String variable.
  331.             do {
  332.                 try {
  333.                     i = ladder.read();
  334.                     if (i != -1) Str += (char) i;
  335.                 } catch (IOException ioe) {}
  336.             } while (i != -1);
  337.             seq = new StringTokenizer(Str, "\r\n", false);
  338.             String instr;
  339.             while (seq.hasMoreTokens()) {
  340.                 switch (pnt) {
  341.                     case 0:
  342.                         topTen[a] = seq.nextToken();
  343.                         pnt = 1;
  344.                         break;
  345.                     case 1:
  346.                         topTenPoints[a] = seq.nextToken();
  347.                         pnt = 0;
  348.                         if (a < 9) a++;
  349.                         else break;
  350.                         break;
  351.                 }
  352.             }
  353.         } catch (FileNotFoundException fnfe) {
  354.             System.out.println("Error: " + fnfe);
  355.         }
  356.     }
  357.     /*
  358.      *This method update the Ladder
  359.      */
  360.     private void updateLadder() {
  361.         int i = 0, b = 0, a;
  362.         String Str = "";
  363.         FileInputStream fin;
  364.         PrintStream pw;
  365.         StringTokenizer st;
  366.         try {
  367.             fin = new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
  368.             do {
  369.                 i = fin.read();
  370.                 if (i != -1) Str += (char) i;
  371.             } while (i != -1);
  372.             i = 0;
  373.             fin.close();
  374.             //find the position in the ladder
  375.             for (a = 0 ; a < 10 ; a++) {
  376.                 try {
  377.                     if (player.getPoints() == 0) {
  378.                         a = -1;
  379.                         break;
  380.                     }
  381.                     else if (Integer.toString(player.getPoints()).length() > topTenPoints[a].length())
  382.                         break;
  383.                     else if (Integer.toString(player.getPoints()).length() < topTenPoints[a].length())
  384.                         continue;
  385.                     else if (numberCompare(Integer.toString(player.getPoints()),topTenPoints[a]) == 1) {
  386.                         System.err.println(Integer.toString(player.getPoints()).length());
  387.                         System.err.println(topTenPoints[a].length());
  388.                         System.err.println(Integer.toString(player.getPoints()));
  389.                         System.err.println(topTenPoints[a]);
  390.                         break;
  391.                     }
  392.                 } catch (NumberFormatException nfe) {}
  393.             }
  394.             if (a != -1) {
  395.             if (a != 0) a *= 4;
  396.             st = new StringTokenizer(Str, "\n", true);
  397.             pw = new PrintStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
  398.             while (st.hasMoreTokens()) {
  399.                 if (a == b) {
  400.                     pw.printf("%s", player.getName());
  401.                     pw.println();
  402.                     //st.nextToken();
  403.                     b++;
  404.                 } else if ((a+1) == b) {
  405.                     pw.printf("%d", player.getPoints());
  406.                     pw.println();
  407.                     //st.nextToken();
  408.                     b++;
  409.                 } else {
  410.                     pw.printf("%s", st.nextToken());
  411.                     b++;
  412.                 }
  413.             }
  414.             pw.close();
  415.         }
  416.         } catch (IOException ioe) {}
  417.     }
  418.     /**
  419.      *@return 1 if str1 is greater than str2, else -1 if str1 is less str2.
  420.      *Return 0 if two String are equals
  421.      */
  422.     private static int numberCompare(String str1, String str2) {
  423.         if (Integer.parseInt(String.valueOf(str1.charAt(0))) >
  424.                 Integer.parseInt(String.valueOf(str2.charAt(0))))
  425.             return 1;
  426.         else if (Integer.parseInt(String.valueOf(str1.charAt(0))) <
  427.                     Integer.parseInt(String.valueOf(str2.charAt(0))))
  428.             return -1;
  429.         else if (Integer.parseInt(String.valueOf(str1.charAt(1))) >
  430.                 Integer.parseInt(String.valueOf(str2.charAt(1))))
  431.             return 1;
  432.         else if (Integer.parseInt(String.valueOf(str1.charAt(1))) <
  433.                     Integer.parseInt(String.valueOf(str2.charAt(1))))
  434.             return -1;
  435.         else if (Integer.parseInt(String.valueOf(str1.charAt(2))) >
  436.                 Integer.parseInt(String.valueOf(str2.charAt(2))))
  437.             return 1;
  438.         else if (Integer.parseInt(String.valueOf(str1.charAt(2))) <
  439.                     Integer.parseInt(String.valueOf(str2.charAt(2))))
  440.             return -1;
  441.         else if (Integer.parseInt(String.valueOf(str1.charAt(3))) >
  442.                 Integer.parseInt(String.valueOf(str2.charAt(3))))
  443.             return 1;
  444.         else if (Integer.parseInt(String.valueOf(str1.charAt(3))) <
  445.                     Integer.parseInt(String.valueOf(str2.charAt(3))))
  446.             return -1;
  447.         else if (Integer.parseInt(String.valueOf(str1.charAt(4))) >
  448.                 Integer.parseInt(String.valueOf(str2.charAt(4))))
  449.             return 1;
  450.         else if (Integer.parseInt(String.valueOf(str1.charAt(4))) <
  451.                     Integer.parseInt(String.valueOf(str2.charAt(4))))
  452.             return -1;
  453.         return 0;
  454.     }
  455.     public void itemStateChanged(ItemEvent ie) {
  456.         if (settingsFastRBMI.isSelected())
  457.             isExactly(Num);
  458.         }
  459. }