-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.java
More file actions
executable file
·294 lines (275 loc) · 5.66 KB
/
memory.java
File metadata and controls
executable file
·294 lines (275 loc) · 5.66 KB
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class memory {
/**
* @param args
*/
static int x1=0,y1=0, x2=0, y2=0;
static int pa;
static int pb;
static int i;
static int j;
static int step;
final static ImageIcon im[][]=new ImageIcon[4][4];
static JFrame f;
static int bit;
static JPanel p1;
JPanel p2;
JPanel p3;
static JButton b[][]=new JButton[4][4];
public static void main(String[] args) {
// TODO Auto-generated method stub
f=new JFrame();
p1=new JPanel();
f.setVisible(true);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p1);
f.setSize(400,400);
p1.setLayout(new GridLayout(4,4));
/*im[0][0]=new ImageIcon("picture001.jpg");
b[0][0]=new JButton(im[0][0]);
//p1.add(b[0][0]);
ImageIcon water = new ImageIcon("src/picture001.jpg");
JButton button = new JButton(water);
p1.add(button);
*/
String[] str={"src/116.jpg","src/download.jpg","src/op.jpg","src/ui.jpg","src/yu.jpg","src/yt.jpg","src/images.jpg","src/im.jpg","src/be.jpg","src/kl.jpg","src/mb.jpg","src/io.jpg","src/bru.jpg","src/you.jpg"};
/*File imageCheck = new File("src/op.jpg");
if(imageCheck.exists())
System.out.println("Image file found!");
else
System.out.println("Image file not found!");
*/int k=0;
int u=0,p=0;
int[] tarr = new int[20];
int flag=0;
for( i = 0;i<4;i++)
{
if(i==2)
break;
for(j=0;j<4;j++)
{
//System.out.println("i"+i+"j"+j);
Random r=new Random();
do
{
u=r.nextInt()%13;
if(u<0)
u=-u;
//System.out.println(u);
//im[0][0]=new ImageIcon("src/116.jpg");
flag=0;
for(int o = 0;o<p;o++)
{// System.out.println("u"+u+"tarr"+tarr[o]);
if(tarr[o]==u)
{
// System.out.println(u+""+tarr[o]);
flag++;
}
}
// System.out.println("flg"+flag);
if(flag<2)
{
// System.out.println("ass");
tarr[p++]=u;
break;
}
//System.out.println("repeat");
}while(true);
// System.out.println("out");
im[i][j]=new ImageIcon(str[u]);
/* b[i][j]=new JButton(im[i][j]);
b[i][j].set);
p1.add(b[i][j]);*/
if(k<9)
k++;
}
}
int t;
for(i=2;i<4;i++)
{
for(j=0;j<4;j++)
{
for(t=0;t<p;t++)
{
for(k=0;k<p;k++)
{
if((tarr[t]==tarr[k])&&(k!=t))
{
break;
}
}
if(k==p)
break;
}
if(t!=p)
{
int tw=tarr[t];
tarr[p++]=tw;
im[i][j]=new ImageIcon(str[tarr[t]]);
/* b[i][j]=new JButton(im[i][j]);
p1.add(b[i][j]);
*/
}
else
{
//copy
Random r=new Random();
do
{
u=r.nextInt()%13;
if(u<0)
u=-u;
// System.out.println(u);
//im[0][0]=new ImageIcon("src/116.jpg");
flag=0;
for(int o = 0;o<p;o++)
{ //System.out.println("u"+u+"tarr"+tarr[o]);
if(tarr[o]==u)
{
// System.out.println(u+""+tarr[o]);
flag++;
}
}
//System.out.println("flg"+flag);
if(flag<2)
{
// System.out.println("ass");
tarr[p++]=u;
break;
}
//System.out.println("repeat");
}while(true);
//System.out.println("out");
im[i][j]=new ImageIcon(str[u]);
/*b[i][j]=new JButton(im[i][j]);
p1.add(b[i][j]);
*/
//end
}
}
}
shuffle(im);
final ImageIcon def=new ImageIcon("src/def.jpg");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
b[i][j]=new JButton();
// b[i][j].setBorderPainted(true);
//b[i][j].setIcon(im[i][j]);
p1.add(b[i][j]);
b[i][j].setIcon(def);
final int r1=i;
final int r2=j;
b[i][j].addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(b[r1][r2].getIcon().toString().equals("src/def.jpg"))
{
step++;
b[r1][r2].setIcon(im[r1][r2]);
if(bit==2)
{
// System.out.println("bit 2"+x1+""+y1+""+x2+""+y2);
bit=0;
// System.out.println(b[x1][y1].getIcon()+","+b[x2][y2].getIcon());
// String s1=b[x2][y2].getIcon();
if(b[x1][y1].getIcon().toString()==(b[x2][y2].getIcon().toString()))
{
// System.out.println("equal");
}
else
{
b[x1][y1].setIcon(def);
b[x2][y2].setIcon(def);
}
x1=r1;
y1=r2;
}
else if(bit==0)
{
// System.out.println("bit 0");
x1=r1;
y1=r2;
}
else
{
// System.out.println("bit 1");
x2=r1;
y2=r2;
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(b[i][j].getIcon().toString().equals("src/def.jpg"))
{
break;
}
}
if(j!=4)
break;
}
int res = JOptionPane.YES_NO_OPTION;
if(i==4)
{
res=JOptionPane.showConfirmDialog(f, "CONGRATS you won by "+step+" moves!!!Would u like to continue??");
if(res==JOptionPane.YES_OPTION)
{
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
// b[i][j].setForeground(Color.BLACK);
b[i][j].setIcon(def);
}
step=0;
shuffle(im);
bit=0;
}
else
{
//int p1=(Integer.parseInt(l3.getText())+Integer.parseInt(l4.getText())+Integer.parseInt(l6.getText()));
JOptionPane.showMessageDialog(f, "thanks for playing!!!");
f.setVisible(false);
}
}
}
bit++;
}
}
});
}
}
}
private static void shuffle(ImageIcon[][] im2) {
// TODO Auto-generated method stub
for(i=0;i<500;i++)
{
Random r=new Random();
int y1=r.nextInt()%4;
int x1=r.nextInt()%4;
int y2=r.nextInt()%4;
int x2=r.nextInt()%4;
if(x1<0)
x1=-x1;
if(y1<0)
y1=-y1;
if(x2<0)
x2=-x2;
if(y2<0)
y2=-y2;
ImageIcon t=im2[x1][y1];
im2[x1][y1]=im2[x2][y2];
im2[x2][y2]=t;
}
//System.out.println("arry");
}
}