This repository has been archived by the owner on Jan 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.java
320 lines (278 loc) · 9.11 KB
/
Game.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
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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.font.ImageGraphicAttribute;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/*
# A-Pathfinding
A* pahfinding
************************************
Author: JXstudio / JXinBOX / Jack Xu
************************************
*
Block Color Indicators:
Red : Starting Point
Blue : Ending Point
Black : Walls (Obstacles)
Orange: Final Shortest Path
Green : Closed List
Blue : Open List
Controller:
1. [Left Click/Drag] : Create Walls (black obstacles)
2. [Right Click/Drag] : Destroy Walls
3. [E]+[One Left Click] : Create Ending Point
4. [S]+[One Left Click] : Create Start Point
P.S.: Starting point can only be created after the existence of Ending point.
5. [A] : Showing The Path Finding Process with Animation Automatically
6. [Space] : Showing The Path Finding Process with Animation Manually, One click and one step.
7. [F] : Showing The Shortest Path and results, and time duration of calculation.
8. [E] : Empty all the path and starting points. (All blocks except walls and the ending block)
9. [L] : Clear all
10. [G] : Show/Hide Grids
11. [Scroll D/U] : Zoom In / Out
12. [Shift]+[Mouse Drag]: Move around the Canvas
*/
public class Game extends Canvas implements Runnable {
public static int horCells = 20, verCells = 20, AvailableX = 0, AvailableY = 0, MaxScale = 50, MinScale = 2;
public static int width = 1000;
public static int MainWidth = 800;
public static int height = MainWidth / 4 * 4;
public static int scale = 1;
public static int Quan_frames = 0;
public static int Quan_updates = 0;
public static double ticks = 0;
public static final String TITLE = "Game";
private Thread thread;// handle game
private JFrame frame;
private Screen screen;
private boolean running = false;
public static InputHandler input;
private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
public static int cellW, cellH;
public static Block Start, End;
public static List wall, OpenList = new List(), ClosedList = new List(), ShortPath = new List();
public static int CostS = 10, CostD = 14;
public Game() {
Dimension size = new Dimension(width * scale, height * scale);
setPreferredSize(size);
screen = new Screen(width, height, MainWidth);
frame = new JFrame();
input = new InputHandler();
addKeyListener(input);
addFocusListener(input);
addMouseListener(input);
addMouseMotionListener(input);
addMouseWheelListener(input);
cellW = MainWidth / horCells;
cellH = height / verCells;
wall = new List();
}
public synchronized void start() {
running = true;
thread = new Thread(this, "A*");
// handle this game, attached to the Game
thread.start();
}
public synchronized void stop() {
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Game game = new Game();
game.frame.setResizable(false);
game.frame.setTitle(TITLE + " | " + Quan_updates + " UPS | " + Quan_frames + " FPS");
game.frame.add(game);// add canvas component to the frame window
game.frame.pack();// size up the frame to this game component
game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
game.frame.setLocationRelativeTo(null);
game.frame.setVisible(true);
game.start();
}
public static long timerf = 0;
boolean FastS=false;
@Override
public void run() {
long lastTime = System.nanoTime();
long timer = System.currentTimeMillis();
long timertick = System.currentTimeMillis();
final double ns = 1000000000.0 / 60.0;
double delta = 0;
Quan_frames = 0;
Quan_updates = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
if (Fastmode) {
timerf = System.currentTimeMillis();
FastS=true;
}
while (delta >= 1) {
update();// hold logic
Quan_updates++;
ticks++;
delta--;
}
if(FastS){
timerf = System.currentTimeMillis() - timer;
FastS=false;
}
render();// render
Quan_frames++;
if (System.currentTimeMillis() - timer > 1000) {
timer += 1000;
frame.setTitle(TITLE + " | " + Quan_updates + " UPS | " + Quan_frames + " FPS");
Quan_updates = 0;
Quan_frames = 0;
}
}
stop();
}
public static int iteration = 0;
static boolean finishPathFinding = false;
static boolean reach = false, Fastmode = false;
public void update() {
if (Fastmode) {
while (!reach) {
OpenList.sortF();
if (OpenList.size() == 0) {
System.out.println("Impossible END");
reach = true;
} else {
Expand((Block) OpenList.get(0));
}
}
Fastmode = false;
} else if ((input.run || input.AutoRun) && Game.Start != null && Game.End != null && !reach) {
if (ticks % 1 == 0) {
OpenList.sortF();
if (OpenList.size() == 0) {
System.out.println("Impossible END");
} else {
Expand((Block) OpenList.get(0));
}
// System.out.println(ClosedList.toString());
iteration++;
input.run = false;
}
}
if (reach && !finishPathFinding) {
BackTracking();
finishPathFinding = true;
System.out.println(ShortPath.toString());
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);// triple buffering
return;
}
screen.clear();
screen.render();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, (int) (getWidth() * 1), (int) (getHeight() * 1), null);
// Every time drawing, so we have to clean up the graphics
g.setFont(new Font("TimesRoman", Font.BOLD, FontHeight));
// for (int i = 0; i < wall.size(); i++) {
// g.drawString("wall", ((Block) wall.get(i)).getX()*cellH, ((Block)
// wall.get(i)).getY()*cellH);
// }
if (cellW >= 40) {
Block temp;
for (int i = 0; i < OpenList.size(); i++) {
temp = ((Block) OpenList.get(i));
if (!((temp.getX() - AvailableX) >= horCells || (temp.getY() - AvailableY) > verCells
|| (temp.getX() - AvailableX) < 0 || (temp.getY() - AvailableY) < 0)) {
g.drawString(temp.toString(2), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 2);
g.drawString(temp.toString(1), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 10);
g.drawString(temp.toString(0), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 30);
}
}
for (int i = 0; i < ClosedList.size(); i++) {
temp = ((Block) ClosedList.get(i));
if (!((temp.getX() - AvailableX) >= horCells || (temp.getY() - AvailableY) > verCells
|| (temp.getX() - AvailableX) < 0 || (temp.getY() - AvailableY) < 0)) {
g.drawString(temp.toString(2), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 2);
g.drawString(temp.toString(1), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 10);
g.drawString(temp.toString(0), (temp.getX() - AvailableX) * cellH + 3,
(temp.getY() - AvailableY) * cellH + cellH - 30);
}
}
}
g.setFont(new Font("TimesRoman", Font.BOLD, 14));
g.drawString("Iteration: " + iteration, MainWidth + 30, 150);
g.drawString("Screen Coor: [" + AvailableX + "," + AvailableY + "]", MainWidth + 30, 200);
g.drawString("Checked box #: " + (ClosedList.size() + OpenList.size()), MainWidth + 30, 250);
g.drawString("Steps Required#: " + (ShortPath.size() - 1), MainWidth + 30, 300);
g.drawString("Algorithm Timer#: " + timerf/1000+"."+timerf%1000, MainWidth + 30, 350);
g.dispose();
bs.show();
}
public void Expand(Block current) {
if (current.getD() == 0) {
reach = true;
ClosedList.add(current);
} else {
Block temp = null;
ClosedList.add(current);
for (int i = -1; i <= 1; i++) {
for (int i1 = -1; i1 <= 1; i1++) {
int c = CostD;
if (i * i1 == 0) {
c = CostS;
}
temp = new Block(new Point(current.getCoor().x - i, current.getCoor().y - i1), c + current.getC());
temp.setParent(current);
OpenList.buildPath(temp);
}
}
OpenList.remove(current);
}
}
private void BackTracking() {
Block current = (Block) ClosedList.get(ClosedList.search(End));
ShortPath.add(current);
do {
current = current.getParent();
ShortPath.add(current);
} while (!current.getCoor().equals(Start.getCoor()));
}
static int FontHeight = 10;
public static void Scale(double scale) {
System.out.println(scale);
cellW = (int) (scale);
cellH = (int) (scale);
horCells = MainWidth / cellW;
verCells = height / cellH;
System.out.println(horCells + "|" + cellW);
}
}