diff --git a/bin/Mouse.class b/bin/Mouse.class index 2470a2e..b0c4576 100644 Binary files a/bin/Mouse.class and b/bin/Mouse.class differ diff --git a/bin/MousePath.class b/bin/MousePath.class index 1668378..c1374bd 100644 Binary files a/bin/MousePath.class and b/bin/MousePath.class differ diff --git a/bin/Point.class b/bin/Point.class index b32fe88..8b3b6dc 100644 Binary files a/bin/Point.class and b/bin/Point.class differ diff --git a/src/Mouse.java b/src/Mouse.java index c0d6b99..8cb7b40 100644 --- a/src/Mouse.java +++ b/src/Mouse.java @@ -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 currentPath = new ArrayList(); 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."); } } \ No newline at end of file diff --git a/src/MousePath.java b/src/MousePath.java index 3cbfe8a..c18b2f7 100644 --- a/src/MousePath.java +++ b/src/MousePath.java @@ -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 _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 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); } } \ No newline at end of file diff --git a/src/Point.java b/src/Point.java index b6280a9..26313e6 100644 --- a/src/Point.java +++ b/src/Point.java @@ -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); + } } \ No newline at end of file