diff --git a/src/algorithm/Explorer.java b/src/algorithm/Explorer.java index 102ba6d03584752768265eef20764f80e55e5a3f..7e9fe2c0e15491928d4961b3f9fc6ac550ec6509 100644 --- a/src/algorithm/Explorer.java +++ b/src/algorithm/Explorer.java @@ -39,7 +39,7 @@ public class Explorer { (int) SharedRessources.getInstance().getVisitMatrix()[index] .getZ(); - if (currentStepsOnField <= steps) { + if (currentStepsOnField <= steps + SharedRessources.getEpsilon()) { return; } diff --git a/src/algorithm/ParallelPathfinder.java b/src/algorithm/ParallelPathfinder.java index 0511c78a042bd0bc57e1ef613bd67a0a90eb1efd..217e09ac9544a2a9bdcc63f8cf41d09e6c8c48a9 100644 --- a/src/algorithm/ParallelPathfinder.java +++ b/src/algorithm/ParallelPathfinder.java @@ -68,10 +68,7 @@ public class ParallelPathfinder { x += dx; y += dy; } - - return path; - } public void draw(Graphics g) { @@ -81,11 +78,28 @@ public class ParallelPathfinder { SharedRessources.getInstance().getWallMatrix()[i + j * SharedRessources.getInstance().getWidth()]; if (b) { g.setColor(Color.blue); + + } else { + Vec3 currentVec + = + SharedRessources.getInstance().getVisitMatrix()[i + j * SharedRessources.getInstance().getWidth()]; + if (currentVec.getX() == 0 && currentVec.getY() == 0) + g.setColor(Color.black); + else { + int green = (int) currentVec.getZ(); + if (green >= 255) + green = 255; + + Color color + = new Color(0, green, 0); + + g.setColor(color); + + } + g.fillRect(i * 1, j * 1, 1, 1); } } } } - - } diff --git a/src/algorithm/WorkingExplorers.java b/src/algorithm/WorkingExplorers.java index edf895d988c5a464a2460d563b53bcd5a06ea852..8b6ad68219f9bc8ba51253f2672fe3e61baaadb0 100644 --- a/src/algorithm/WorkingExplorers.java +++ b/src/algorithm/WorkingExplorers.java @@ -10,14 +10,19 @@ import java.util.concurrent.LinkedBlockingQueue; public class WorkingExplorers extends EagerWorkingThread { + private Collection<Explorer> currentWaitingExplorers; + public WorkingExplorers() { super(); this.startListen(); System.out.println("[*] Thread initialized @" + this.getName()); - } + if (SharedRessources.isUsingQueue()) + currentWaitingExplorers + = new ConcurrentLinkedQueue<>(); + else currentWaitingExplorers = new Stack<Explorer>(); - private ConcurrentLinkedQueue<Explorer> currentWaitingExplorers = new ConcurrentLinkedQueue<Explorer>(); // suited for maze only!!! + } @Override public void onBegin() { @@ -25,21 +30,33 @@ public class WorkingExplorers extends EagerWorkingThread { @Override public void onTrigger() { - if (!currentWaitingExplorers.isEmpty()) - currentWaitingExplorers - .poll() + if (!currentWaitingExplorers.isEmpty()) { + + if (SharedRessources.isUsingQueue()) + ((ConcurrentLinkedQueue<Explorer>) currentWaitingExplorers) + .poll() + .explore(); + else ((Stack<Explorer>) currentWaitingExplorers) + .pop() .explore(); + } } @Override public void onEnd() { - synchronized (SharedRessources.getInstance().getSignalingObject()){ + synchronized (SharedRessources.getInstance().getSignalingObject()) { SharedRessources.getInstance().getSignalingObject().notifyAll(); } } public void add(Explorer explorer) { - this.currentWaitingExplorers.offer(explorer); + + if (SharedRessources.isUsingQueue()) + ((ConcurrentLinkedQueue<Explorer>) currentWaitingExplorers) + .offer(explorer); + else ((Stack<Explorer>) currentWaitingExplorers) + .push(explorer); + this.trigger(); } diff --git a/src/parallel/SharedRessources.java b/src/parallel/SharedRessources.java index 9e99be281053bdbfb2871be4a16b37120a2496b3..7f115e9d32ace52b9d15605f103350caf96a6db8 100644 --- a/src/parallel/SharedRessources.java +++ b/src/parallel/SharedRessources.java @@ -19,6 +19,26 @@ public class SharedRessources { USING_CORES = cores; } + private static int EPSILON; + + public static void setEpsilon(int epsilon) { + EPSILON = epsilon; + } + + public static int getEpsilon() { + return EPSILON; + } + + private static boolean USE_QUEUE; + + public static void useQueue(boolean status) { + USE_QUEUE = status; + } + + public static boolean isUsingQueue() { + return USE_QUEUE; + } + private static SharedRessources sharedRessources; public static SharedRessources getInstance() { diff --git a/src/window/MainWindow.java b/src/window/MainWindow.java index 97adbb75fbd6ae26cdceb34a0a9a53df9452aa16..3600393dad86563b6557548768d24de4e4927e22 100644 --- a/src/window/MainWindow.java +++ b/src/window/MainWindow.java @@ -23,7 +23,10 @@ public class MainWindow extends Game { @Override public void loadGame() { - SharedRessources.setMaximumCores(Integer.MAX_VALUE); + SharedRessources.setMaximumCores(1); + SharedRessources.setEpsilon(0); + SharedRessources.useQueue(true); + try { // MAZE GENERATED WITH http://www.mazegenerator.net/ MapLoader.getDefault().load(ImageIO.read(new File("examples/maze.png"))); @@ -63,7 +66,7 @@ public class MainWindow extends Game { g.setColor(Color.red); for (var pt : pts) { - g.fillRect(pt.x * 1, pt.y * 1, 1, 1); + g.fillRect(pt.x - 2, pt.y - 2, 4, 4); } }