-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTicToy.java
233 lines (176 loc) · 5.76 KB
/
TicToy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.URL;
import javax.sound.sampled.*;
public class TicToy extends JFrame implements KeyListener
{
JButton jb[]=new JButton[16];
int n,j;
TicToy()
{
int n[]=new int[15];
setVisible(true);
setLocationRelativeTo(null);
setLayout(new GridLayout(4,4,4,4));
Dimension size=Toolkit.getDefaultToolkit().getScreenSize();
setSize(500,500);
setTitle("TicToy for Childrens of class 5th");
n=Generate(n);
for(int i=0;i<15;i++)
{
jb[i]=new JButton(""+n[i]+"");
add(jb[i]);
jb[i].addKeyListener(this);
}
jb[15]=new JButton("");
add(jb[15]);
jb[15].addKeyListener(this);
System.out.println(n);
}
public static void main(String []arr)
{
TicToy ob=new TicToy();
}
public int[] Generate(int n[])
{
for(int i=0;i<n.length;i++)
{
n[i]=i;
}
for (int i=0; i<n.length; i++) {
Random rm=new Random();
int randomPosition = rm.nextInt(15);
int temp = n[i];
n[i] = n[randomPosition];
n[randomPosition] = temp;
}
for(int i=0;i<n.length;i++)
{ System.out.println(n[i]);}
return n;
}
public void keyTyped(KeyEvent e)
{
int count=0;
if(e.getKeyChar()==122)
{
for(int i=0;i<jb.length;i++)
{
String s=jb[i].getText();
count++;
if((s.equals("")))
{
j=count;
}
}
try { jb[j-1].setText(jb[j-5].getText());
jb[j-5].setText("");
startBGMusic("BITE.wav");
}
catch(ArrayIndexOutOfBoundsException e2)
{
startBGMusic("error.wav");
JOptionPane.showMessageDialog(null,"YOU Are Idiot NO Move Possible Here!!","Warning",JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getKeyChar()==119)
{
count=0;
for(int i=0;i<jb.length;i++)
{
String s=jb[i].getText();
count++;
if((s.equals("")))
{
j=count;
}
}
try { jb[j-1].setText(jb[j+3].getText());
jb[j+3].setText("");
startBGMusic("BITE.wav");
}
catch(ArrayIndexOutOfBoundsException e2)
{
startBGMusic("error.wav");
JOptionPane.showMessageDialog(null,"YOU Are Idiot NO Move Possible Here!!","Warning",JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getKeyChar()==97)
{
count=0;
for(int i=0;i<jb.length;i++)
{
String s=jb[i].getText();
count++;
if((s.equals("")))
{
j=count;
}
}
System.out.println(count); System.out.println(j);
jb[j-1].setText(jb[j].getText());
jb[j].setText("");
startBGMusic("BITE.wav");
try{
if(j==4 || j==8 || j==12 || j==16)
throw new ArithmeticException();
}
catch(ArithmeticException e1)
{
jb[j].setText(jb[j-1].getText());
jb[j-1].setText("");
startBGMusic("error.wav");
JOptionPane.showMessageDialog(null,"YOU Are Idiot NO Move Possible Here!!","Warning",JOptionPane.WARNING_MESSAGE);
}
}
else if(e.getKeyChar()==100)
{
count=0;
for(int i=0;i<jb.length;i++)
{
String s=jb[i].getText();
count++;
if((s.equals("")))
{
j=count;
}
} System.out.println(count); System.out.println(j);
jb[j-1].setText(jb[j-2].getText());
jb[j-2].setText("");
startBGMusic("BITE.wav");
try{
if(j==1 || j==5 || j==9 || j==13)
throw new ArithmeticException();
}
catch(ArithmeticException e1)
{
jb[j-2].setText(jb[j-1].getText());
jb[j-1].setText("");
startBGMusic("error.wav");
JOptionPane.showMessageDialog(null,"YOU Are Idiot NO Move Possible Here!!","Warning",JOptionPane.WARNING_MESSAGE);
}
}
}
public void startBGMusic(String music) {
try {
// Open an audio input stream.
URL url = this.getClass().getClassLoader().getResource(music);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
// Get a sound clip resource.
Clip clip = AudioSystem.getClip();
// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}
public void keyReleased(KeyEvent e){}
public void keyPressed(KeyEvent e){}
}