Guest User

Untitled

a guest
May 21st, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.95 KB | None | 0 0
  1. public void ecrireFichier(String user, String motPass) {
  2. this.user = user;
  3. this.motPass = motPass;
  4. try{
  5. BufferedWriter sortie = new BufferedWriter(new FileWriter("init.txt",true));
  6. //sortie.newLine();
  7. //sortie.newLine();
  8. sortie.write(user);
  9. //sortie.newLine();
  10. sortie.newLine();
  11. sortie.write(motPass);
  12. sortie.newLine();
  13. sortie.newLine();
  14.  
  15. JOptionPane.showMessageDialog(null," utilisateur enregistré! " );
  16. sortie.close();
  17. }
  18. catch(Exception ex){ ex.getMessage();
  19. }
  20.  
  21. }
  22. }
  23.  
  24. public class Question {
  25.  
  26. private char operator = '?';
  27. private int x, y;
  28. private int result;
  29. private boolean isCorrect;
  30.  
  31. public Question() {
  32. init();
  33. }
  34. //initie les questions
  35. private void init() {
  36. x = (int) Math.round(Math.random() * 20);
  37. y = (int) Math.round(Math.random() * 20);
  38. char[] ops = {'*', '-', '+'};
  39. operator = ops[(int) Math.round(Math.random() * 2)];
  40. switch (operator) {
  41. case '+': {
  42. result = x+y;
  43. break;
  44. }
  45. case '-': {
  46. result = x-y;
  47. break;
  48. }
  49. case '*': {
  50. result = x*y;
  51. break;
  52. }
  53. default: {
  54. throw new IllegalArgumentException("Unknown operator provided: "+operator);
  55. }
  56. }
  57. }
  58. //demande a l'usager de repondre
  59. private static int proposalA = 0, proposalB = 0, proposalC = 0, proposalD = 0;
  60. public String ask() {
  61. System.out.println();
  62. switch ((int) Math.round(Math.random()*3)) {
  63. case 0: {
  64. proposalA = result;
  65. proposalB = (int) Math.round(Math.random()*(1.5*result));
  66. proposalC = (int) Math.round(Math.random()*(1.5*result));
  67. proposalD = (int) Math.round(Math.random()*(1.5*result));
  68. break;
  69. }
  70. case 1: {
  71. proposalB = result;
  72. proposalA = (int) Math.round(Math.random()*(1.5*result));
  73. proposalC = (int) Math.round(Math.random()*(1.5*result));
  74. proposalD = (int) Math.round(Math.random()*(1.5*result));
  75. break;
  76. }
  77. case 2: {
  78. proposalC = result;
  79. proposalB = (int) Math.round(Math.random()*(1.5*result));
  80. proposalA = (int) Math.round(Math.random()*(1.5*result));
  81. proposalD = (int) Math.round(Math.random()*(1.5*result));
  82. break;
  83. }
  84. case 3: {
  85. proposalD = result;
  86. proposalB = (int) Math.round(Math.random()*(1.5*result));
  87. proposalC = (int) Math.round(Math.random()*(1.5*result));
  88. proposalA = (int) Math.round(Math.random()*(1.5*result));
  89. break;
  90. }
  91. default: {
  92. throw new IllegalArgumentException("None of the provided proposals have been chosen");
  93. }
  94. } // on calcule les r�ponses de fa�on al�atoire
  95. return x+" "+operator+" "+y+" = ? (Entrer une de ces choix de ru00E9ponse)n"+"t- A: "+proposalA+"nt- B: "+proposalB+"nt- C: "+proposalC+"nt- D: "+proposalD;
  96. // Et on retourne un {@link java.lang.String} contenant la question
  97. }
  98.  
  99. public int getResult() {
  100. return result; // on retourne le r�sultat de la question
  101. }
  102.  
  103. public boolean getPoint() {
  104. return isCorrect;
  105. }
  106. }
  107.  
  108. class MathNormalPanel extends JPanel {
  109.  
  110. JPanel mathNormalPanel=this;
  111. JButton startBouton, retourBouton;
  112. Questionnaire quest;
  113. JFrame maFrame;
  114.  
  115. public MathNormalPanel(JFrame frame) {
  116. setBackground(new Color(218, 165, 32));
  117.  
  118. GridLayout grid = new GridLayout(0, 1, 20, 20);
  119. setLayout(grid);
  120.  
  121. JButton startBouton = new JButton("Commencer");
  122. add(startBouton);
  123. startBouton.setBorder(null);
  124. startBouton.addActionListener(new ActionListener() {
  125. @Override
  126. public void actionPerformed(ActionEvent event) {
  127. String cmd = event.getActionCommand();
  128. Questionnaire mathN = new Questionnaire();
  129.  
  130. boolean isFinished = false;
  131.  
  132. int correct = 0; // pour avoir le nombre de r�ponses correctes
  133. int vies = 3;
  134. for (int i = 0; i < 15; i++) {
  135. boolean isCorrect = false;
  136. boolean isVies = false;
  137. boolean isValid = false;
  138. JOptionPane.showMessageDialog(mathNormalPanel, "Il vous reste:" +" "+ vies+" "+"de vies" + " et vous avez ru00E9pondu:" + " "+i + " questions", "Score de jeu",
  139. JOptionPane.INFORMATION_MESSAGE);
  140. while (!isValid && !isFinished) {
  141. String reponse = JOptionPane.showInputDialog(null, mathN.questions.get(i).ask()); // on questionne l'utilisateur
  142. try {
  143.  
  144. if(reponse == null || reponse == "" ){
  145. throw new NumberFormatException();
  146. }
  147.  
  148. Field field = Question.class.getDeclaredField("proposal" + String.valueOf(reponse).toUpperCase().charAt(0));
  149. field.setAccessible(true);
  150. int value = (int) field.get(0);
  151. isCorrect = value == mathN.questions.get(i).getResult(); // On obtient et verifie la reponse de l'utilisateur
  152. isValid = true;
  153. isVies = value != mathN.questions.get(i).getResult();
  154. if (vies <= 0) {
  155. final ImageIcon icon2 = new ImageIcon("src/images/gameover-3.jpg");
  156. JOptionPane.showMessageDialog(null, null, "Game Over", JOptionPane.INFORMATION_MESSAGE, icon2);
  157.  
  158. final ImageIcon icon = new ImageIcon("src/images/imageicon.jpg");
  159. JOptionPane.showMessageDialog(mathNormalPanel, "votre résultat final est: " +" "+ correct + "/15"+" avec:"+" "+vies+ " vies" ,"Score total", JOptionPane.INFORMATION_MESSAGE, icon);
  160. // open the sound file as a Java input stream
  161. // String gongFile = "src/sounds/YouDiedSound.mp3";
  162. // InputStream in = new FileInputStream(gongFile);
  163.  
  164. // create an audiostream from the inputstream
  165. // AudioStream audioStream = new AudioStream(in);
  166.  
  167. // play the audio clip with the audioplayer class
  168. // AudioPlayer.player.start(audioStream);
  169.  
  170.  
  171. isFinished = true;
  172. System.out.println("MAFRAME "+maFrame);
  173. maFrame.dispose();
  174. break;
  175. }
  176. } catch (NumberFormatException e){JOptionPane.showMessageDialog(null, "Button 'annuler' appuyu00E9 ", "Retour au menu", JOptionPane.ERROR_MESSAGE);
  177. maFrame.dispose(); }
  178. catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
  179. // TODO Auto-generated catch block
  180. //System.out.println("veillez entré une des valeurs présentées " + e.getMessage());
  181. //e.printStackTrace();
  182.  
  183. JOptionPane.showMessageDialog(null, "veuillez choisir une des valeurs présentées", "Erreur", JOptionPane.ERROR_MESSAGE);
  184. isValid = false;
  185.  
  186. } //catch (FileNotFoundException ex) {
  187. // Logger.getLogger(MathNormalFrame.class.getName()).log(Level.SEVERE, null, ex);
  188. // } catch (IOException ex) {
  189. // Logger.getLogger(MathNormalFrame.class.getName()).log(Level.SEVERE, null, ex);
  190. // }
  191. }
  192.  
  193. if (isFinished) {
  194. break;
  195. }
  196. if (isCorrect) {
  197. ++correct; // si la r�ponse est bonne on augmente le nombre de bonnes r�ponses
  198. }
  199. if (isVies) {
  200. --vies;
  201. }
  202. }
  203. if (vies > 1) {
  204. final ImageIcon icon = new ImageIcon("src/images/imageicon.jpg");
  205. JOptionPane.showMessageDialog(mathNormalPanel, "votre résultat final est: " + " "+correct + "/15"+" avec:"+" "+vies+ " vies" ,"Score total", JOptionPane.INFORMATION_MESSAGE, icon);
  206. }
  207. if (cmd.equals("Commencer") && !isFinished) {
  208. maFrame.dispose();
  209.  
  210. new NiveauPanel(frame).setVisible(true);
  211. }
  212. }
  213. });
  214.  
  215. JButton retourBouton = new JButton("Retour");
  216. add(retourBouton);
  217. retourBouton.setBorder(null);
  218. retourBouton.addActionListener(new ActionListener(){
  219. @Override
  220. public void actionPerformed(ActionEvent event) {
  221. String cmd = event.getActionCommand();
  222.  
  223. if (cmd.equals("Retour")) {
  224. try {
  225. frame.dispose();
  226. new MenuFrame().setVisible(true);
  227. }
  228. catch (IOException e) {
  229. JOptionPane.showMessageDialog(null, " Erreur" );
  230. }
  231. }
  232. }
  233. });
  234.  
  235. }
  236. }
Add Comment
Please, Sign In to add comment