@@ -499,7 +499,7 @@ public void actionPerformed(ActionEvent e) {
499499 // animate the ant from start to goal
500500 public void animateAnt () {
501501 if (startMovingAnt ) {
502- double speed = 3.0 ;
502+
503503 int delay = 10 ; // 0.1 second delay
504504 LinkedList <Tile > path = ant .getPath ();
505505
@@ -519,6 +519,7 @@ public void animateAnt() {
519519
520520 Timer timer = new Timer (delay , new ActionListener () {
521521 int i ;
522+ double speed ;
522523
523524 @ Override
524525 public void actionPerformed (ActionEvent e ) {
@@ -529,6 +530,9 @@ public void actionPerformed(ActionEvent e) {
529530
530531 Tile nextTile = path .get (i );
531532
533+ speed = setSpeed (nextTile );
534+
535+
532536 double dx = nextTile .getXpixel () - ant .getX ();
533537 double dy = nextTile .getYpixel () - ant .getY ();
534538
@@ -541,6 +545,8 @@ public void actionPerformed(ActionEvent e) {
541545
542546 // if the ant is close to the next tile, remove the tile from the path
543547 if (distance < speed ) {
548+ ant .setX (nextTile .getXpixel ());
549+ ant .setY (nextTile .getYpixel ());
544550 path .remove (i );
545551
546552 // if the ant reached the goal, stop the timer
@@ -561,20 +567,30 @@ public void actionPerformed(ActionEvent e) {
561567 }
562568
563569 }
570+
571+ private double setSpeed (Tile nextTile ) {
572+ double output = Tile .COST_SWAMPLAND ; // highest cost 4 for open terrain
573+
574+ if (nextTile .isSwampland ()){
575+ output = (output /Tile .COST_SWAMPLAND ) + 0.25 ; // 1.25
576+ } else if (nextTile .isGrassland ()){
577+ output = output *2 /Tile .COST_GRASSLAND + 0.5 ; // 3.16
578+ }
579+
580+ return output ;
581+ }
564582 });
565583 timer .start ();
566584 }
567585 }
568586 }
569-
587+
570588 @ Override
571589 protected void paintComponent (Graphics g ) {
572590 super .paintComponent (g );
573591
574592 printBoardAndCount (g );
575593
576-
577-
578594 // draw ant
579595 if (ant != null ) {
580596 ant .draw (g , TILE_SIZE );
@@ -590,7 +606,6 @@ protected void paintComponent(Graphics g) {
590606 g .drawString ("No Path Found" , 300 , 300 );
591607 }
592608
593-
594609 // Draw the elapsed time
595610 g .setColor (Color .RED ); // Sets the color to red.
596611 g .setFont (new Font ("Courier New" , Font .PLAIN , 20 ));
@@ -636,8 +651,6 @@ private void printBoardAndCount(Graphics g) {
636651 openTerrainCount = openTerrain ;
637652 }
638653
639-
640-
641654 private void setTimerMageCreation () {
642655 int delay = 50 ;
643656 Timer timer = new Timer (delay , new ActionListener () {
@@ -662,7 +675,7 @@ public void actionPerformed(ActionEvent e) {
662675 timer .start ();
663676 }
664677
665- private void setTimerSolving () {
678+ private void setTimerSolving () {
666679 int delay = 50 ;
667680 Timer timer = new Timer (delay , new ActionListener () {
668681 @ Override
@@ -711,6 +724,4 @@ public static int getTileSize() {
711724 return TILE_SIZE ;
712725 }
713726
714-
715-
716727}// end class
0 commit comments