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

-1-

parent 509e8d3e
No related branches found
No related tags found
No related merge requests found
package algorithm;
import parallel.EagerWorkingThread;
import parallel.SharedRessources;
import structs.Vec3;
public class Explorer {
public enum Direction {
North,
East,
South,
West
}
private int x;
private int y;
private int steps;
private Direction direction;
public Explorer(int x, int y, int steps, Direction direction) {
this.x = x;
this.y = y;
this.steps = steps;
this.direction = direction;
}
public void explore() {
int currentStepsOnField
=
(int) SharedRessources.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]
.getZ();
if(currentStepsOnField < steps)
return;
boolean wall =
SharedRessources.getInstance().getWallMatrix()[x + y * SharedRessources.getInstance().getWidth()];
if (wall)
return;
Explorer recursiveNext
= null;
switch (direction) {
case North:
if (steps < currentStepsOnField) {
SharedRessources
.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]
= new Vec3(0, +1, steps);
}
recursiveNext
= new Explorer(x, y - 1, steps++, direction);
addLeftRight();
break;
case East:
if (steps < currentStepsOnField) {
SharedRessources
.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]
= new Vec3(-1, 0, steps);
}
recursiveNext
= new Explorer(x + 1, y, steps++, direction);
addUpDown();
break;
case South:
if (steps < currentStepsOnField) {
SharedRessources
.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]
= new Vec3(0, -1, steps);
}
recursiveNext
= new Explorer(x, y + 1, steps++, direction);
addLeftRight();
break;
case West:
if (steps < currentStepsOnField) {
SharedRessources
.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()]
= new Vec3(+1, 0, steps);
}
recursiveNext
= new Explorer(x - 1, y, steps++, direction);
addUpDown();
break;
}
recursiveNext.explore();
}
private void addLeftRight() {
SharedRessources
.getInstance().add(new Explorer(x - 1, y, steps++, Direction.West));
SharedRessources
.getInstance().add(new Explorer(x + 1, y, steps++, Direction.East));
}
private void addUpDown() {
SharedRessources
.getInstance().add(new Explorer(x, y - 1, steps++, Direction.North));
SharedRessources
.getInstance().add(new Explorer(x, y + 1, steps++, Direction.South));
}
}
package algorithm;
import parallel.EagerWorkingThread;
import parallel.SharedRessources;
import structs.Vec3;
import java.awt.*;
import java.util.ArrayList;
public class ParallelPathfinder {
private void flood(int destX, int destY) {
SharedRessources
.getInstance().add(new Explorer(destX - 1, destY, 1, Explorer.Direction.West));
SharedRessources
.getInstance().add(new Explorer(destX + 1, destY, 1, Explorer.Direction.East));
SharedRessources
.getInstance().add(new Explorer(destX, destY - 1, 1, Explorer.Direction.North));
SharedRessources
.getInstance().add(new Explorer(destX, destY + 1, 1, Explorer.Direction.South));
while (!SharedRessources.getInstance().done());
}
public ArrayList<Point> calculatePath(int startX, int startY, int destX, int destY) {
flood(destX, destY);
ArrayList<Point> path
= new ArrayList<>();
int x = startX;
int y = startY;
while (x != destX || y != destY) {
path.add(new Point(x, y));
Vec3 currentDir
=
SharedRessources.getInstance().getVisitMatrix()[x + y * SharedRessources.getInstance().getWidth()];
int dx = (int) currentDir.getX();
int dy = (int) currentDir.getY();
if(dx == 0 && dy == 0)
return null;
x += dx;
y += dy;
}
return path;
}
public void draw(Graphics g) {
for (int j = 0; j < SharedRessources.getInstance().getHeight(); j++) {
for (int i = 0; i < SharedRessources.getInstance().getWidth(); i++) {
boolean b =
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();
Color color
= new Color(0, green, 0);
g.setColor(color);
}
}
g.fillRect(i * 10, j * 10, 10, 10);
}
}
}
}
package algorithm;
import parallel.EagerWorkingThread;
import parallel.SharedRessources;
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.LinkedBlockingQueue;
public class WorkingExplorers extends EagerWorkingThread {
public WorkingExplorers() {
super();
this.setAllowRunning(true);
System.out.println("[*] Thread initialized @" + this.getName());
}
private Stack<Explorer> currentWaitingExplorers = new Stack<>();
@Override
public void onBegin() {
}
@Override
public void onTrigger() {
if (!currentWaitingExplorers.isEmpty())
currentWaitingExplorers.pop().explore();
}
@Override
public void onEnd() {
}
public void add(Explorer explorer) throws InterruptedException {
this.currentWaitingExplorers.push(explorer);
this.trigger();
}
}
package parallel;
public abstract class EagerWorkingThread extends Thread {
private static volatile boolean RUN_THREADS = true;
public static void stopAllThreads() {
RUN_THREADS = false;
}
private volatile int triggerCount;
private volatile boolean isTriggered;
private volatile boolean allowRunning;
public void setAllowRunning(boolean allowRunning) {
this.allowRunning = allowRunning;
}
public boolean isAllowedRunning() {
return allowRunning;
}
@Override
public void run() {
while (RUN_THREADS && allowRunning) {
synchronized (this) {
while (!isTriggered) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
onBegin();
for (int i = 0; i < triggerCount; i++)
onTrigger();
onEnd();
triggerCount = 0;
isTriggered = false;
}
}
public synchronized boolean trigger() {
triggerCount++;
isTriggered = true;
this.notifyAll();
return true;
}
public synchronized boolean isReady(){
return this.triggerCount == 0;
}
public abstract void onBegin();
public abstract void onTrigger();
public abstract void onEnd();
}
package parallel;
import structs.Vec3;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
public class MapLoader {
private int width;
private int height;
private MapLoader() {
}
private static MapLoader mapLoader;
public static MapLoader getDefault() {
if (mapLoader == null)
mapLoader = new MapLoader();
return mapLoader;
}
public void load(BufferedImage src) {
this.width = src.getWidth();
this.height = src.getHeight();
SharedRessources.getInstance().setWidth(width);
SharedRessources.getInstance().setHeight(height);
SharedRessources.getInstance().setVisitMatrix(new Vec3[width * height]);
SharedRessources.getInstance().setWallMatrix(new boolean[width * height]);
for (int i = 0; i < SharedRessources.getInstance().getVisitMatrix().length; i++)
SharedRessources.getInstance().getVisitMatrix()[i] = new Vec3(0, 0, 10000);
// https://stackoverflow.com/a/9470843/14727115
byte[] pixels = ((DataBufferByte) src.getRaster().getDataBuffer()).getData();
for (int j = 0; j < height; j++) {
for (int i = 0; i < width; i++) {
SharedRessources.getInstance().getWallMatrix()[i + j * width] =
((BufferedImage) src).getRGB(i, j) == Color.white.getRGB();
}
}
}
}
package parallel;
import algorithm.Explorer;
import algorithm.WorkingExplorers;
import structs.Vec3;
public class SharedRessources {
private static int USING_CORES;
/***
* Use Integer.MAX_VALUE for all cores
* @param cores
*/
public static void setMaximumCores(int cores) {
USING_CORES = cores;
}
private static SharedRessources sharedRessources;
public static synchronized SharedRessources getInstance() {
if (sharedRessources == null)
sharedRessources = new SharedRessources(USING_CORES);
return sharedRessources;
}
private int maxCores;
private int currentCore;
private WorkingExplorers[] workingThreads;
public void start() {
for (int i = 0; i < maxCores; i++)
this.workingThreads[i].start();
}
public void add(Explorer explorer) {
try {
this.workingThreads[(currentCore++) % maxCores].add(explorer);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public boolean done() {
for (int i = 0; i < maxCores; i++)
if (!this.workingThreads[i].isReady())
return false;
return true;
}
private SharedRessources(int cores) {
if (cores >= Runtime.getRuntime().availableProcessors())
maxCores = Runtime.getRuntime().availableProcessors();
else maxCores = cores;
System.out.println("[*] Using " + maxCores + " cores.");
workingThreads = new WorkingExplorers[maxCores];
for (int i = 0; i < maxCores; i++)
workingThreads[i] = new WorkingExplorers();
start();
currentCore = 0;
}
private int width;
private int height;
private boolean[] wallMatrix;
private Vec3[] visitMatrix;
public Vec3[] getVisitMatrix() {
return visitMatrix;
}
public boolean[] getWallMatrix() {
return wallMatrix;
}
protected void setVisitMatrix(Vec3[] visitMatrix) {
this.visitMatrix = visitMatrix;
}
protected void setWallMatrix(boolean[] wallMatrix) {
this.wallMatrix = wallMatrix;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
protected void setWidth(int width) {
this.width = width;
}
protected void setHeight(int height) {
this.height = height;
}
}
package structs;
public class Vec3 {
private float x;
private float y;
private float z;
public Vec3(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}
public void setX(float x) {
this.x = x;
}
public void setY(float y) {
this.y = y;
}
public void setZ(float z) {
this.z = z;
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
}
package window;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public abstract class Game extends JFrame {
private JPanel drawingSurface;
private Thread windowThread;
private boolean running;
protected boolean[] keys;
public Game(String title, int w, int h) {
super(title);
this.setSize(w, h);
keys = new boolean[1024];
this.windowThread = new Thread(() -> {
loadGame();
while (running) {
updateGame();
this.drawingSurface.repaint();
}
});
this.drawingSurface
= new JPanel() {
@Override
public void paint(Graphics g) {
super.paint(g);
renderGame(g);
}
};
this.drawingSurface.setSize(this.getSize());
this.add(this.drawingSurface);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
keys[e.getKeyCode()] = true;
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
keys[e.getKeyCode()] = false;
}
});
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public final void run() {
this.setVisible(true);
this.running = true;
this.windowThread
.start();
}
public abstract void loadGame();
public abstract void updateGame();
public abstract void renderGame(Graphics g);
}
package window;
import algorithm.ParallelPathfinder;
import parallel.MapLoader;
import parallel.SharedRessources;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
public class MainWindow extends Game {
public MainWindow(String title, int w, int h) {
super(title, w, h);
}
private ParallelPathfinder parallelPathfinder;
private ArrayList<Point> pts;
@Override
public void loadGame() {
SharedRessources.setMaximumCores(Integer.MAX_VALUE);
try {
MapLoader.getDefault().load(ImageIO.read(new File("c:/users/enesh/desktop/t.png")));
} catch (IOException e) {
e.printStackTrace();
}
parallelPathfinder
= new ParallelPathfinder();
}
@Override
public void updateGame() {
if (keys[KeyEvent.VK_ENTER])
pts = parallelPathfinder.calculatePath(70, 26, 23, 24);
}
@Override
public void renderGame(Graphics g) {
if (parallelPathfinder == null)
return;
parallelPathfinder.draw(g);
if (pts == null)
return;
g.setColor(Color.red);
for (var pt : pts) {
g.fillRect(pt.x * 10, pt.y * 10, 10, 10);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment