public class TargetVisitChecker
extends java.lang.Object
The set of locations is represented as a pair of identically sized arrays. The first target's latitude is the first entry in the latitudes array; its longitude is the first entry in the longitudes array. The path array contains indexes of the captured targets in the order they were captured. To ensure that's it's possible to capture all targets, the path array has the same size as the coordinate arrays. If not all targets have been captured yet, unused slots of the path array have the value -1. The path array is filled starting at the beginning, so all instances of -1 are contiguous at the end of the array.
For example, if latitudes is [40.2, 40.8], longitudes is [-88.5, -88.3], and path is [1, -1], there are two targets: (40.2, -88.5) and (40.8, -88.3). The target at index 1, that is, (40.8, -88.3), has been captured; the other has not. If path were [-1, -1], that would indicate that the user has captured no targets so far. If path were [0, 1], that would indicate that the user first captured target 0, that is, (40.2, -88.5), and then captured target 1.
You need to complete the three functions here. You can then use them to implement the game in the location update handler of GameActivity!
Constructor and Description |
---|
TargetVisitChecker() |
Modifier and Type | Method and Description |
---|---|
static boolean |
checkSnakeRule(double[] latitudes,
double[] longitudes,
int[] path,
int tryVisit)
Determines whether the specified target can be visited without violating the snake rule.
|
static int |
getTargetWithinRange(double[] latitudes,
double[] longitudes,
int[] path,
double currentLatitude,
double currentLongitude,
int range)
Gets an index of an unvisited target within the specified range of the current location.
|
static int |
visitTarget(int[] path,
int targetIndex)
Marks a target captured by putting its index in the first available (-1) slot of the path array.
|
public static boolean checkSnakeRule(double[] latitudes, double[] longitudes, int[] path, int tryVisit)
The snake rule is violated if the new line created between the last captured target and this new target would cross the straight line connecting two sequentially captured targets. For example, there is a line between the first-captured and second-captured targets, and between the second-captured and third-captured targets, but no line directly connecting the first-captured and third-captured targets. If zero or one targets have been captured so far, there are no lines and it is permissible to capture any target.
It is assumed that all parameters are valid. The three arrays are non-null and of the same length. All coordinates are valid. The index of the target to visit is a valid index into the coordinate arrays. The index of the target to visit does not appear in the path array (i.e. the target has not been visited yet). No existing lines violate the snake rule.
This function should not modify the arrays it receives.
latitudes
- latitudes of all targetslongitudes
- longitudes of all targetspath
- indexes of targets visited so far (-1 for empty slots)tryVisit
- index of the target to try to visitpublic static int getTargetWithinRange(double[] latitudes, double[] longitudes, int[] path, double currentLatitude, double currentLongitude, int range)
A target is visited if its index appears in the path array.
It is assumed that all parameters are valid. The three arrays are non-null and of the same length. All coordinates are valid. The range is positive.
This function should not modify the arrays it receives.
latitudes
- latitudes of all targetslongitudes
- longitudes of all targets (same size as latitudes)path
- indexes of targets visited so far (same size as latitudes, -1 for empty slots)currentLatitude
- the current latitudecurrentLongitude
- the current longituderange
- maximum distance to target, in meterspublic static int visitTarget(int[] path, int targetIndex)
It is assumed that all parameters are valid. The path array is non-null and does not yet contain the target index. The target index is non-negative.
path
- the path arraytargetIndex
- the target being visited