Advertisement
Guest User

Untitled

a guest
May 27th, 2015
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.util.*;
  4. import javax.swing.*;
  5.  
  6. public class Puzzle implements WindowListener,ActionListener
  7. {
  8. JFrame mf;
  9. JPanel p1,p2;
  10. JButton jb[],rb;
  11. JTextField cc;
  12. JLabel lcc;
  13. int eb;
  14. int ccnt;
  15. void initbl()
  16. {
  17. for (int i=0;i<9;i++)
  18. jb[i].setText("0");
  19. jb[8].setText("");
  20. }
  21. Puzzle()
  22. {
  23. eb=8;
  24. mf=new JFrame("NLabs' Puzzle");
  25. p1=new JPanel();
  26. p2=new JPanel();
  27. jb=new JButton[9];
  28. rb=new JButton("Randomize");
  29. for (int i=0;i<9;i++)
  30. jb[i]=new JButton("0");
  31. jb[8].setText("");
  32. cc=new JTextField(5);
  33. lcc=new JLabel("Click Count: ");
  34. p1.setLayout(new GridLayout(3, 3));
  35. for (int i=0;i<9;i++)
  36. p1.add(jb[i]);
  37. p2.add(lcc);
  38. p2.add(cc);
  39. p2.add(rb);
  40. (mf.getContentPane()).setLayout(new GridLayout(2, 1));
  41. (mf.getContentPane()).add(p1);
  42. (mf.getContentPane()).add(p2);
  43. mf.setSize(200, 200);
  44. mf.setVisible(true);
  45. mf.addWindowListener(this);
  46. mf.setResizable(false);
  47. rnd();
  48. rb.addActionListener(this);
  49. for (int i=0;i<9;i++)
  50. jb[i].addActionListener(this);
  51. ccnt=0;
  52. dccnt();
  53. }
  54. void dccnt()
  55. {
  56. cc.setText(Integer.toString(ccnt));
  57. }
  58. void rnd()
  59. {
  60. Random r=new Random();
  61. for (int i=0;i<8;)
  62. {
  63. int j=r.nextInt(9);
  64. if (jb[j].getText().equals("0"))
  65. {
  66. jb[j].setText(Integer.toString(i+1));
  67. i++;
  68. }
  69. }
  70. }
  71. public static void main(String a[])
  72. {
  73. new Puzzle();
  74. }
  75.  
  76. public void windowOpened(WindowEvent we)
  77. {
  78.  
  79. }
  80.  
  81. public void windowClosing(WindowEvent we)
  82. {
  83. System.exit(0);
  84. }
  85.  
  86. public void windowClosed(WindowEvent we)
  87. {
  88.  
  89. }
  90.  
  91. public void windowIconified(WindowEvent we)
  92. {
  93.  
  94. }
  95.  
  96. public void windowDeiconified(WindowEvent we)
  97. {
  98.  
  99. }
  100.  
  101. public void windowActivated(WindowEvent we)
  102. {
  103.  
  104. }
  105.  
  106. public void windowDeactivated(WindowEvent we)
  107. {
  108.  
  109. }
  110. void chek()
  111. {
  112. boolean go=false;
  113. for (int i=0;i<8;i++)
  114. {
  115. if (jb[i].getText().equals(Integer.toString(i+1)))
  116. {
  117. go=true;
  118. }
  119. else
  120. {
  121. go=false;
  122. break;
  123. }
  124. }
  125. if (go)
  126. {
  127. JOptionPane jp=new JOptionPane();
  128. jp.showMessageDialog(mf,"You have completed the puzzle in "+ ccnt +" counts.");
  129. ccnt=0;
  130. initbl();
  131. rnd();
  132. dccnt();
  133. }
  134. }
  135. public void actionPerformed(ActionEvent ae)
  136. {
  137. if (ae.getSource()==rb)
  138. {
  139. ccnt=0;
  140. initbl();
  141. rnd();
  142. dccnt();
  143. return;
  144. }
  145. for (int i=0;i<9;i++)
  146. {
  147. if (ae.getSource()==jb[i])
  148. {
  149. ccnt++;
  150. dccnt();
  151. if (eb!=i)
  152. {
  153. jb[eb].setText(jb[i].getText());
  154. eb=i;
  155. jb[eb].setText("");
  156. chek();
  157. }
  158. }
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement