-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoPanel.java
More file actions
256 lines (228 loc) · 7.34 KB
/
DemoPanel.java
File metadata and controls
256 lines (228 loc) · 7.34 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
import java.awt.Color;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Dimension;
import javax.swing.JPanel;
public class DemoPanel extends JPanel
{
// screen settings
final int maxCol=20;
final int maxRow=20;
final int nodeSize=70;
final int screenWidth=nodeSize*maxCol;
final int screenHeight=nodeSize*maxRow;
//Node
Node[][] node = new Node[maxCol][maxRow];
Node startNode, goalNode, currentNode;
ArrayList<Node> openList=new ArrayList<>();
ArrayList<Node> checkedList=new ArrayList<>();
//others
boolean goalReached=false;
int step=0;
public DemoPanel()
{
this.setPreferredSize(new Dimension(screenWidth,screenHeight));
this.setBackground(Color.black);
this.setLayout(new GridLayout(maxRow,maxCol));
this.addKeyListener(new KeyHandler(this));
this.setFocusable(true);
//place nodes
int col=0;
int row=0;
while(col<maxCol && row<maxRow)
{
node[col][row]=new Node(col,row);
this.add(node[col][row]);
col++;
if(col == maxCol)
{
col=0;
row++;
}
}
// set start and goal node
Random rand = new Random();
setStartNode(rand.nextInt(maxCol), rand.nextInt(maxRow));
setGoalNode(rand.nextInt(maxCol), rand.nextInt(maxRow));
// place solid nodes
int numberOfSolidNodes = 150; // Adjust as needed
for (int i = 0; i < numberOfSolidNodes; i++) {
col = rand.nextInt(maxCol);
row = rand.nextInt(maxRow);
if (node[col][row] != startNode && node[col][row] != goalNode) {
setSolidNode(col, row);
}
}
//set cost
setCostOnNodes();
}
private void setStartNode(int col, int row)
{
node[col][row].setAsStart();
startNode=node[col][row];
currentNode=startNode;
}
private void setGoalNode(int col, int row)
{
node[col][row].setAsGoal();
goalNode=node[col][row];
}
private void setSolidNode(int col, int row)
{
node[col][row].setAsSolid();
}
private void setCostOnNodes()
{
int col=0,row=0;
while (col<maxCol && row<maxRow)
{
getCost(node[col][row]);
col++;
if(col == maxCol)
{
col=0;
row++;
}
}
}
private void getCost(Node node)
{
//get G-cost (the distance from the start node)
int xDistance=Math.abs(node.col-startNode.col);
int yDistance=Math.abs(node.row-startNode.row);
node.gCost= xDistance+yDistance;
//get H-cost (the distance from the goal node)
xDistance=Math.abs(node.col-goalNode.col);
yDistance=Math.abs(node.row-goalNode.row);
node.hCost= xDistance+yDistance;
//get F-cost(the total cost)
node.fCost =node.gCost+node.hCost;
//display the cost on node
if(node!=startNode && node!=goalNode)
{
node.setText("<html>F:"+node.fCost+"<br>G:"+node.gCost+"<html>");
}
}
public void search()
{
if(goalReached==false)
{
int col=currentNode.col;
int row=currentNode.row;
currentNode.setAsChecked();
checkedList.add(currentNode);
openList.remove(currentNode);
//open the up node
if(row-1>=0)
openNode(node[col][row-1]);
//open the left node
if(col-1>=0)
openNode(node[col-1][row]);
//open the downn node
if(row+1<maxRow)
openNode(node[col][row+1]);
//open the left node
if(col+1<maxCol)
openNode(node[col+1][row]);
//find the best node
int bestNodeIndex=0;
int bestNodeCost=999;
for(int i=0;i<openList.size();i++)
{
//check if this node's F-cost is better
if(openList.get(i).fCost<bestNodeCost)
{
bestNodeIndex=i;
bestNodeCost=openList.get(i).fCost;
}
//if F-cost is equal,check the G-cost
else if(openList.get(i).fCost==bestNodeCost)
{
if(openList.get(i).gCost<openList.get(bestNodeIndex).gCost)
{
bestNodeIndex=i;
}
}
}
//After the loop,we get the best node which is our next step
currentNode=openList.get(bestNodeIndex);
if(currentNode==goalNode)
goalReached=true;
}
}
public void autoSearch()
{
while(goalReached==false && step<300)
{
int col=currentNode.col;
int row=currentNode.row;
currentNode.setAsChecked();
checkedList.add(currentNode);
openList.remove(currentNode);
//open the up node
if(row-1>=0)
openNode(node[col][row-1]);
//open the left node
if(col-1>=0)
openNode(node[col-1][row]);
//open the downn node
if(row+1<maxRow)
openNode(node[col][row+1]);
//open the left node
if(col+1<maxCol)
openNode(node[col+1][row]);
//find the best node
int bestNodeIndex=0;
int bestNodeCost=999;
for(int i=0;i<openList.size();i++)
{
//check if this node's F-cost is better
if(openList.get(i).fCost<bestNodeCost)
{
bestNodeIndex=i;
bestNodeCost=openList.get(i).fCost;
}
//if F-cost is equal,check the G-cost
else if(openList.get(i).fCost==bestNodeCost)
{
if(openList.get(i).gCost<openList.get(bestNodeIndex).gCost)
{
bestNodeIndex=i;
}
}
}
//After the loop,we get the best node which is our next step
currentNode=openList.get(bestNodeIndex);
if(currentNode==goalNode)
{
goalReached=true;
trackThePath();
}
}
step++;
}
private void openNode(Node node)
{
if(node.open==false && node.checked==false && node.solid==false)
{
//if the node is not opened yet, add it to the open list
node.setAsOpen();
node.parent=currentNode;
openList.add(node);
}
}
private void trackThePath()
{
//Backtrack and draw the path
Node current=goalNode;
while(current!=startNode)
{
current=current.parent;
if(current!=startNode)
{
current.setAsPath();
}
}
}
}