Example of Puzzle Game
1. import java.awt.*;
2. import javax.swing.*;
3. import java.awt.event.*;
4. public class puzzle extends JFrame implements ActionListener{
5. JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,next;
6. puzzle(){
7. super("puzzle");
8. b1=new JButton("1");
9. b2=new JButton(" ");
10. b3=new JButton("3");
11. b4=new JButton("4");
12. b5=new JButton("5");
13. b6=new JButton("6");
14. b7=new JButton("7");
15. b8=new JButton("8");
16. b9=new JButton("2");
17. next=new JButton("next");
18.
19. b1.setBounds(10,30,50,40);
20. b2.setBounds(70,30,50,40);
21. b3.setBounds(130,30,50,40);
22. b4.setBounds(10,80,50,40);
23. b5.setBounds(70,80,50,40);
24. b6.setBounds(130,80,50,40);
25. b7.setBounds(10,130,50,40);
26. b8.setBounds(70,130,50,40);
27. b9.setBounds(130,130,50,40);
28. next.setBounds(70,200,100,40);
29.
30. add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);add(b9); add(next);
31. b1.addActionListener(this);
32. b2.addActionListener(this);
33. b3.addActionListener(this);
34. b4.addActionListener(this);
35. b5.addActionListener(this);
36. b6.addActionListener(this);
37. b7.addActionListener(this);
38. b8.addActionListener(this);
39. b9.addActionListener(this);
40. next.addActionListener(this);
41.
42. next.setBackground(Color.black);
43. next.setForeground(Color.green);
44. setSize(250,300);
45. setLayout(null);
46. setVisible(true);
47. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
48. }//end of constructor
49.
50. public void actionPerformed(ActionEvent e){
51. if(e.getSource()==next){
52. String s=b4.getLabel();
53. b4.setLabel(b9.getLabel());
54. b9.setLabel(s);
55. s=b1.getLabel();
56. b1.setLabel(b5.getLabel());
57. b5.setLabel(s);
58. s=b2.getLabel();
59. b2.setLabel(b7.getLabel());
60. b7.setLabel(s);
61. }
62. if(e.getSource()==b1){
63. String s=b1.getLabel();
64. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b1.setLabel(" ");}
65. else if(b4.getLabel().equals(" ")){ b4.setLabel(s); b1.setLabel(" ");}
66. }//end of if
67.
68. if(e.getSource()==b3){
69. String s=b3.getLabel();
70. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b3.setLabel(" ");}
71. else if(b6.getLabel().equals(" ")){ b6.setLabel(s); b3.setLabel(" ");}
72. }//end of if
73.
74. if(e.getSource()==b2){
75. String s=b2.getLabel();
76. if(b1.getLabel().equals(" ")){ b1.setLabel(s); b2.setLabel(" ");}
77. else if(b3.getLabel().equals(" ")){ b3.setLabel(s); b2.setLabel(" ");}
78. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b2.setLabel(" ");}
79. }//end of if
80.
81. if(e.getSource()==b4){
82. String s=b4.getLabel();
83. if(b1.getLabel().equals(" ")){ b1.setLabel(s); b4.setLabel(" ");}
84. else if(b7.getLabel().equals(" ")){ b7.setLabel(s); b4.setLabel(" ");}
85. else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b4.setLabel(" ");}
86. }//end of if
87.
88. if(e.getSource()==b5){
89. String s=b5.getLabel();
90. if(b2.getLabel().equals(" ")){ b2.setLabel(s); b5.setLabel(" ");}
91. else if(b4.getLabel().equals(" ")){ b4.setLabel(s); b5.setLabel(" ");}
92. else if(b6.getLabel().equals(" ")){ b6.setLabel(s); b5.setLabel(" ");}
93. else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b5.setLabel(" ");}
94. }//end of if
95.
96. if(e.getSource()==b6){
97.
98. String s=b6.getLabel();
99. if(b9.getLabel().equals(" ")){ b9.setLabel(s); b6.setLabel(" ");}
100.
else if(b3.getLabel().equals(" ")){ b3.setLabel(s); b6.setLabel(" ");}
101.
else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b6.setLabel(" ");}
102.
103.
}//end of if
104.
105.
if(e.getSource()==b7){
106.
String s=b7.getLabel();
107.
if(b4.getLabel().equals(" ")){ b4.setLabel(s); b7.setLabel(" ");}
108.
else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b7.setLabel(" ");}
109.
110.
}//end of if
111.
112.
if(e.getSource()==b8){
113.
String s=b8.getLabel();
114.
if(b7.getLabel().equals(" ")){ b7.setLabel(s); b8.setLabel(" ");}
115.
else if(b9.getLabel().equals(" ")){ b9.setLabel(s); b8.setLabel(" ");}
116.
else if(b5.getLabel().equals(" ")){ b5.setLabel(s); b8.setLabel(" ");}
117.
118.
}//end of if
119.
120.
if(e.getSource()==b9){
121.
String s=b9.getLabel();
122.
if(b6.getLabel().equals(" ")){ b6.setLabel(s); b9.setLabel(" ");}
123.
else if(b8.getLabel().equals(" ")){ b8.setLabel(s); b9.setLabel(" ");}
124.
if(b1.getLabel().equals("1")&&b2.getLabel().equals("2")&&b3.getLabel()
125.
.equals("3")&&b4.getLabel().equals("4")&&b5.getLabel().equals("5")
126.
&&b6.getLabel().equals("6")&&b7.getLabel().equals("7")&&b8.getLabel()
127.
.equals("8")&&b9.getLabel().equals(" ")){
128.
JOptionPane.showMessageDialog(puzzle.this,"!!!you won!!!");
129.
}
130.
}//end of if
131.
132.
}//end of actionPerformed
133.
134.
135.
public static void main(String[] args){
136.
new puzzle();
137.
}//end of main
138.
139.
}//end of class
No comments:
Post a Comment