-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGui.java
More file actions
403 lines (278 loc) · 7.44 KB
/
Gui.java
File metadata and controls
403 lines (278 loc) · 7.44 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Stack;
import java.util.TimerTask;
import java.util.Timer;
import java.awt.event.*;
import java.lang.*;
import java.awt.event.KeyEvent.*;
import java.awt.event.KeyListener.*;
import java.awt.Graphics.*;
import java.util.Random;
/**
* The main Gui window for the system. displays the grid of tiles and will show how solutions were found
*
* @author Eqwen Cluley
* @version v1
*/
public class Gui
{
private JFrame window;
private JPanel gridPanel;
private JLabel[][] grid;
private Container pane;
private Timer timer;
int pz[][]=new int[4][4];
int z=0,k,l;
int N=4;
/**
* Builds a Gui window and initalises the puzzle. It also setsup a timer object to be used later when a solution has been found.
*/
public Gui()
{
timer = new Timer();
window = new JFrame();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pane = window.getContentPane();
draw();
}
/**
* Redraws the grid, based upon the current puzzle. By updating the puzzle, and calling this method you can change the display
*/
public void draw()
{
pane.removeAll();
gridPanel = null;
gridPanel = new JPanel();
grid = new JLabel[4][4];
pane.setLayout(new FlowLayout());
gridPanel.setLayout(new GridLayout(0,4));
if(z==0){MakeSquare();
z=1;
}
for(int i =0; i<4;i++){
for(int j = 0; j<4; j++){
int m=GetSquare(i,j);
grid[i][j] = new JLabel(""+m, 4);
grid[i][j].setFont(new Font("Sans-serif", Font.BOLD, 24));
grid[i][j].setBorder(BorderFactory.createLineBorder(Color.black));
if(m == 16){
grid[i][j].setText(" ");
}
gridPanel.add(grid[i][j]);
}
}
pane.add(gridPanel);
JPanel controlls = new JPanel();
controlls.setLayout(new GridLayout(2, 3));
JButton shuff = new JButton("SHUFFLE");
shuff.addActionListener(new SolveListener("shuff"));
JButton solveBF = new JButton("UP");
solveBF.addActionListener(new SolveListener("BF"));
JButton solve = new JButton("SOLVE");
solve.addActionListener(new SolveListener("solve"));
JButton left = new JButton("LEFT");
left.addActionListener(new SolveListener("left"));
JButton solveAS = new JButton("DOWN");
solveAS.addActionListener(new SolveListener("AS"));
JButton right = new JButton("RIGHT");
right.addActionListener(new SolveListener("right"));
controlls.add(shuff);
controlls.add(solveBF);
controlls.add(solve);
controlls.add(left);
controlls.add(solveAS);
controlls.add(right);
pane.add(controlls);
window.setSize(600,200);
window.setVisible(true);
}
private class SolveListener implements ActionListener
{
private String type;
/**
* Takes in a strign to define which type of search it will execute.
* @param type String "BF" for breadth first search, "DF" for depth first and any other for A*
*/
private SolveListener(String type)
{
this.type = type;
}
public void actionPerformed(ActionEvent e){
int i,j;
if(type == "DF"){
} else if(type == "BF"){
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(pz[i][j]==16)
{
k=i;
l=j;
break;}
}
if(k!=3){
pz[k][l]=pz[k+1][l];
pz[k+1][l]=16;
draw();}
}
else if(type == "left"){
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(pz[i][j]==16)
{
k=i;
l=j;
break;}
}
if(l!=3){
pz[k][l]=pz[k][l+1];
pz[k][l+1]=16;
draw();
}
}
else if(type == "right"){
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if(pz[i][j]==16)
{
k=i;
l=j;
break;}
}
if(l!=0){
pz[k][l]=pz[k][l-1];
pz[k][l-1]=16;
draw();
}
}
else if(type == "shuff"){
shuffle(0,0);
}
else {
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
if((pz[i][j]==16)||(pz[i][j]==0))
{k=i;
l=j;
break;
}
}
if(k!=0){
pz[k][l]=pz[k-1][l];
pz[k-1][l]=16;
draw();
}
}
}
}
void MakeSquare()
{
int k=1;
int o,p;
for(o=0;o<4;o++)
for(p=0;p<4;p++)
{
pz[o][p]=k;
k++;
}
}
int GetSquare(int o,int p)
{
return pz[o][p];
}
public static void main(String[] args)
{
new Gui();
}
void shuffle(int i,int j){
int m=0;
int c[]=RandomizeArray(1,16);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
pz[i][j]=c[m];
m++;
}
if(isSolvable(pz))
draw();
else
{
shuffle(0,0);}
}
int getInvCount(int arr[])
{
int inv_count = 0;
for (int i = 0; i < N*N - 1; i++)
for (int j = i+1; j < N*N; j++)
if (arr[i] > arr[j])
inv_count++;
return inv_count;
}
// find Position of blank from bottom
int findXPosition(int pz[][])
{int i,j;
// start from bottom-right corner of matrix
for (i = N - 1; i >= 0; i--)
for (j = N - 1; j >= 0; j--)
if (pz[i][j] == 0)
break;
return N-i;
}
// This function returns true if given
// instance of N*N - 1 puzzle is solvable
boolean isSolvable(int pz[][])
{
int c[]=new int[16];
int m=0;
for(int i=0;i<4;i++)
for(int j=0;j<4;j++)
{
c[m]=pz[i][j];
m++;
}
// Count inversions in given puzzle
int invCount = getInvCount(c);
// If grid is odd, return true if inversion
// count is even.
if (N%2!=0)
{
if(invCount%2==0)
return true;
else return false;
}
else // grid is even
{
int pos = findXPosition(pz);
if (pos%2!=0)
if(invCount%2==0)
{
return true;}
else return false;
else
if(invCount%2==0)
return false;
else {
return true;}
}
}
public static int[] RandomizeArray(int a, int b){
Random rgen = new Random(); // Random number generator
int size = b-a+1;
int[] array = new int[size];
for(int i=0; i< size; i++){
array[i] = a+i;
}
for (int i=0; i<array.length; i++) {
int randomPosition = rgen.nextInt(array.length);
int temp = array[i];
array[i] = array[randomPosition];
array[randomPosition] = temp;
}
return array;
}
}