Added safety checks and cleaned up code

This commit is contained in:
davpapp 2018-01-24 02:56:52 -05:00
parent 6ea01289e9
commit a1f569cddb
6 changed files with 61 additions and 22 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,10 @@
/* Reads a file of coordinates
*
*
* Testing:
* - Only valid coordinates are added (through regex)
* -
*/
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
@ -21,7 +27,7 @@ public class Mouse {
File file = new File(path); File file = new File(path);
FileReader fileReader = new FileReader(file); FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader); 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; String line;
Point lastPoint = new Point(0, 0, 0); Point lastPoint = new Point(0, 0, 0);
@ -29,24 +35,25 @@ public class Mouse {
ArrayList<Point> currentPath = new ArrayList<Point>(); ArrayList<Point> currentPath = new ArrayList<Point>();
currentPath.add(lastPoint); currentPath.add(lastPoint);
//int count = 0;
while ((line = bufferedReader.readLine()) != null) { while ((line = bufferedReader.readLine()) != null) {
if (!isLineValid(line, linePattern)) { if (!isLineValid(line, linePattern)) {
System.out.println(line + " does not match regex -- SKIPPING"); System.out.println(line + " does not match regex -- SKIPPING");
continue; continue;
} }
//count += 1;
//if (count > 1000) break;
Point point = getPointFromLine(line);
if (point.getX() == lastPoint.getX() && point.getY() == lastPoint.getY()) { Point point = getPointFromLine(line);
numberOfRepeats += 1; if (!point.isValid()) {
//System.out.println("Mouse at same point..."); continue;
}
if (point.hasSameLocation(lastPoint)) {
numberOfRepeats++;
if (numberOfRepeats == 20) { if (numberOfRepeats == 20) {
//System.out.println("Creating new path!"); if (currentPath.size() < 5) {
//System.out.println("Current path length:" + currentPath.size()); continue;
}
MousePath newPath = new MousePath(currentPath); MousePath newPath = new MousePath(currentPath);
mousePaths.add(newPath); mousePaths.add(newPath); // Deep copies
currentPath.clear(); currentPath.clear();
} }
} }
@ -56,7 +63,6 @@ public class Mouse {
} }
lastPoint = point; lastPoint = point;
} }
fileReader.close(); fileReader.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -72,12 +78,8 @@ public class Mouse {
} }
private Point getPointFromLine(String line) { private Point getPointFromLine(String line) {
String[] parts = line.split(Pattern.quote(":")); String[] parts = line.split(Pattern.quote(","));
/*System.out.println(line); return new Point(Integer.parseInt(parts[0]), Integer.parseInt(parts[1]), Integer.parseInt(parts[2]));
System.out.println(parts[0]);
System.out.println(parts[1]);
System.out.println(parts[2]);*/
return new Point(0, 0, 0);
} }
public void displayPaths() { public void displayPaths() {
@ -85,5 +87,6 @@ public class Mouse {
path.display(); path.display();
System.out.println("----------------------------------------------------------"); System.out.println("----------------------------------------------------------");
} }
System.out.println("There are " + mousePaths.size() + " paths.");
} }
} }

View File

@ -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; import java.util.ArrayList;
public class MousePath { public class MousePath {
@ -6,6 +17,7 @@ public class MousePath {
private int numPoints; private int numPoints;
private Point startingPoint; private Point startingPoint;
private Point endingPoint; private Point endingPoint;
private int timespan;
public MousePath(ArrayList<Point> _path) public MousePath(ArrayList<Point> _path)
{ {
@ -15,9 +27,9 @@ public class MousePath {
path.add(pointCopy); path.add(pointCopy);
} }
numPoints = path.size(); numPoints = path.size();
System.out.println("Created copy with size: " + path.size()); startingPoint = path.get(0);
//startingPoint = path.get(0); endingPoint = path.get(numPoints - 1);
//endingPoint = path.get(numPoints - 1); timespan = endingPoint.getTime() - startingPoint.getTime();
} }
public ArrayList<Point> getPath() { public ArrayList<Point> getPath() {
@ -32,5 +44,6 @@ public class MousePath {
for (Point p : path) { for (Point p : path) {
System.out.println("(" + p.getX() + ", " + p.getY() + "), " + p.getTime()); System.out.println("(" + p.getX() + ", " + p.getY() + "), " + p.getTime());
} }
System.out.println("Length:" + numPoints + ", Timespan:" + timespan);
} }
} }

View File

@ -25,4 +25,27 @@ public class Point {
{ {
return time; 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);
}
} }