Skip to content
Snippets Groups Projects
Commit c863c073 authored by NET-D3v3l0p3r's avatar NET-D3v3l0p3r
Browse files

AlhamdulIllah

parent 2ed1eea3
No related branches found
No related tags found
No related merge requests found
examples/bug.png

157 KiB

examples/maze3.png

37.6 KiB

package approach3.misc;
import structs.Int3;
import java.awt.*;
import java.util.HashMap;
public class Lexicon {
private HashMap<Point, String> directionOrientation
= new HashMap<>() {
{
put(new Point(+1, 0), "Osten");
put(new Point(-1, 0), "Westen");
put(new Point(0, -1), "Norden");
put(new Point(0, +1), "Süden");
put(new Point(+1, -1), "Nord-Osten");
put(new Point(-1, -1), "Nord-Westen");
put(new Point(+1, +1), "Süd-Osten");
put(new Point(-1, +1), "Süd-Westen");
}
};
private static Lexicon lexicon;
public static Lexicon getInstance() {
if (lexicon == null)
lexicon = new Lexicon();
return lexicon;
}
public String getText(Point direction) {
return directionOrientation.get(direction);
}
}
...@@ -10,12 +10,48 @@ public class SharedRessources { ...@@ -10,12 +10,48 @@ public class SharedRessources {
private static final int AVAIBLE_CORES private static final int AVAIBLE_CORES
= Runtime.getRuntime().availableProcessors(); = Runtime.getRuntime().availableProcessors();
private static int MAX_CORES = 1; public enum Profile {
public static void setMaxCores(int cores) { /***
MAX_CORES = cores >= AVAIBLE_CORES ? AVAIBLE_CORES : cores; * Every iteration in flood a line to dest will be constructed
* <br>Information: The path found can be optimal
*/
BEST_NODES(1),
/***
* Every 18th iteration in flood a line to dest will be constructed
* <br>Warning: The path found won't be optimal
*/
MEDIUM(18),
/***
* Every 64th iteration in flood a line to dest will be constructed
* <br>Warning: The path found won't be optimal
*/
FAST(64),
/***
* Every 128th iteration in flood a line to dest will be constructed
* <br>Warning: By using this profile, the algorithm can fail to find a solution
* <br>Warning: The path found won't be optimal
*/
FASTER(128),
/***
* Every 256th iteration in flood a line to dest will be constructed
* <br>Warning: By using this profile, the algorithm can fail to find a solution
* <br>Warning: The path found won't be optimal
*/
FASTEST(256);
private final int profile;
Profile(int profile) {
this.profile = profile;
}
public int getValue() {
return profile;
}
} }
private static SharedRessources sharedRessources; private static SharedRessources sharedRessources;
public static SharedRessources getInstance() { public static SharedRessources getInstance() {
...@@ -23,12 +59,15 @@ public class SharedRessources { ...@@ -23,12 +59,15 @@ public class SharedRessources {
sharedRessources sharedRessources
= new SharedRessources(); = new SharedRessources();
return sharedRessources; return sharedRessources;
} }
private ExecutorService threadPool; private ExecutorService threadPool;
private Int3[] data; private Int3[] data;
private boolean[] walls; private boolean[] walls;
private int width; private int width;
...@@ -39,8 +78,12 @@ public class SharedRessources { ...@@ -39,8 +78,12 @@ public class SharedRessources {
private volatile boolean found; private volatile boolean found;
private Profile currentProfile;
private int maxCores = 1;
private SharedRessources() { private SharedRessources() {
threadPool = Executors.newFixedThreadPool(MAX_CORES);
} }
public void initialize(int w, int h) { public void initialize(int w, int h) {
...@@ -53,6 +96,10 @@ public class SharedRessources { ...@@ -53,6 +96,10 @@ public class SharedRessources {
data[i] = new Int3(0, 0, Integer.MAX_VALUE); data[i] = new Int3(0, 0, Integer.MAX_VALUE);
walls = new boolean[width * height]; walls = new boolean[width * height];
threadPool = Executors.newFixedThreadPool(maxCores);
System.out.println("[*] Using " + maxCores + " cores.");
} }
...@@ -85,16 +132,12 @@ public class SharedRessources { ...@@ -85,16 +132,12 @@ public class SharedRessources {
} }
public int getStep(int x, int y) { public int getStep(int x, int y) {
synchronized (this) {
return data[x + y * width].getZ(); return data[x + y * width].getZ();
} }
}
public void setData(int x, int y, int dirX, int dirY, int step) { public void setData(int x, int y, int dirX, int dirY, int step) {
synchronized (this) {
data[x + y * width] = new Int3(dirX, dirY, step); data[x + y * width] = new Int3(dirX, dirY, step);
} }
}
public Int3 getData(int x, int y) { public Int3 getData(int x, int y) {
return data[x + y * width]; return data[x + y * width];
...@@ -116,4 +159,16 @@ public class SharedRessources { ...@@ -116,4 +159,16 @@ public class SharedRessources {
return this.height; return this.height;
} }
public void setCurrentProfile(Profile profile) {
this.currentProfile = profile;
}
public Profile getCurrentProfile() {
return currentProfile;
}
public void setMaxCores(int cores) {
maxCores = cores >= AVAIBLE_CORES ? AVAIBLE_CORES : cores;
}
} }
package approach3.parallel; package approach3.parallel;
import approach3.misc.Lexicon;
import approach3.parallel.operations.Trace; import approach3.parallel.operations.Trace;
import approach3.window.MainWindowApproach3;
import structs.Int3; import structs.Int3;
import structs.Vec3; import structs.Vec3;
...@@ -28,17 +30,17 @@ public class WallBasedPathfinder { ...@@ -28,17 +30,17 @@ public class WallBasedPathfinder {
} }
ArrayList<Point> debug = new ArrayList<>();
public ArrayList<Point> calculatePath(int startX, int startY, int destX, int destY) { public ArrayList<Point> calculatePath(int startX, int startY, int destX, int destY) {
ArrayList<Point> path
= new ArrayList<>();
int x = startX; int x = startX;
int y = startY; int y = startY;
while (x != destX || y != destY) { while (x != destX || y != destY) {
debug.add(new Point(x, y)); path.add(new Point(x, y));
Int3 currentDir Int3 currentDir
= =
...@@ -50,72 +52,70 @@ public class WallBasedPathfinder { ...@@ -50,72 +52,70 @@ public class WallBasedPathfinder {
if (dx == 0 && dy == 0) if (dx == 0 && dy == 0)
break; break;
// System.out.println(Lexicon.getInstance().getText(new Point(dx, dy)));
x += dx; x += dx;
y += dy; y += dy;
} }
return path;
System.out.println("Done.");
return debug;
} }
public void draw(Graphics g) { public void draw(Graphics g, boolean showMeta) {
for (int j = 0; j < SharedRessources.getInstance().getHeight(); j++) { for (int j = 0; j < SharedRessources.getInstance().getHeight(); j++) {
for (int i = 0; i < SharedRessources.getInstance().getWidth(); i++) { for (int i = 0; i < SharedRessources.getInstance().getWidth(); i++) {
if (SharedRessources.getInstance().isWall(i, j)) { if (SharedRessources.getInstance().isWall(i, j)) {
g.setColor(Color.blue); g.setColor(Color.blue);
g.fillRect(i * 5, j * 5, 5, 5); g.fillRect(i * MainWindowApproach3.SIZE, j * MainWindowApproach3.SIZE,
} else { MainWindowApproach3.SIZE, MainWindowApproach3.SIZE);
int dx = (int) SharedRessources.getInstance().getData(i, j).getX() * 5; } else if (SharedRessources.getInstance().getStep(i, j) != Integer.MAX_VALUE && showMeta) {
int dy = (int) SharedRessources.getInstance().getData(i, j).getY() * 5; int dx =
(int) SharedRessources.getInstance().getData(i, j).getX() * MainWindowApproach3.SIZE;
int dy =
(int) SharedRessources.getInstance().getData(i, j).getY() * MainWindowApproach3.SIZE;
if (dx != 0 || dy != 0) {
int green = SharedRessources.getInstance().getStep(i, j); int green = SharedRessources.getInstance().getStep(i, j);
if (green >= 255) if (green >= 255)
green = 255; green = 255;
Color color Color color
= new Color(0, 255, 0); = new Color(0, green, 0);
g.setColor(color); g.setColor(color);
g.fillRect(i * MainWindowApproach3.SIZE, j * MainWindowApproach3.SIZE,
MainWindowApproach3.SIZE, MainWindowApproach3.SIZE);
g.fillRect(i * 5, j * 5, 5, 5);
g.setColor(Color.red); g.setColor(Color.red);
g.drawLine(i * 5, j * 5, i * 5 + dx, j * 5 + dy); g.drawLine(
i * MainWindowApproach3.SIZE,
j * MainWindowApproach3.SIZE,
i * MainWindowApproach3.SIZE + dx,
j * MainWindowApproach3.SIZE + dy);
} else { } else {
g.setColor(Color.black); g.setColor(Color.black);
g.fillRect(i * 5, j * 5, 5, 5); g.fillRect(i * MainWindowApproach3.SIZE, j * MainWindowApproach3.SIZE,
} MainWindowApproach3.SIZE, MainWindowApproach3.SIZE);
} }
} }
} }
if (SharedRessources.getInstance().getEnd() == null || SharedRessources.getInstance().getStart() == null) if (SharedRessources.getInstance().
getEnd() == null || SharedRessources.getInstance().getStart() == null)
return; return;
g.setColor(Color.yellow); g.setColor(Color.yellow);
g.fillRect(SharedRessources.getInstance().getEnd().x * 5, g.fillRect(
SharedRessources.getInstance().getEnd().y * 5, 5 SharedRessources.getInstance().getEnd().x * MainWindowApproach3.SIZE,
, 5); SharedRessources.getInstance().getEnd().y * MainWindowApproach3.SIZE,
MainWindowApproach3.SIZE, MainWindowApproach3.SIZE);
g.setColor(Color.red);
g.fillRect(SharedRessources.getInstance().getStart().x * 5,
SharedRessources.getInstance().getStart().y * 5
, 5, 5);
g.setColor(Color.red); g.setColor(Color.red);
g.fillRect(
for (int i = 0; i < debug.size(); i++) SharedRessources.getInstance().getStart().x * MainWindowApproach3.SIZE,
g.fillRect(debug.get(i).x * 5, debug.get(i).y * 5, 5, 5); SharedRessources.getInstance().getStart().y * MainWindowApproach3.SIZE,
MainWindowApproach3.SIZE, MainWindowApproach3.SIZE);
} }
} }
...@@ -17,7 +17,7 @@ public class Flood implements Runnable { ...@@ -17,7 +17,7 @@ public class Flood implements Runnable {
private int steps; private int steps;
private Queue<Point> openPoints private ArrayDeque<Point> openPoints
= new ArrayDeque<>(); = new ArrayDeque<>();
public Flood(int x, int y, int steps) { public Flood(int x, int y, int steps) {
...@@ -123,7 +123,7 @@ public class Flood implements Runnable { ...@@ -123,7 +123,7 @@ public class Flood implements Runnable {
} }
if (steps % 18 == 0) if (steps % SharedRessources.getInstance().getCurrentProfile().getValue() == 0)
SharedRessources SharedRessources
.getInstance() .getInstance()
.getExecutor() .getExecutor()
......
...@@ -46,7 +46,7 @@ public class Trace implements Runnable { ...@@ -46,7 +46,7 @@ public class Trace implements Runnable {
if (x0 == x1 && y0 == y1) { if (x0 == x1 && y0 == y1) {
SharedRessources.getInstance().setFound(true); SharedRessources.getInstance().setFound(true);
System.out.println("FOUND!!!!!!!!"); System.out.println("Start-End-Connection established");
break; break;
} }
......
package approach3.parallel.path;
import java.awt.*;
import java.util.ArrayList;
public class PathOptimization {
private static PathOptimization pathOptimization;
public static PathOptimization getDefault() {
if (pathOptimization == null)
pathOptimization = new PathOptimization();
return pathOptimization;
}
public ArrayList<Point> optimize(ArrayList<Point> path) {
return null;
}
}
...@@ -7,7 +7,6 @@ import framework.Game; ...@@ -7,7 +7,6 @@ import framework.Game;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent; import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File; import java.io.File;
...@@ -20,33 +19,40 @@ public class MainWindowApproach3 extends Game { ...@@ -20,33 +19,40 @@ public class MainWindowApproach3 extends Game {
} }
public static final int SIZE = 5;
private WallBasedPathfinder pathfinder; private WallBasedPathfinder pathfinder;
private ArrayList<Point> foundPath;
private boolean pressed;
@Override @Override
public void loadGame() { public void loadGame() {
SharedRessources.setMaxCores(4); SharedRessources.getInstance().setMaxCores(4);
SharedRessources.getInstance()
.setCurrentProfile(SharedRessources.Profile.MEDIUM);
try { try {
MapLoader.getDefault().load(ImageIO.read(new File("examples/maze1.png"))); MapLoader.getDefault().load(ImageIO.read(new File("examples/debug.png")));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
pathfinder = new WallBasedPathfinder(); pathfinder = new WallBasedPathfinder();
}
private boolean pressed; }
@Override @Override
public void updateGame() { public void updateGame() {
if (isMouseKeyDown(MouseEvent.BUTTON1)) if (isMouseKeyDown(MouseEvent.BUTTON1))
SharedRessources.getInstance().setStart(this.getMouseX() / 5, this.getMouseY() / 5); SharedRessources.getInstance().setStart(this.getMouseX() / SIZE, this.getMouseY() / SIZE);
if (isMouseKeyDown(MouseEvent.BUTTON3)) if (isMouseKeyDown(MouseEvent.BUTTON3))
SharedRessources.getInstance().setEnd(this.getMouseX() / 5, this.getMouseY() / 5); SharedRessources.getInstance().setEnd(this.getMouseX() / SIZE, this.getMouseY() / SIZE);
if (isKeyDown(KeyEvent.VK_ENTER) && !pressed) { if (isKeyDown(KeyEvent.VK_ENTER) && !pressed) {
pressed = true; pressed = true;
...@@ -61,16 +67,7 @@ public class MainWindowApproach3 extends Game { ...@@ -61,16 +67,7 @@ public class MainWindowApproach3 extends Game {
int endX = SharedRessources.getInstance().getEnd().x; int endX = SharedRessources.getInstance().getEnd().x;
int endY = SharedRessources.getInstance().getEnd().y; int endY = SharedRessources.getInstance().getEnd().y;
foundPath = pathfinder.calculatePath(beginX, beginY, endX, endY);
SharedRessources.getInstance()
.getExecutor()
.execute(new Runnable() {
@Override
public void run() {
pts = pathfinder.calculatePath(beginX, beginY, endX, endY);
}
});
} }
if (isKeyUp(KeyEvent.VK_ENTER)) if (isKeyUp(KeyEvent.VK_ENTER))
...@@ -79,13 +76,19 @@ public class MainWindowApproach3 extends Game { ...@@ -79,13 +76,19 @@ public class MainWindowApproach3 extends Game {
} }
ArrayList<Point> pts;
@Override @Override
public void renderGame(Graphics g) { public void renderGame(Graphics g) {
if (pathfinder == null) if (pathfinder == null)
return; return;
pathfinder.draw(g); pathfinder.draw(g, false);
if (foundPath == null)
return;
g.setColor(Color.red);
for (var pt : foundPath)
g.fillRect(pt.x * SIZE, pt.y * SIZE, SIZE, SIZE);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment