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, 40.5], longitudes is [-88.5, -88.3, -88.0], and path is [1, -1, -1], there are three targets: (40.2, -88.5), (40.8, -88.3), and (40.5, -88.0). The target at index 1, that is, (40.8, -88.3), has been captured; the other two have not. If path were [-1, -1, -1], that would indicate that the user has captured no targets so far. If path were [0, 1, -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 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 |
getVisitCandidate(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 boolean |
isTargetVisited(int[] path,
int targetIndex)
Determines whether a target is already visited.
|
static boolean |
isTargetWithinRange(double[] latitudes,
double[] longitudes,
int targetIndex,
double currentLatitude,
double currentLongitude,
int range)
Determines whether the specified target is within 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 targets (same order as latitudes)path
- indexes of targets visited so far (same size as latitudes, -1 for empty slots)tryVisit
- index of the target to try to visitpublic static int getVisitCandidate(double[] latitudes, double[] longitudes, int[] path, double currentLatitude, double currentLongitude, int range)
It is assumed that all parameters are valid. The 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
- the latitudes of all targetslongitudes
- the longitudes of all targets (same order 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 boolean isTargetVisited(int[] path, int targetIndex)
A target is visited if its index appears in the path array.
It is assumed that the index is valid. This function should not modify the array it receives.
path
- indexes of targets visited so far (-1 for empty slots)targetIndex
- the index of the target to check for visitednesspublic static boolean isTargetWithinRange(double[] latitudes, double[] longitudes, int targetIndex, double currentLatitude, double currentLongitude, int range)
It is assumed that all parameters are valid. The latitudes and longitudes arrays are the same size. All coordinates are valid. The range is positive.
This function should not modify the arrays it receives.
latitudes
- the latitudes of all targets in the gamelongitudes
- the longitudes of all targets in the game (same order as latitudes)targetIndex
- the index (into the coordinate arrays) of the target to check for proximitycurrentLatitude
- the player's current latitudecurrentLongitude
- the player's current longituderange
- the proximity thresholdpublic 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