diff --git a/examples/maze2.png b/examples/maze2.png index 1aaec2636c11260c9a9edfce809568154ef6b57d..304c1b4719fbf9bace2456bba719211982f3b9e7 100644 Binary files a/examples/maze2.png and b/examples/maze2.png differ diff --git a/src/approach3/parallel/SharedRessources.java b/src/approach3/parallel/SharedRessources.java index 4860103bc3ea757f95ceaa7cc41f59468331739d..d2b4ea6e41d38d9949e423e514fe47fd7832f3e5 100644 --- a/src/approach3/parallel/SharedRessources.java +++ b/src/approach3/parallel/SharedRessources.java @@ -132,15 +132,21 @@ public class SharedRessources { } public int getStep(int x, int y) { - return data[x + y * width].getZ(); + synchronized (this) { + return data[x + y * width].getZ(); + } } public void setData(int x, int y, int dirX, int dirY, int step) { - data[x + y * width] = new Int3(dirX, dirY, step); + synchronized (this) { + data[x + y * width] = new Int3(dirX, dirY, step); + } } public Int3 getData(int x, int y) { - return data[x + y * width]; + synchronized (this) { + return data[x + y * width]; + } } public boolean isWall(int x, int y) { diff --git a/src/approach3/parallel/WallBasedPathfinder.java b/src/approach3/parallel/WallBasedPathfinder.java index 280f597fcd22325c46d5681f30ba1906afe9342e..8508721bde8c05b9da9ded00393143e4c5b82838 100644 --- a/src/approach3/parallel/WallBasedPathfinder.java +++ b/src/approach3/parallel/WallBasedPathfinder.java @@ -30,10 +30,11 @@ public class WallBasedPathfinder { } + ArrayList<Point> path + = new ArrayList<>(); + public ArrayList<Point> calculatePath(int startX, int startY, int destX, int destY) { - ArrayList<Point> path - = new ArrayList<>(); int x = startX; int y = startY; @@ -52,10 +53,23 @@ public class WallBasedPathfinder { if (dx == 0 && dy == 0) break; + // System.out.println(Lexicon.getInstance().getText(new Point(dx, dy))); x += dx; y += dy; + + /* + try { + Thread.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + */ } + + System.out.println("Done tracing back."); + return path; } @@ -117,5 +131,11 @@ public class WallBasedPathfinder { SharedRessources.getInstance().getStart().x * MainWindowApproach3.SIZE, SharedRessources.getInstance().getStart().y * MainWindowApproach3.SIZE, MainWindowApproach3.SIZE, MainWindowApproach3.SIZE); + + g.setColor(Color.red); + for (int i = 0; i < path.size(); i++) { + var pt = path.get(i); + g.fillRect(pt.x * MainWindowApproach3.SIZE, pt.y * MainWindowApproach3.SIZE, MainWindowApproach3.SIZE, MainWindowApproach3.SIZE); + } } } diff --git a/src/approach3/parallel/operations/Flood.java b/src/approach3/parallel/operations/Flood.java index 6accf9ca81a1cc4ec1e18f5742372aab818c7228..3c65bfa1701c7cd99a50f0f930a54292f7373640 100644 --- a/src/approach3/parallel/operations/Flood.java +++ b/src/approach3/parallel/operations/Flood.java @@ -17,7 +17,7 @@ public class Flood implements Runnable { private int steps; - private ArrayDeque<Point> openPoints + private Queue<Point> openPoints = new ArrayDeque<>(); public Flood(int x, int y, int steps) { @@ -34,10 +34,7 @@ public class Flood implements Runnable { openPoints.offer(new Point(px, py)); - while (!openPoints.isEmpty()) { - - if (SharedRessources.getInstance().isFound()) - break; + while (!openPoints.isEmpty() && !SharedRessources.getInstance().isFound()) { Point cur = openPoints.poll(); @@ -138,6 +135,8 @@ public class Flood implements Runnable { } + // System.out.println("[*] Done flooding."); + } } diff --git a/src/approach3/parallel/operations/Trace.java b/src/approach3/parallel/operations/Trace.java index 6a42ffa0e49eebd7f37415ed6eeadf3c88d6f519..855cd858f169b156694567b5af286090c92f81b1 100644 --- a/src/approach3/parallel/operations/Trace.java +++ b/src/approach3/parallel/operations/Trace.java @@ -75,11 +75,13 @@ public class Trace implements Runnable { if (SharedRessources.getInstance().isWall(x0, y0)) { + // System.out.println("Collision at step #" + step); + if (step < SharedRessources.getInstance().getStep(oldX - 1, oldY)) { SharedRessources .getInstance() .getExecutor() - .execute(new Flood(oldX - 1, oldY, step)); + .submit(new Flood(oldX - 1, oldY, step)); SharedRessources.getInstance() .setData(oldX - 1, oldY, 1, 0, step); @@ -90,7 +92,7 @@ public class Trace implements Runnable { SharedRessources .getInstance() .getExecutor() - .execute(new Flood(oldX + 1, oldY, step)); + .submit(new Flood(oldX + 1, oldY, step)); SharedRessources.getInstance() .setData(oldX + 1, oldY, -1, 0, step); @@ -100,7 +102,7 @@ public class Trace implements Runnable { SharedRessources .getInstance() .getExecutor() - .execute(new Flood(oldX, oldY - 1, step)); + .submit(new Flood(oldX, oldY - 1, step)); SharedRessources.getInstance() .setData(oldX, oldY - 1, 0, 1, step); @@ -111,7 +113,7 @@ public class Trace implements Runnable { SharedRessources .getInstance() .getExecutor() - .execute(new Flood(oldX, oldY + 1, step)); + .submit(new Flood(oldX, oldY + 1, step)); SharedRessources.getInstance() .setData(oldX, oldY + 1, 0, -1, step); @@ -126,5 +128,7 @@ public class Trace implements Runnable { } + // System.out.println("[*] Done line"); + } } diff --git a/src/approach3/window/MainWindowApproach3.java b/src/approach3/window/MainWindowApproach3.java index e5fcc745ff62829ff0a1e7ecf3da660538f8d214..fdad62e21ba76b1655ffb21d4a215a695cf6a6fe 100644 --- a/src/approach3/window/MainWindowApproach3.java +++ b/src/approach3/window/MainWindowApproach3.java @@ -19,7 +19,7 @@ public class MainWindowApproach3 extends Game { } - public static final int SIZE = 5; + public static final int SIZE = 1; private WallBasedPathfinder pathfinder; private ArrayList<Point> foundPath; @@ -30,12 +30,12 @@ public class MainWindowApproach3 extends Game { @Override public void loadGame() { - SharedRessources.getInstance().setMaxCores(4); + SharedRessources.getInstance().setMaxCores(2); SharedRessources.getInstance() - .setCurrentProfile(SharedRessources.Profile.MEDIUM); + .setCurrentProfile(SharedRessources.Profile.BEST_NODES); try { - MapLoader.getDefault().load(ImageIO.read(new File("examples/debug.png"))); + MapLoader.getDefault().load(ImageIO.read(new File("examples/maze3.png"))); } catch (IOException e) { e.printStackTrace(); } @@ -55,19 +55,43 @@ public class MainWindowApproach3 extends Game { SharedRessources.getInstance().setEnd(this.getMouseX() / SIZE, this.getMouseY() / SIZE); if (isKeyDown(KeyEvent.VK_ENTER) && !pressed) { + + + SharedRessources + .getInstance() + .setStart(12, 38); + + SharedRessources + .getInstance() + .setEnd(793, 741); + + System.out.println("BEGIN: " + SharedRessources.getInstance().getStart()); + System.out.println("END: " + SharedRessources.getInstance().getEnd()); + + pressed = true; pathfinder.find(); - while (!SharedRessources.getInstance().isFound()) ; + SharedRessources + .getInstance().getExecutor() + .execute(new Runnable() { + @Override + public void run() { + while (!SharedRessources.getInstance().isFound()) ; + + int beginX = SharedRessources.getInstance().getStart().x; + int beginY = SharedRessources.getInstance().getStart().y; + + int endX = SharedRessources.getInstance().getEnd().x; + int endY = SharedRessources.getInstance().getEnd().y; - int beginX = SharedRessources.getInstance().getStart().x; - int beginY = SharedRessources.getInstance().getStart().y; - int endX = SharedRessources.getInstance().getEnd().x; - int endY = SharedRessources.getInstance().getEnd().y; + foundPath = pathfinder.calculatePath(beginX, beginY, endX, endY); + } + }); + - foundPath = pathfinder.calculatePath(beginX, beginY, endX, endY); } if (isKeyUp(KeyEvent.VK_ENTER)) @@ -86,9 +110,7 @@ public class MainWindowApproach3 extends Game { if (foundPath == null) return; - g.setColor(Color.red); - for (var pt : foundPath) - g.fillRect(pt.x * SIZE, pt.y * SIZE, SIZE, SIZE); + } } diff --git a/src/structs/Int2.java b/src/structs/Int2.java new file mode 100644 index 0000000000000000000000000000000000000000..040b592809ccd9925d87808a29b24d47ccf088a9 --- /dev/null +++ b/src/structs/Int2.java @@ -0,0 +1,28 @@ +package structs; + +public class Int2 { + private int x; + private int y; + + public Int2(int x, int y) { + this.x = x; + this.y = y; + } + + public void setX(int x) { + this.x = x; + } + + public void setY(int y) { + this.y = y; + } + + public int getX() { + return x; + } + + public int getY() { + return y; + } + +}