Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void ecrireFichier(String user, String motPass) {
- this.user = user;
- this.motPass = motPass;
- try{
- BufferedWriter sortie = new BufferedWriter(new FileWriter("init.txt",true));
- //sortie.newLine();
- //sortie.newLine();
- sortie.write(user);
- //sortie.newLine();
- sortie.newLine();
- sortie.write(motPass);
- sortie.newLine();
- sortie.newLine();
- JOptionPane.showMessageDialog(null," utilisateur enregistré! " );
- sortie.close();
- }
- catch(Exception ex){ ex.getMessage();
- }
- }
- }
- public class Question {
- private char operator = '?';
- private int x, y;
- private int result;
- private boolean isCorrect;
- public Question() {
- init();
- }
- //initie les questions
- private void init() {
- x = (int) Math.round(Math.random() * 20);
- y = (int) Math.round(Math.random() * 20);
- char[] ops = {'*', '-', '+'};
- operator = ops[(int) Math.round(Math.random() * 2)];
- switch (operator) {
- case '+': {
- result = x+y;
- break;
- }
- case '-': {
- result = x-y;
- break;
- }
- case '*': {
- result = x*y;
- break;
- }
- default: {
- throw new IllegalArgumentException("Unknown operator provided: "+operator);
- }
- }
- }
- //demande a l'usager de repondre
- private static int proposalA = 0, proposalB = 0, proposalC = 0, proposalD = 0;
- public String ask() {
- System.out.println();
- switch ((int) Math.round(Math.random()*3)) {
- case 0: {
- proposalA = result;
- proposalB = (int) Math.round(Math.random()*(1.5*result));
- proposalC = (int) Math.round(Math.random()*(1.5*result));
- proposalD = (int) Math.round(Math.random()*(1.5*result));
- break;
- }
- case 1: {
- proposalB = result;
- proposalA = (int) Math.round(Math.random()*(1.5*result));
- proposalC = (int) Math.round(Math.random()*(1.5*result));
- proposalD = (int) Math.round(Math.random()*(1.5*result));
- break;
- }
- case 2: {
- proposalC = result;
- proposalB = (int) Math.round(Math.random()*(1.5*result));
- proposalA = (int) Math.round(Math.random()*(1.5*result));
- proposalD = (int) Math.round(Math.random()*(1.5*result));
- break;
- }
- case 3: {
- proposalD = result;
- proposalB = (int) Math.round(Math.random()*(1.5*result));
- proposalC = (int) Math.round(Math.random()*(1.5*result));
- proposalA = (int) Math.round(Math.random()*(1.5*result));
- break;
- }
- default: {
- throw new IllegalArgumentException("None of the provided proposals have been chosen");
- }
- } // on calcule les r�ponses de fa�on al�atoire
- return x+" "+operator+" "+y+" = ? (Entrer une de ces choix de ru00E9ponse)n"+"t- A: "+proposalA+"nt- B: "+proposalB+"nt- C: "+proposalC+"nt- D: "+proposalD;
- // Et on retourne un {@link java.lang.String} contenant la question
- }
- public int getResult() {
- return result; // on retourne le r�sultat de la question
- }
- public boolean getPoint() {
- return isCorrect;
- }
- }
- class MathNormalPanel extends JPanel {
- JPanel mathNormalPanel=this;
- JButton startBouton, retourBouton;
- Questionnaire quest;
- JFrame maFrame;
- public MathNormalPanel(JFrame frame) {
- setBackground(new Color(218, 165, 32));
- GridLayout grid = new GridLayout(0, 1, 20, 20);
- setLayout(grid);
- JButton startBouton = new JButton("Commencer");
- add(startBouton);
- startBouton.setBorder(null);
- startBouton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent event) {
- String cmd = event.getActionCommand();
- Questionnaire mathN = new Questionnaire();
- boolean isFinished = false;
- int correct = 0; // pour avoir le nombre de r�ponses correctes
- int vies = 3;
- for (int i = 0; i < 15; i++) {
- boolean isCorrect = false;
- boolean isVies = false;
- boolean isValid = false;
- JOptionPane.showMessageDialog(mathNormalPanel, "Il vous reste:" +" "+ vies+" "+"de vies" + " et vous avez ru00E9pondu:" + " "+i + " questions", "Score de jeu",
- JOptionPane.INFORMATION_MESSAGE);
- while (!isValid && !isFinished) {
- String reponse = JOptionPane.showInputDialog(null, mathN.questions.get(i).ask()); // on questionne l'utilisateur
- try {
- if(reponse == null || reponse == "" ){
- throw new NumberFormatException();
- }
- Field field = Question.class.getDeclaredField("proposal" + String.valueOf(reponse).toUpperCase().charAt(0));
- field.setAccessible(true);
- int value = (int) field.get(0);
- isCorrect = value == mathN.questions.get(i).getResult(); // On obtient et verifie la reponse de l'utilisateur
- isValid = true;
- isVies = value != mathN.questions.get(i).getResult();
- if (vies <= 0) {
- final ImageIcon icon2 = new ImageIcon("src/images/gameover-3.jpg");
- JOptionPane.showMessageDialog(null, null, "Game Over", JOptionPane.INFORMATION_MESSAGE, icon2);
- final ImageIcon icon = new ImageIcon("src/images/imageicon.jpg");
- JOptionPane.showMessageDialog(mathNormalPanel, "votre résultat final est: " +" "+ correct + "/15"+" avec:"+" "+vies+ " vies" ,"Score total", JOptionPane.INFORMATION_MESSAGE, icon);
- // open the sound file as a Java input stream
- // String gongFile = "src/sounds/YouDiedSound.mp3";
- // InputStream in = new FileInputStream(gongFile);
- // create an audiostream from the inputstream
- // AudioStream audioStream = new AudioStream(in);
- // play the audio clip with the audioplayer class
- // AudioPlayer.player.start(audioStream);
- isFinished = true;
- System.out.println("MAFRAME "+maFrame);
- maFrame.dispose();
- break;
- }
- } catch (NumberFormatException e){JOptionPane.showMessageDialog(null, "Button 'annuler' appuyu00E9 ", "Retour au menu", JOptionPane.ERROR_MESSAGE);
- maFrame.dispose(); }
- catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
- // TODO Auto-generated catch block
- //System.out.println("veillez entré une des valeurs présentées " + e.getMessage());
- //e.printStackTrace();
- JOptionPane.showMessageDialog(null, "veuillez choisir une des valeurs présentées", "Erreur", JOptionPane.ERROR_MESSAGE);
- isValid = false;
- } //catch (FileNotFoundException ex) {
- // Logger.getLogger(MathNormalFrame.class.getName()).log(Level.SEVERE, null, ex);
- // } catch (IOException ex) {
- // Logger.getLogger(MathNormalFrame.class.getName()).log(Level.SEVERE, null, ex);
- // }
- }
- if (isFinished) {
- break;
- }
- if (isCorrect) {
- ++correct; // si la r�ponse est bonne on augmente le nombre de bonnes r�ponses
- }
- if (isVies) {
- --vies;
- }
- }
- if (vies > 1) {
- final ImageIcon icon = new ImageIcon("src/images/imageicon.jpg");
- JOptionPane.showMessageDialog(mathNormalPanel, "votre résultat final est: " + " "+correct + "/15"+" avec:"+" "+vies+ " vies" ,"Score total", JOptionPane.INFORMATION_MESSAGE, icon);
- }
- if (cmd.equals("Commencer") && !isFinished) {
- maFrame.dispose();
- new NiveauPanel(frame).setVisible(true);
- }
- }
- });
- JButton retourBouton = new JButton("Retour");
- add(retourBouton);
- retourBouton.setBorder(null);
- retourBouton.addActionListener(new ActionListener(){
- @Override
- public void actionPerformed(ActionEvent event) {
- String cmd = event.getActionCommand();
- if (cmd.equals("Retour")) {
- try {
- frame.dispose();
- new MenuFrame().setVisible(true);
- }
- catch (IOException e) {
- JOptionPane.showMessageDialog(null, " Erreur" );
- }
- }
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment