import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.StringTokenizer;
import java.io.*;
/**
*The S-Quiz main class
*@author Black Shadow (Giovanni Casinelli)
*/
/*Variabili*/
Player player; //The player
Questions questions; //The questions
JLabel question
; //The question
JButton Answer
; //The Button to Answer at the question.
JPanel qPanel, aPanel, APanel
; //The Panels for the Game.
try {
"In totale ci sono " +
getTotQ() +
" domande.",
"Domande Totali.",
} catch (HeadlessException he) {}
}
};
int Num; //Register the last num param of MakeGUI();
long start, end; //Used for currentTimeMillis() function.
/**
*S-Quiz constructor
*/
SQuiz() {
setTitle("S-Quiz! Riuscirai ad arrivare alla fine?");
setSize(640, 320);
setResizable(false);
}
});
newGame();
}
});
fileMenu.add(fileNewMenuItem);
}
});
fileMenu.add(fileExitMenuItem);
settingsMenu.
setMnemonic(KeyEvent.
VK_I);
settingsFastRBMI.setSelected(false);
settingsNormalRBMI.setSelected(true);
}
});
settingsNormalRBMI.
setMnemonic(KeyEvent.
VK_N);
settingsFastRBMI.
setMnemonic(KeyEvent.
VK_V);
settingsNormalRBMI.setSelected(false);
settingsFastRBMI.setSelected(true);
}
});
settingsMenu.add(settingsNormalRBMI);
settingsMenu.add(settingsFastRBMI);
try {
" 1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
" 2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
" 3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
" 4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
" 5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
" 6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
" 7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
" 8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
" 9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
"10- " + topTen[9] + ": " + topTenPoints[9]
, "Classifica"
} catch (HeadlessException he) {}
}
});
ladderMenu.add(ladderLadderMenuItem);
helpStatMenuItem.addActionListener(questionsListeners);
helpMenu.add(helpStatMenuItem);
try {
"S-Quiz, sviluppato da Black Shadow " +
"(Giovanni Casinelli).\n\n" +
"Editor: NetBeans 5.5 Beta 2.\n\n" +
"Per critiche, suggerimenti o Bug potete contattarmi " +
"alla mail: blackshadow.gc@libero.it",
"About",
} catch (HeadlessException he) {}
}
});
helpMenu.add(helpAboutMenuItem);
menuBar.add(fileMenu);
menuBar.add(settingsMenu);
menuBar.add(ladderMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
initLadder();
}
/**
*The main class that Start the Game
*/
public static void main
(String args
[]) {
SQuiz squiz = new SQuiz();
squiz.setVisible(true);
}
/*Methods*/
public void newGame() {
player = new Player("Unidentified-Error");
try {
tempName
= JOptionPane.
showInputDialog("Inserisci il tuo nome:");
player.setName(tempName);
} catch (HeadlessException he) {
} finally {
questions = new Questions(player);
GameStart();
}
}
public void GameStart() {
questions.MakeQandA(questions);
MakeGUI((int) questions.getCasualNumber());
}
public void MakeGUI(final int num) {
destroyGUI();
if (num != -1) {
Num = num;
question
= new JLabel(questions.
questions[player.
getCurrentQ()-1
][num
]);
qPanel.setBorder(
" - Punteggio + " + player.getPoints() +
" - Domanda " + player.getCurrentQ()));
qPanel.add(question);
ans1
= new JRadioButton(questions.
answer[player.
getCurrentQ()-1
][num
* 4
]);
ans2
= new JRadioButton(questions.
answer[player.
getCurrentQ()-1
][num
* 4
+ 1
]);
ans3
= new JRadioButton(questions.
answer[player.
getCurrentQ()-1
][num
* 4
+ 2
]);
ans4
= new JRadioButton(questions.
answer[player.
getCurrentQ()-1
][num
* 4
+ 3
]);
ans1.addItemListener(this);
ans2.addItemListener(this);
ans3.addItemListener(this);
ans4.addItemListener(this);
aPanel.add(ans1);
aPanel.add(ans2);
aPanel.add(ans3);
aPanel.add(ans4);
answers.add(ans1); answers.add(ans2); answers.add(ans3); answers.add(ans4);
if (settingsNormalRBMI.isSelected())
isExactly(num);
}
});
APanel.add(Answer);
qPanel.repaint();
aPanel.repaint();
APanel.repaint();
repaint();
setVisible(true);
start
= System.
currentTimeMillis();
}
}
private void isExactly(int num) {
end
= System.
currentTimeMillis();
if (questions.exactly[player.getCurrentQ()-1][num].equals
(ans1.getText()) && ans1.isSelected())
Continue();
else if (questions.exactly[player.getCurrentQ()-1][num].equals
(ans2.getText()) && ans2.isSelected())
Continue();
else if (questions.exactly[player.getCurrentQ()-1][num].equals
(ans3.getText()) && ans3.isSelected())
Continue();
else if (questions.exactly[player.getCurrentQ()-1][num].equals
(ans4.getText()) && ans4.isSelected())
Continue();
else if (!ans1.isSelected() && !ans2.isSelected() && !ans3.isSelected() &&
!ans4.isSelected()) {
try {
JOptionPane.
showMessageDialog(null,
"Devi selezionare una risposta!",
} catch (HeadlessException he) {}
}
else Break();
}
private void Continue() {
player.addPoints((int)((1000000+((end-start)*500))/((end - start)+1000)));
try {
if (player.getCurrentQ() < 15)
JOptionPane.
showMessageDialog(this,
"Risposta Esatta!\n" +
"Hai totalizzato " + (int)((1000000+((end-start)*500))/((end - start)+1000)) + " punti!\n" +
"Adesso hai: " + player.getPoints() + " punti!\n" +
"Passiamo alla Domanda " + (player.getCurrentQ() + 1) + "!",
else {
JOptionPane.
showMessageDialog(this,
"Risposta Esatta! Complimenti! " +
"Hai finito il gioco! Hai vinto! Tu si che sei un vero " +
"intelligente!\n\n" +
"Hai totalizzato " + player.getPoints() + " punti!",
"Complimenti! Hai VINTO!",
destroyGUI();
updateLadder();
initLadder();
try {
" 1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
" 2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
" 3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
" 4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
" 5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
" 6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
" 7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
" 8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
" 9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
"10 - " + topTen[9] + ": " + topTenPoints[9]
, "Classifica"
} catch (HeadlessException he) {}
}
} catch (HeadlessException he) {
} finally {
player.addCurrentQ();
MakeGUI((int) questions.getCasualNumber());
}
}
private void Break() {
try {
JOptionPane.
showMessageDialog(this,
"La risposta è sbagliata!" +
" Mi dispiace!",
"Errore!",
JOptionPane.
ERROR_MESSAGE);
} catch (HeadlessException he) {
} finally {
destroyGUI();
updateLadder();
initLadder();
try {
" 1 - " + topTen[0] + ": " + topTenPoints[0] + "\n" +
" 2 - " + topTen[1] + ": " + topTenPoints[1] + "\n" +
" 3 - " + topTen[2] + ": " + topTenPoints[2] + "\n" +
" 4 - " + topTen[3] + ": " + topTenPoints[3] + "\n" +
" 5 - " + topTen[4] + ": " + topTenPoints[4] + "\n" +
" 6 - " + topTen[5] + ": " + topTenPoints[5] + "\n" +
" 7 - " + topTen[6] + ": " + topTenPoints[6] + "\n" +
" 8 - " + topTen[7] + ": " + topTenPoints[7] + "\n" +
" 9 - " + topTen[8] + ": " + topTenPoints[8] + "\n" +
"10- " + topTen[9] + ": " + topTenPoints[9]
, "Classifica"
} catch (HeadlessException he) {}
}
}
private void destroyGUI() {
try {
qPanel.removeAll();
aPanel.removeAll();
APanel.removeAll();
remove(qPanel);
remove(aPanel);
remove(APanel);
repaint();
setVisible(true);
}
}
public int getTotQ() {
Questions testQ = new Questions();
return testQ.getTotQ();
}
private void initLadder() {
int i = 0, a = 0;
int pnt = 0;
try {
ladder
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
//Read the file and copy it into Str String variable.
do {
try {
i = ladder.read();
if (i != -1) Str += (char) i;
} while (i != -1);
while (seq.hasMoreTokens()) {
switch (pnt) {
case 0:
topTen[a] = seq.nextToken();
pnt = 1;
break;
case 1:
topTenPoints[a] = seq.nextToken();
pnt = 0;
if (a < 9) a++;
else break;
break;
}
}
System.
out.
println("Error: " + fnfe
);
}
}
/*
*This method update the Ladder
*/
private void updateLadder() {
int i = 0, b = 0, a;
try {
fin
= new FileInputStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
do {
i = fin.read();
if (i != -1) Str += (char) i;
} while (i != -1);
i = 0;
fin.close();
//find the position in the ladder
for (a = 0 ; a < 10 ; a++) {
try {
if (player.getPoints() == 0) {
a = -1;
break;
}
else if (Integer.
toString(player.
getPoints()).
length() > topTenPoints
[a
].
length())
break;
else if (Integer.
toString(player.
getPoints()).
length() < topTenPoints
[a
].
length())
continue;
else if (numberCompare
(Integer.
toString(player.
getPoints()),topTenPoints
[a
]) == 1
) {
System.
err.
println(Integer.
toString(player.
getPoints()).
length());
System.
err.
println(topTenPoints
[a
].
length());
System.
err.
println(topTenPoints
[a
]);
break;
}
}
if (a != -1) {
if (a != 0) a *= 4;
pw
= new PrintStream("C:\\Programmi\\S-Quiz\\build\\Databases\\dbLadder.txt");
while (st.hasMoreTokens()) {
if (a == b) {
pw.printf("%s", player.getName());
pw.println();
//st.nextToken();
b++;
} else if ((a+1) == b) {
pw.printf("%d", player.getPoints());
pw.println();
//st.nextToken();
b++;
} else {
pw.printf("%s", st.nextToken());
b++;
}
}
pw.close();
}
}
/**
*@return 1 if str1 is greater than str2, else -1 if str1 is less str2.
*Return 0 if two String are equals
*/
private static int numberCompare
(String str1,
String str2
) {
return 1;
return -1;
return 1;
return -1;
return 1;
return -1;
return 1;
return -1;
return 1;
return -1;
return 0;
}
if (settingsFastRBMI.isSelected())
isExactly(Num);
}
}