Skip to content

Commit

Permalink
compeltes search for closest creek
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvyLiu committed Mar 25, 2024
1 parent 1279808 commit ddb234e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
35 changes: 24 additions & 11 deletions src/main/java/ca/mcmaster/se2aa4/island/team101/AreaMap.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ca.mcmaster.se2aa4.island.team101;
import java.lang.Math;
import java.util.List;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.awt.Point;
import java.util.Arrays;
import org.apache.logging.log4j.LogManager;
Expand All @@ -25,33 +25,46 @@ public void updateMap(Point currentPosition, Response response, Command latestCo
if(type.equals("scan")){
ScanResponse scanResponse = (ScanResponse)response;
Tile tile = new Tile();
Point p = new Point(currentPosition.x, currentPosition.y);
// need to make new point otherwise code breaks
tile.fillTile((ScanResponse)response);
if ((scanResponse.getCreeks().length()) > 0){
logger.info(tile.getCreekID() + "creek id" + currentPosition);
creekMap.put(currentPosition, tile.getCreekID());
creekMap.put(p, tile.getCreekID());
logger.info((Arrays.asList(creekMap)) + "creekmap");
}
if (tile.getSiteID() != null){
logger.info(tile.getSiteID() + "site id" + currentPosition);
emergencyPoint = currentPosition;
logger.info(tile.getSiteID() + "site id" + p);
emergencyPoint = p;
}
}
}

public String findClosestCreek(){
logger.info((Arrays.asList(creekMap)));

for (Point p : creekMap.keySet()){
logger.info(p + "point of creek");
}
Point closestInlet = new Point(0,0);
double minDist = 100000, distx, disty, calc_dist;
double minDist = Integer.MAX_VALUE, distx, disty, calc_dist;
if (emergencyPoint == null){
return "nothing";
List<String> valuesList = new ArrayList<String>(creekMap.values());
return valuesList.get(0);
}
for (HashMap.Entry<Point, String> entry : creekMap.entrySet()){
Point key = entry.getKey();
String id = entry.getValue();


distx = emergencyPoint.getX() - key.getX();
disty = emergencyPoint.getY() - key.getY();
calc_dist = Math.pow((Math.pow(distx, 2) + Math.pow(disty, 2)), 0.5);

if (calc_dist <= minDist){
minDist = calc_dist;
closestInlet = key;
}

}
// find the closest creek using the creekmap
// case where no closest creek is found is NOT ACCOUNTED FOR (i.e. closestInlet is null)
return creekMap.get(0);
return creekMap.get(closestInlet);
}
}
2 changes: 1 addition & 1 deletion src/main/java/ca/mcmaster/se2aa4/island/team101/Drone.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Drone implements Traveler{
private AreaMap map;
private Response latestResponse;
private Compass compass;
private DroneController controller;
private Controller controller;
private Command latestCommand;

private final Logger logger = LogManager.getLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,8 @@ public void acknowledgeResults(String s) {

@Override
public String deliverFinalReport() {
logger.info("final");
String closest = drone.getClosestCreek();
logger.info(closest + "final");
logger.info(closest);
return closest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ public ScanResponse(int cost, JSONObject extras, String status){
this.sites = extras.optJSONArray("sites");
}

@Override
public int getCost(){
return this.cost;
}

public JSONArray getBiomes(){
return this.biomes;
}
Expand Down

0 comments on commit ddb234e

Please sign in to comment.