Added safety checks and cleaned up code
This commit is contained in:
parent
6ea01289e9
commit
a1f569cddb
BIN
bin/Mouse.class
BIN
bin/Mouse.class
Binary file not shown.
Binary file not shown.
BIN
bin/Point.class
BIN
bin/Point.class
Binary file not shown.
|
@ -1,4 +1,10 @@
|
|||
|
||||
/* Reads a file of coordinates
|
||||
*
|
||||
*
|
||||
* Testing:
|
||||
* - Only valid coordinates are added (through regex)
|
||||
* -
|
||||
*/
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
|
@ -21,32 +27,33 @@ public class Mouse {
|
|||
File file = new File(path);
|
||||
FileReader fileReader = new FileReader(file);
|
||||
BufferedReader bufferedReader = new BufferedReader(fileReader);
|
||||
Pattern linePattern = Pattern.compile("x:[0-9]* y:[0-9]* [0-9]*");
|
||||
Pattern linePattern = Pattern.compile("[0-9]*,[0-9]*,[0-9]*$");
|
||||
|
||||
String line;
|
||||
Point lastPoint = new Point(0, 0, 0);
|
||||
int numberOfRepeats = 0;
|
||||
ArrayList<Point> currentPath = new ArrayList<Point>();
|
||||
currentPath.add(lastPoint);
|
||||
|
||||
//int count = 0;
|
||||
|
||||
while ((line = bufferedReader.readLine()) != null) {
|
||||
if (!isLineValid(line, linePattern)) {
|
||||
System.out.println(line + " does not match regex -- SKIPPING");
|
||||
continue;
|
||||
}
|
||||
//count += 1;
|
||||
//if (count > 1000) break;
|
||||
|
||||
Point point = getPointFromLine(line);
|
||||
if (!point.isValid()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (point.getX() == lastPoint.getX() && point.getY() == lastPoint.getY()) {
|
||||
numberOfRepeats += 1;
|
||||
//System.out.println("Mouse at same point...");
|
||||
if (point.hasSameLocation(lastPoint)) {
|
||||
numberOfRepeats++;
|
||||
if (numberOfRepeats == 20) {
|
||||
//System.out.println("Creating new path!");
|
||||
//System.out.println("Current path length:" + currentPath.size());
|
||||
if (currentPath.size() < 5) {
|
||||
continue;
|
||||
}
|
||||
MousePath newPath = new MousePath(currentPath);
|
||||
mousePaths.add(newPath);
|
||||
mousePaths.add(newPath); // Deep copies
|
||||
currentPath.clear();
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +63,6 @@ public class Mouse {
|
|||
}
|
||||
lastPoint = point;
|
||||
}
|
||||
|
||||
fileReader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
@ -72,12 +78,8 @@ public class Mouse {
|
|||
}
|
||||
|
||||
private Point getPointFromLine(String line) {
|
||||
String[] parts = line.split(Pattern.quote(":"));
|
||||
/*System.out.println(line);
|
||||
System.out.println(parts[0]);
|
||||
System.out.println(parts[1]);
|
||||
System.out.println(parts[2]);*/
|
||||
return new Point(0, 0, 0);
|
||||
String[] parts = line.split(Pattern.quote(","));
|
||||
return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
|
||||
}
|
||||
|
||||
public void displayPaths() {
|
||||
|
@ -85,5 +87,6 @@ public class Mouse {
|
|||
path.display();
|
||||
System.out.println("----------------------------------------------------------");
|
||||
}
|
||||
System.out.println("There are " + mousePaths.size() + " paths.");
|
||||
}
|
||||
}
|
|
@ -1,3 +1,14 @@
|
|||
/*
|
||||
* Represents each mouse path as an ArrayList of points.
|
||||
*
|
||||
* Testing:
|
||||
* - Each point must be valid.
|
||||
* - Timespan is reasonable
|
||||
* - Does not go off screen
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class MousePath {
|
||||
|
@ -6,6 +17,7 @@ public class MousePath {
|
|||
private int numPoints;
|
||||
private Point startingPoint;
|
||||
private Point endingPoint;
|
||||
private int timespan;
|
||||
|
||||
public MousePath(ArrayList<Point> _path)
|
||||
{
|
||||
|
@ -15,9 +27,9 @@ public class MousePath {
|
|||
path.add(pointCopy);
|
||||
}
|
||||
numPoints = path.size();
|
||||
System.out.println("Created copy with size: " + path.size());
|
||||
//startingPoint = path.get(0);
|
||||
//endingPoint = path.get(numPoints - 1);
|
||||
startingPoint = path.get(0);
|
||||
endingPoint = path.get(numPoints - 1);
|
||||
timespan = endingPoint.getTime() - startingPoint.getTime();
|
||||
}
|
||||
|
||||
public ArrayList<Point> getPath() {
|
||||
|
@ -32,5 +44,6 @@ public class MousePath {
|
|||
for (Point p : path) {
|
||||
System.out.println("(" + p.getX() + ", " + p.getY() + "), " + p.getTime());
|
||||
}
|
||||
System.out.println("Length:" + numPoints + ", Timespan:" + timespan);
|
||||
}
|
||||
}
|
|
@ -25,4 +25,27 @@ public class Point {
|
|||
{
|
||||
return time;
|
||||
}
|
||||
|
||||
public boolean hasSameLocation(Point p2) {
|
||||
return (this.x == p2.getX() && this.y == p2.getY());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
if (!(obj instanceof Point)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Point p = (Point) obj;
|
||||
|
||||
// Compare the data members and return accordingly
|
||||
return (this.x == p.x && this.y == p.y && this.time == p.time);
|
||||
}
|
||||
|
||||
public boolean isValid() {
|
||||
return (x >= 0 && x < 1920 && y >= 0 && y < 1920 && time >= 0);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue