-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrid.java
279 lines (264 loc) · 10 KB
/
Grid.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
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
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
public class Grid implements ActionListener{
protected int a;
protected int b;
public int k=0,x,w,y,z,row,xs=0,ys=0,col,ks=1,kd=1,aa,destval=0,xd,yd,makepath=0;
JFrame frame = new JFrame();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
JButton source=new JButton("Source");// Starting point of rat
JButton dest=new JButton("Destination");
JButton obs=new JButton("Obstacles"); // Rats cant go on that block
JButton path=new JButton("BuildPath"); // The path covered by rat having smallest path
JButton run=new JButton("Run"); // It will show the path covered with yellow colour
JButton reset=new JButton("Reset");
JButton ran = new JButton("Random"); // It will allow you to push random number of obstacles at once
JButton[][] button = new JButton[100][100];
int[][] Arr = new int[100][100];
Color color = UIManager.getColor ( "Panel.background" );
Grid(int a, int b){
this.a=a;this.b=b;
row=a;col=b;
frame.setTitle("MicroMouse");
frame.setVisible(true);
frame.setBounds(500, 200, 125, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
p1.setLayout(new GridLayout(a,b));
p2.setBackground(Color.GRAY);
p1.setBackground(Color.BLACK);
frame.add(p1);
//bottom
source.addActionListener(this);
dest.addActionListener(this);
obs.addActionListener(this);
path.addActionListener(this);
run.addActionListener(this);
reset.addActionListener(this);
ran.addActionListener(this);
frame.add(p2, BorderLayout.SOUTH);
p2.add(source);p2.add(dest);p2.add(obs);p2.add(path);p2.add(run);p2.add(reset);p2.add(ran);
//JButton[][] button = new JButton[a][b];
for(int i=0;i<a;++i){
for(int j=0;j<b;++j){
button[i][j]=new JButton();
button[i][j].setText("-");
//button[i][j].setActionCommand("-");
button[i][j].addActionListener(this);
p1.add(button[i][j]);
}
}
frame.pack();
frame.add(p1);
frame.setVisible(true);
}
public static void main(int a, int b) {
// TODO Auto-generated method stub
new Grid(a,b);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s=e.getActionCommand();
if(s.equalsIgnoreCase("Source")){
k=1;
}
if(k==1 && s.equalsIgnoreCase("-")){
for(int i=0;i<a;i++){
for(int j=0;j<b;j++){
if(button[i][j].getText()=="0"){
button[i][j].setText("-");
Color color = UIManager.getColor ( "Panel.background" );
button[i][j].setBackground(color);
}
if(button[i][j].isFocusOwner()){
button[i][j].setText("0");
button[i][j].setBackground(Color.GREEN); //k=0
xs=i; ys=j;
Arr[i][j]=0;
break;
}
}//if(k==0) break;
}
}
if(s.equalsIgnoreCase("Destination")){
k=2;
}
if(k==2 && s.equalsIgnoreCase("-")){
for(int i=0;i<a;++i){
for(int j=0;j<b;++j){
if(button[i][j].getText()=="d"){
button[i][j].setText("-");
button[i][j].setBackground(color);
}
if(button[i][j].isFocusOwner()){
button[i][j].setText("d");
button[i][j].setBackground(Color.BLUE);//k=0;
xd=i;yd=j; break;
}
}
//if(k==0) break;
}
}
if(s.equalsIgnoreCase("Obstacles")){
k=3;
}
if(k==3 && s.equalsIgnoreCase("-")){
for(int i=0;i<a;++i){//System.out.println("obs3");
for(int j=0;j<b;++j){//System.out.println("obs2");
if(button[i][j].isFocusOwner()){
button[i][j].setText("x");
button[i][j].setBackground(Color.BLACK);
w=i;x=j;
Arr[i][j]=-1;
break;
}
}
}
}else if(k==3 && s.equalsIgnoreCase("x")){
for(int i=0;i<a;++i){
for(int j=0;j<b;++j){
if(button[i][j].isFocusOwner()){
button[i][j].setText("-");
button[i][j].setBackground(color);
w=i;x=j;
Arr[i][j]=0;
break;
}
}
}
}
// for path
if(s.equalsIgnoreCase("BuildPath")){
k=4;
int i,j;
Queue<Integer> q1=new LinkedList<Integer>();
Queue<Integer> q2=new LinkedList<Integer>();
q1.add(xs); q2.add(ys);
while(!q1.isEmpty() && !q2.isEmpty()){
i=q1.poll();
j=q2.poll();
if(((i+1)<row)){
if(Arr[i+1][j]==0 && !((i+1)==xs && j==ys)){
Arr[i+1][j]=Arr[i][j]+1; //System.out.print(" hye");
q1.add(i+1);q2.add(j);
}
}
if((i-1)>=0){
if(Arr[i-1][j]==0 && !((i-1)==xs && j==ys)){//System.out.print(" hye11");
Arr[i-1][j]=Arr[i][j]+1; //System.out.print(Arr[i-1][j]+" hye2");
q1.add(i-1);q2.add(j);
}
}
if((j+1)<col){
if(Arr[i][j+1]==0 && !((i)==xs && (j+1)==ys)){/
Arr[i][j+1]=Arr[i][j]+1;
q1.add(i);q2.add(j+1);
}
}
if((j-1)>=0){
if( Arr[i][j-1]==0 && !((i)==xs && (j-1)==ys)){//System.out.print(" hy3e");
Arr[i][j-1]=Arr[i][j]+1;// System.out.print(Arr[i][j-1]+"hye4 ");
q1.add(i);q2.add(j-1);
}
}
}
/*for(int m=0;m<row;++m){
for(int n=0;n<col;++n){
System.out.print(Arr[m][n]+" ");
}System.out.println("");
}*/
for(int i1=0;i1<row;++i1){
for(int j1=0;j1<col;++j1){
if(button[i1][j1].getText()!="x" && button[i1][j1].getText()!="s"){
button[i1][j1].setText(Integer.toString(Arr[i1][j1]));
}
}
}
if(Arr[xd][yd]==0){
makepath=1;
}
}
if(s.equalsIgnoreCase("run")){
k=5;
int i=xd; int j=yd;
if(Arr[xd][yd]==0){
JOptionPane.showMessageDialog(null, "oops! No path found");
}else{
int count=Integer.parseInt(button[i][j].getText());
while(count!=0){count--;
int ss;
if(((i+1)<row && button[i+1][j].getText()!="x")){
ss=Integer.parseInt(button[i+1][j].getText());
if(count==ss ){
button[i+1][j].setBackground(Color.YELLOW);
i++;
continue;
}
}
if((i-1)>=0&& button[i-1][j].getText()!="x"){
ss=Integer.parseInt(button[i-1][j].getText());
if(count==ss ){
button[i-1][j].setBackground(Color.YELLOW);
i--;
continue;
}
}
if((j+1)<col&& button[i][j+1].getText()!="x"){
ss=Integer.parseInt(button[i][j+1].getText());
if(count==ss ){
button[i][j+1].setBackground(Color.YELLOW);
j++;
continue;
}
}
if((j-1)>=0&& button[i][j-1].getText()!="x"){
ss=Integer.parseInt(button[i][j-1].getText());
if(count==ss ){
button[i][j-1].setBackground(Color.YELLOW);
j--;
continue;
}
}
}button[xs][ys].setBackground(Color.GREEN);
}
}
if(s.equalsIgnoreCase("Reset")){
for(int i=0;i<a;++i){
for(int j=0;j<b;++j){
button[i][j].setText("-");
button[i][j].setBackground(color);
Arr[i][j]=0;
}
}
k=0;
}
if(s.equalsIgnoreCase("Random")){
k=7;
}
if(k==7){
int loop =((int)(Math.random()*1000)%((row*col)/5));
//System.out.println(loop);
while(loop>0){
loop--;
int ll =((int)(Math.random()*1000))%row;
int rr = ((int)(Math.random()*1000))%col;
if(!((ll==xs && rr==ys)||(ll==xd && rr==yd))){
button[ll][rr].setText("x");
button[ll][rr].setBackground(Color.BLACK);
Arr[ll][rr]=-1;
}
}
}
}
}