@@ -24,6 +24,7 @@ The robot character (an ant) should find its way from the start point (home) to
24
24
import java .awt .BorderLayout ;
25
25
import java .awt .Color ;
26
26
import java .awt .Dimension ;
27
+ import java .awt .Font ;
27
28
import java .awt .Graphics ;
28
29
import java .awt .GridLayout ;
29
30
import java .awt .Image ;
@@ -42,7 +43,7 @@ The robot character (an ant) should find its way from the start point (home) to
42
43
43
44
public class Game extends JPanel implements MouseListener , MouseMotionListener {
44
45
45
- private final int TILE_SIZE = 40 ;
46
+ private final static int TILE_SIZE = 40 ;
46
47
private final int NUM_ROWS = 16 ;
47
48
private final int NUM_COLS = 16 ;
48
49
@@ -69,15 +70,12 @@ public class Game extends JPanel implements MouseListener, MouseMotionListener {
69
70
70
71
private int timerX ;
71
72
private int timerY ;
72
- private long startTimeBeforeSearch ;
73
73
74
+ private long startTimeBeforeSearch ;
74
75
private long elapsedTimeAfterSearch ;
75
-
76
76
private String elapsedTimeStringBeforeSearch ;
77
77
78
-
79
78
private long startTimeBeforeAnimation ;
80
-
81
79
private long elapsedTimeAfterAnimation ;
82
80
private String elapsedTimeStringAfterAnimation ;
83
81
private boolean antReached ;
@@ -93,8 +91,8 @@ public Game(JFrame frame) {
93
91
buttonPanel .setLayout (new GridLayout (1 , buttonCount , 10 , 10 ));
94
92
95
93
// set tile offset
96
- int xOffset = (int ) ((frame .getWidth () - TILE_SIZE * NUM_COLS ) / 2 )/ TILE_SIZE ;
97
- int yOffset = (int ) ((frame .getHeight () - TILE_SIZE * NUM_ROWS ) / 2 )/ TILE_SIZE ;
94
+ int xOffset = (int ) ((frame .getWidth () - TILE_SIZE * NUM_COLS ) / 2 );
95
+ int yOffset = (int ) ((frame .getHeight () - TILE_SIZE * NUM_ROWS ) / 2 );
98
96
99
97
Tile .setxOffset (xOffset );
100
98
Tile .setyOffset (yOffset );
@@ -132,8 +130,8 @@ public Game(JFrame frame) {
132
130
tobeDrawn = new LinkedList <Tile >();
133
131
134
132
// set timer
135
- elapsedTimeStringBeforeSearch = "00 :00:000" ;
136
- elapsedTimeStringAfterAnimation = "00 :00:000" ;
133
+ elapsedTimeStringBeforeSearch = "0 :00:000" ;
134
+ elapsedTimeStringAfterAnimation = "0 :00:000" ;
137
135
timerX = 100 ;
138
136
timerY = frame .getHeight () - 100 ;
139
137
startTimeBeforeSearch = System .currentTimeMillis ();
@@ -154,7 +152,14 @@ public void actionPerformed(ActionEvent e) {
154
152
noPath = false ;
155
153
startMovingAnt = false ;
156
154
tobeDrawn = new LinkedList <Tile >();
155
+ startTimeBeforeSearch = 0 ;
156
+ elapsedTimeAfterSearch = 0 ;
157
+ elapsedTimeStringBeforeSearch = "0:00:000" ;
157
158
159
+ startTimeBeforeAnimation = 0 ;
160
+ elapsedTimeAfterAnimation = 0 ;
161
+ elapsedTimeStringAfterAnimation = "0:00:000" ;
162
+ antReached = false ;
158
163
createTiles ();
159
164
repaint ();
160
165
}
@@ -173,7 +178,7 @@ private void loadFoodImg() {
173
178
// the selection mode of available buttons
174
179
private void selectOpenTerrain () {
175
180
// Create the reset button
176
- JButton result = new JButton ("<html>Open Terrain<br/><center><font size='2'>Cost: 0 </font></center></html>" );
181
+ JButton result = new JButton ("<html>Open Terrain<br/><center><font size='2'>Cost: 1 </font></center></html>" );
177
182
result .addActionListener (new ActionListener () {
178
183
@ Override
179
184
public void actionPerformed (ActionEvent e ) {
@@ -243,6 +248,7 @@ public void actionPerformed(ActionEvent e) {
243
248
if (startTile != null && goalTile != null ) {
244
249
ant = new Ant (startTile , goalTile , TILE_SIZE , tiles );
245
250
startClicked = true ;
251
+ startTimeBeforeAnimation = System .currentTimeMillis ();
246
252
ant .search ();
247
253
delayPaint ();
248
254
}
@@ -305,12 +311,8 @@ public void actionPerformed(ActionEvent e) {
305
311
private void handleGoalLocationSelection (Tile clickedTile ) {
306
312
goalLocationSelectionMode = false ;
307
313
308
- for (int i = 0 ; i < NUM_ROWS ; i ++) {
309
- for (int j = 0 ; j < NUM_COLS ; j ++) {
310
- if (tiles [i ][j ].isGoal ()) {
311
- tiles [i ][j ].resetGoal ();
312
- }
313
- }
314
+ if (goalTile != null ){
315
+ goalTile .resetGoal ();
314
316
}
315
317
// Set the clicked tile as the start location
316
318
clickedTile .setGoal ();
@@ -321,12 +323,8 @@ private void handleGoalLocationSelection(Tile clickedTile) {
321
323
private void handleStartLocationSelection (Tile clickedTile ) {
322
324
startLocationSelectionMode = false ;
323
325
324
- for (int i = 0 ; i < NUM_ROWS ; i ++) {
325
- for (int j = 0 ; j < NUM_COLS ; j ++) {
326
- if (tiles [i ][j ].isStart ()) {
327
- tiles [i ][j ].resetStart ();
328
- }
329
- }
326
+ if (startTile != null ){
327
+ startTile .resetStart ();
330
328
}
331
329
// Set the clicked tile as the start location
332
330
clickedTile .setStart ();
@@ -337,29 +335,26 @@ private void handleStartLocationSelection(Tile clickedTile) {
337
335
private void changeTileState (MouseEvent e ) {
338
336
int row = e .getX ();
339
337
int col = e .getY ();
340
-
341
- if (row >= 0 && row < NUM_ROWS * TILE_SIZE && col >= 0 && col < NUM_COLS * TILE_SIZE ) {
342
-
343
- Tile clickedTile = fetchTile (row , col );
344
-
345
- if (clickedTile != null && clickedTile != lastDraggedTile ) {
346
- lastDraggedTile = clickedTile ;
347
-
348
- if (startLocationSelectionMode ) {
349
- handleStartLocationSelection (clickedTile );
350
- } else if (goalLocationSelectionMode ) {
351
- handleGoalLocationSelection (clickedTile );
352
- } else if (obstacleSelectionMode ) {
353
- clickedTile .setObstacle ();
354
- } else if (openTerrainSelectionMode ) {
355
- clickedTile .setOpenTerrain ();
356
- } else if (swamplandSelectionMode ) {
357
- clickedTile .setSwampland ();
358
- } else if (grasslandSelectionMode ) {
359
- clickedTile .setGrassland ();
360
- }
361
- repaint ();
338
+
339
+ Tile clickedTile = fetchTile (row , col );
340
+
341
+ if (clickedTile != null && clickedTile != lastDraggedTile ) {
342
+ lastDraggedTile = clickedTile ;
343
+
344
+ if (startLocationSelectionMode ) {
345
+ handleStartLocationSelection (clickedTile );
346
+ } else if (goalLocationSelectionMode ) {
347
+ handleGoalLocationSelection (clickedTile );
348
+ } else if (obstacleSelectionMode ) {
349
+ clickedTile .setObstacle ();
350
+ } else if (openTerrainSelectionMode ) {
351
+ clickedTile .setOpenTerrain ();
352
+ } else if (swamplandSelectionMode ) {
353
+ clickedTile .setSwampland ();
354
+ } else if (grasslandSelectionMode ) {
355
+ clickedTile .setGrassland ();
362
356
}
357
+ repaint ();
363
358
}
364
359
}
365
360
@@ -392,7 +387,6 @@ public void mouseExited(MouseEvent e) {}
392
387
393
388
private void createTiles () {
394
389
tiles = new Tile [NUM_ROWS ][NUM_COLS ];
395
-
396
390
397
391
for (int i = 0 ; i < NUM_ROWS ; i ++) {
398
392
for (int j = 0 ; j < NUM_COLS ; j ++) {
@@ -403,11 +397,12 @@ private void createTiles() {
403
397
}
404
398
405
399
private Tile fetchTile (int mouseX , int mouseY ) {
400
+
406
401
Tile result = null ;
407
402
for (int i = 0 ; i < NUM_ROWS ; i ++) {
408
403
for (int j = 0 ; j < NUM_COLS ; j ++) {
409
404
Tile tile = tiles [i ][j ];
410
- if (tile .getTile (mouseX , mouseY , TILE_SIZE )) {
405
+ if (tile .isWithinTile (mouseX , mouseY )) {
411
406
result = tile ;
412
407
}
413
408
}
@@ -432,7 +427,7 @@ public void actionPerformed(ActionEvent e) {
432
427
startMovingAnt = true ;
433
428
tobeDrawn = ant .getPath ();
434
429
((Timer ) e .getSource ()).stop ();
435
- startTimeBeforeAnimation = System . currentTimeMillis ();
430
+
436
431
animateAnt ();
437
432
} else {
438
433
@@ -492,9 +487,14 @@ public void animateAnt() {
492
487
noPath = true ;
493
488
494
489
} else {
490
+ // first remove the start tile static image
491
+ path .get (path .size () - 1 ).removeAntImage (true );
492
+
495
493
// set the ant to the start location
496
- ant .setX (path .get (path .size () - 1 ).getX () * TILE_SIZE );
497
- ant .setY (path .get (path .size () - 1 ).getY () * TILE_SIZE );
494
+ ant .setX (path .get (path .size () - 1 ).getXpixel ());
495
+ ant .setY (path .get (path .size () - 1 ).getYpixel ());
496
+
497
+
498
498
499
499
Timer timer = new Timer (delay , new ActionListener () {
500
500
int i ;
@@ -508,8 +508,8 @@ public void actionPerformed(ActionEvent e) {
508
508
509
509
Tile nextTile = path .get (i );
510
510
511
- double dx = nextTile .getX () * TILE_SIZE - ant .getX ();
512
- double dy = nextTile .getY () * TILE_SIZE - ant .getY ();
511
+ double dx = nextTile .getXpixel () - ant .getX ();
512
+ double dy = nextTile .getYpixel () - ant .getY ();
513
513
514
514
// angle from current tile to next tile
515
515
double angle = Math .atan2 (dy , dx );
@@ -521,6 +521,11 @@ public void actionPerformed(ActionEvent e) {
521
521
// if the ant is close to the next tile, remove the tile from the path
522
522
if (distance < speed ) {
523
523
path .remove (i );
524
+
525
+ // if the ant reached the goal, stop the timer
526
+ if (path .isEmpty ()) {
527
+ antReached = true ;
528
+ }
524
529
} else {
525
530
// move the ant
526
531
ant .setX ((int ) newAntX );
@@ -548,22 +553,20 @@ protected void paintComponent(Graphics g) {
548
553
// draw tiles
549
554
for (int i = 0 ; i < NUM_ROWS ; i ++) {
550
555
for (int j = 0 ; j < NUM_COLS ; j ++) {
551
- tiles [i ][j ].draw (g , TILE_SIZE );
556
+ tiles [i ][j ].draw (g );
552
557
}
553
558
}
554
559
555
560
// draw food
556
561
if (goalTile != null && goalTile .isGoal ()) {
557
- g .drawImage (foodImg , goalTile .getX () * TILE_SIZE , goalTile .getY () * TILE_SIZE , TILE_SIZE , TILE_SIZE , null );
562
+ g .drawImage (foodImg , goalTile .getXpixel () , goalTile .getYpixel () , TILE_SIZE , TILE_SIZE , null );
558
563
}
559
564
560
565
// draw ant
561
566
if (ant != null ) {
562
567
ant .draw (g , TILE_SIZE );
563
568
}
564
569
565
-
566
-
567
570
// draw path
568
571
drawPath (g , TILE_SIZE , tobeDrawn );
569
572
@@ -575,10 +578,8 @@ protected void paintComponent(Graphics g) {
575
578
}
576
579
577
580
// Draw the elapsed time
578
-
579
-
580
-
581
581
g .setColor (Color .RED ); // Sets the color to red.
582
+ g .setFont (new Font ("Courier New" , Font .PLAIN , 20 ));
582
583
g .drawString ("Mage Create: " +elapsedTimeStringBeforeSearch , timerX , timerY );
583
584
g .drawString ("Solved Time: " +elapsedTimeStringAfterAnimation , timerX , timerY + 20 );
584
585
@@ -639,19 +640,19 @@ public void drawPath(Graphics g, int tileSize, LinkedList<Tile> LinkedList) {
639
640
Tile current = LinkedList .get (i );
640
641
641
642
Tile next = LinkedList .get (i + 1 );
642
-
643
- int x1 = (int ) current .getX () * tileSize + tileSize / 2 ;
644
- int y1 = (int ) current .getY () * tileSize + tileSize / 2 ;
645
- int x2 = (int ) next .getX () * tileSize + tileSize / 2 ;
646
- int y2 = (int ) next .getY () * tileSize + tileSize / 2 ;
643
+ System . out . println ( "current: " + current . getXpixel () + ", " + current . getYpixel ());
644
+ int x1 = (int ) current .getXpixel () + tileSize / 2 ;
645
+ int y1 = (int ) current .getYpixel () + tileSize / 2 ;
646
+ int x2 = (int ) next .getXpixel () + tileSize / 2 ;
647
+ int y2 = (int ) next .getYpixel () + tileSize / 2 ;
647
648
648
649
g .drawLine (x1 , y1 , x2 , y2 );
649
650
// repaint();
650
651
}
651
652
}
652
653
}
653
654
654
- public int getTileSize () {
655
+ public static int getTileSize () {
655
656
return TILE_SIZE ;
656
657
}
657
658
0 commit comments