-
Notifications
You must be signed in to change notification settings - Fork 0
/
NumberTile.java
152 lines (139 loc) · 3.45 KB
/
NumberTile.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
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Color;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class NumberTile extends JFrame{
JPanel top = null;
JPanel center = null;
JPanel bottom = null;
JButton[][] button = null;
JButton reset = null;
ImageIcon img = null;
List<List<Integer>> arr = new ArrayList<List<Integer>>();
JLabel win = null;
JLabel countStep = null;
int totalStep;
public NumberTile() {
setSize(400,400);
setTitle("NUMBER TILE'S");
//TOP
top = new JPanel();
add(top, BorderLayout.NORTH);
win = new JLabel();
win.setText("GAME IS ON");
top.add(win);
reset = new JButton("Reset");
top.add(reset);
//CENTER
center = new JPanel();
add(center, BorderLayout.CENTER);
center.setSize(300,300);
center.setLayout(new GridLayout(3,3));
button = new JButton[3][3];
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++){
button[i][j] = new JButton();
center.add(button[i][j]);
}
}
//BOTTOM
bottom = new JPanel();
add(bottom, BorderLayout.SOUTH);
countStep = new JLabel();
countStep.setText("No. of Step : 0");
bottom.add(countStep);
totalStep = 0;
initilizeGame(0);
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++){
final int finalRow = i;
final int finalCol = j;
button[i][j].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
change(finalRow, finalCol);
}
});
}
}
//Reset function
reset.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
initilizeGame(1);
}
});
setVisible(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
}
public void initilizeGame(int check){
countStep.setText("No. of Step : 0");
top.setBackground(null);
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
button[i][j].setEnabled(true);
}
}
List<Integer> temp = new ArrayList<>(Arrays.asList(0,1,2,3,4,5,6,7,8));
Collections.shuffle(temp);
int tempCount = 0;
for(int x=0; x<9; x=x+3){
if(check == 0)
arr.add(new ArrayList<Integer>(Arrays.asList(temp.get(x),temp.get(x+1),temp.get(x+2))));
else{
arr.set(tempCount, new ArrayList<Integer>(Arrays.asList(temp.get(x),temp.get(x+1),temp.get(x+2))));
tempCount++;
}
}
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++){
if(arr.get(i).get(j) != 0){
img = new ImageIcon("image\\" + arr.get(i).get(j) + ".png");
button[i][j].setIcon(img);
} else{
button[i][j].setIcon(null);
}
}
}
}
public void change(int i, int j) {
int a = i;
int b = j;
if(j>0 && arr.get(i).get(j-1) == 0)
b = j-1;
else if(j<2 && arr.get(i).get(j+1) == 0)
b = j+1;
else if(i>0 && arr.get(i-1).get(j) == 0)
a = i-1;
else if(i<2 && arr.get(i+1).get(j) == 0)
a = i+1;
if(!(i == a && j == b)){
button[a][b].setIcon(button[i][j].getIcon());
button[i][j].setIcon(null);
arr.get(a).set(b, arr.get(i).get(j));
arr.get(i).set(j, 0);
totalStep++;
countStep.setText("No. of Step : " + totalStep);
}
final_check();
}
public void final_check(){
int count = 1;
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if((count%9) == arr.get(i).get(j))
count++;
else
return;
if(count == 10) {
win.setText("YOU WIN");
top.setBackground(Color.GREEN);
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
button[i][j].setEnabled(false);
}
}
public static void main(String []a) throws Exception{
new NumberTile();
}
}