Commit 48840ff9 authored by lordbenedikt's avatar lordbenedikt
Browse files

Graphen traversieren Demo erstellt

parent 99af70ab
class Button {
String label;
float w;
float h;
float x;
float y;
Button(float x, float y, float w, float h, String label) {
this.label = label;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
}
void drawThis() {
press();
button(x, y, w, h, algorithm.equals(label), 2);
fill(0);
textAlign(CENTER, CENTER);
text(label, x+w/2, y+h/2);
}
void press() {
if (mouseWasPressed==0) {
if (insideRect(mouseX, mouseY, x, y, x+w, y+h)) {
algorithm = label;
}
}
}
}
class Graph {
Node start;
int count;
int speed = 5;
ArrayList<Node> nodes = new ArrayList<Node>();
ArrayList<Node> todo = new ArrayList<Node>();
ArrayList<Node> visited = new ArrayList<Node>();
void bfs() {
for (Node n : g.nodes) {
n.col = color(255);
}
todo.clear();
visited.clear();
todo.add(start);
bfsRek();
}
void bfsRek() {
for (Node n : visited) {
n.col = color(0, 255, 0);
}
if (todo.size()==0) {
for (Node n : g.nodes) {
if (visited.contains(n)) {
n.col = color(255, 255, 100);
} else {
n.col = color(255);
}
}
}
int size = todo.size();
for (int i = 0; i<size; i++) {
Node cur = todo.get(0);
if(visited.contains(cur)) {
continue;
}
cur.col = color(255, 0, 0);
todo.remove(cur);
visited.add(cur);
for (Node n : cur.connectedTo) {
if (!visited.contains(n) && !todo.contains(n)) {
todo.add(n);
}
}
}
}
void addNode(Node node) {
if (nodes.size()==0) {
start = node;
}
nodes.add(node);
}
void removeNode(Node node) {
for (Node n : node.connectedTo) {
if (n.connectedTo.contains(node)) {
n.connectedTo.remove(node);
}
}
nodes.remove(node);
}
void drawThis() {
if (count>0) {
count--;
}
if (count==0) {
count = 100/speed;
bfsRek();
}
stroke(0);
strokeWeight(1);
for (Node n : nodes) {
n.drawThis();
}
}
ArrayList<Node> selectedNodes() {
ArrayList<Node> selectedNodes = new ArrayList<Node>();
for (Node n : nodes) {
if (n.selected) {
selectedNodes.add(n);
}
}
return selectedNodes;
}
void connect(char a, char b, boolean bidirectional) {
Node nodeA = null;
Node nodeB = null;
for (Node n : nodes) {
if (n.name == a) {
nodeA = n;
}
if (n.name == b) {
nodeB = n;
}
}
if (nodeA!=null & nodeB!=null) {
nodeA.connectedTo.add(nodeB);
if(bidirectional) {
nodeB.connectedTo.add(nodeA);
}
}
}
}
Graph g = new Graph();
boolean directional = false;
String algorithm = "BFS";
Button[] buttons = new Button[2];
void setup() {
size(800, 600);
// Setup Buttons
buttons[0] = new Button(width-200, height-50, 80, 40, "BFS");
buttons[1] = new Button(width-100, height-50, 80, 40, "DFS");
// Setup Graph
g.addNode(new Node(200, 300, 'A'));
g.addNode(new Node(300, 300, 'B'));
g.addNode(new Node(400, 300, 'C'));
g.addNode(new Node(300, 200, 'D'));
g.addNode(new Node(300, 400, 'E'));
g.addNode(new Node(450, 450, 'F'));
g.addNode(new Node(500, 220, 'G'));
g.addNode(new Node(430, 140, 'H'));
g.addNode(new Node(130, 200, 'I'));
g.addNode(new Node(130, 430, 'J'));
g.addNode(new Node(80, 320, 'K'));
g.addNode(new Node(80, 40, 'L'));
g.addNode(new Node(600, 150, 'M'));
g.connect('A', 'B', true);
g.connect('B', 'C', true);
g.connect('B', 'D', true);
g.connect('B', 'E', true);
g.connect('E', 'F', true);
g.connect('F', 'G', true);
g.connect('G', 'H', true);
g.connect('H', 'B', true);
g.connect('A', 'I', false);
g.connect('E', 'J', true);
g.connect('I', 'K', false);
g.connect('K', 'J', false);
g.connect('I', 'L', false);
g.connect('M', 'G', false);
}
void draw() {
background(255);
handleInput();
g.drawThis();
drawRubberBand();
drawButtons();
showInfo();
resetInputVariables();
}
void showInfo() {
fill(0);
textAlign(LEFT, TOP);
if (g.selectedNodes().size()>0) {
Node cur = g.selectedNodes().get(0);
textAlign(LEFT, TOP);
text("Points to: ", 10, 10);
for (int i = 0; i< cur.connectedTo.size(); i++) {
text(cur.connectedTo.get(i).name, 110+i*15, 10);
}
}
text("Directial: " + (directional ? "Yes" : "No"), width-310, 10);
text("Speed: " + g.speed, width-110, 10);
}
void drawButtons() {
for (Button b : buttons) {
b.drawThis();
}
}
ArrayList<Integer> pressedKeys = new ArrayList<Integer>();
PVector prevMouse = new PVector(0, 0);
int mouseWasPressed = -1;
int mouseWasReleased = -1;
void mousePressed() {
if (mouseButton==LEFT) {
mouseWasPressed = 0;
}
if (mouseButton==RIGHT) {
mouseWasPressed = 1;
}
if (mouseButton==CENTER) {
mouseWasPressed = 2;
}
}
void mouseReleased() {
if (mouseButton==LEFT) {
mouseWasReleased = 0;
}
if (mouseButton==RIGHT) {
mouseWasReleased = 1;
}
if (mouseButton==CENTER) {
mouseWasReleased = 2;
}
}
void keyPressed() {
if (!pressedKeys.contains(keyCode)) {
pressedKeys.add(keyCode);
}
if (keyCode=='S') {
if (keyIsDown(CONTROL)) {
saveToFile();
} else {
if (g.selectedNodes().size()>0) {
g.start = g.selectedNodes().get(0);
}
if (algorithm.equals("BFS")) {
g.bfs();
} else if(algorithm.equals("DFS")) {
//g.dfs();
}
}
}
if (keyCode=='L') {
if (keyIsDown(CONTROL)) {
loadFile("Graph.txt");
}
}
if (keyCode==DELETE) {
for (Node n : g.selectedNodes()) {
g.removeNode(n);
}
}
if (key=='+') {
g.speed = constrain(g.speed + 1, 1, 10);
}
if (key=='-') {
g.speed = constrain(g.speed - 1, 1, 10);
}
}
void keyReleased() {
if (pressedKeys.contains(keyCode)) {
pressedKeys.remove((Integer)keyCode);
}
}
boolean keyIsDown(int code) {
return pressedKeys.contains(code);
}
boolean keyIsDown(char c) {
return pressedKeys.contains((int)Character.toUpperCase(c));
}
void handleInput() {
// Add Node
if (keyIsDown(ALT) && mouseWasPressed==0) {
char name = (char)(65+g.nodes.size());
g.addNode(new Node(mouseX, mouseY, name, g.selectedNodes()));
}
// Switch to directional edges and back
if (mouseWasPressed==2) {
directional = !directional;
}
// Rubber band
if (mouseWasPressed==0) {
rubberBand = new PVector(mouseX, mouseY);
}
if (mouseWasReleased==0) {
if (dist(mouseX, mouseY, rubberBand.x, rubberBand.y)>30) {
for (Node n : g.nodes) {
if ((n.x<mouseX && n.x<rubberBand.x) || (n.x>mouseX && n.x>rubberBand.x) ||
(n.y<mouseY && n.y<rubberBand.y) || (n.y>mouseY && n.y>rubberBand.y)) {
n.selected = false;
} else {
n.selected = true;
}
}
}
rubberBand = null;
}
}
void resetInputVariables() {
mouseWasPressed = -1;
mouseWasReleased = -1;
// Remember previous mouse position
prevMouse.x = mouseX;
prevMouse.y = mouseY;
}
PVector rubberBand = null;
void drawRubberBand() {
if (rubberBand!=null) {
stroke(0, 120);
fill(0, 30);
rect(rubberBand.x, rubberBand.y, mouseX-rubberBand.x, mouseY-rubberBand.y);
stroke(0);
}
}
boolean insideRect(float _x, float _y, float x1, float y1, float x2, float y2) {
boolean outside = (_x<x1 && _x<x2) || (_x>x1 && _x>x2) || (_y<y1 && _y<y2) || (_y>y1 && _y>y2);
return !outside;
}
void button(float x, float y, float w, float h, boolean isDown, float thickness) {
if (!isDown) {
noStroke();
fill(100);
rect(x+thickness/2, y+thickness/2, w-thickness/2-1, h-thickness/2-1);
strokeWeight(thickness);
stroke(0);
line(x+thickness/2+1, y+h-thickness/2, x+w-thickness/2-1, y+h-thickness/2);
line(x+w-thickness/2, y+thickness/2+1, x+w-thickness/2, y+h-thickness/2-1);
stroke(200);
line(x+thickness/2+1, y+thickness/2, x+w-thickness/2-1, y+thickness/2);
line(x+thickness/2-1, y+thickness/2+1, x+thickness/2-1, y+h-thickness/2-1);
} else {
noStroke();
fill(80);
rect(x+1, y+1, w-2, h-2);
strokeWeight(thickness);
stroke(150);
line(x+1, y+h-1, x+w-2, y+h-1);
line(x+w-1, y+1, x+w-1, y+h-2);
stroke(0);
line(x+1, y, x+w-2, y);
line(x, y+1, x, y+h-2);
}
}
class Node {
float x;
float y;
float r;
color col = color(255);
char name;
boolean selected;
ArrayList<Node> connectedTo = new ArrayList<Node>();
Node(float x, float y, char name) {
this.x = x;
this.y = y;
this.r = 15;
this.name = name;
}
Node(float x, float y, char name, Node ... nodes) {
this(x, y, name);
for (Node n : nodes) {
if (!directional) {
connectedTo.add(n);
}
n.connectedTo.add(this);
}
}
Node(float x, float y, char name, ArrayList<Node> nodes) {
this(x, y, name);
for (Node n : nodes) {
if (!directional) {
connectedTo.add(n);
}
n.connectedTo.add(this);
}
}
void drawThis() {
// Select
if (mouseWasPressed==0) {
if (!selected) {
if (dist(x, y, mouseX, mouseY)<r) {
selected = true;
} else {
selected = false;
}
} else {
if (keyIsDown(SHIFT)) {
if (dist(x, y, mouseX, mouseY)<r) {
selected = false;
}
} else {
selected = false;
}
}
}
// Connect
if (mouseWasPressed==1) {
if (dist(x, y, mouseX, mouseY)<r) {
for (Node n : g.selectedNodes()) {
if (!n.connectedTo.contains(this)) {
n.connectedTo.add(this);
} else {
n.connectedTo.remove(this);
}
}
}
}
// Update Position
if (selected==true && keyIsDown(CONTROL)) {
x += mouseX - prevMouse.x;
y += mouseY - prevMouse.y;
}
// Draw Node
fill(selected ? color(255, 150, 0) : col);
circle(x, y, r*2);
fill(0);
textSize(20);
textAlign(CENTER, CENTER);
text(name, x+0.5f, y-3);
// Draw Edges
fill(0);
for (Node n : connectedTo) {
// Draw Edge only if nodes aren't touching
float distance = dist(x, y, n.x, n.y);
if (distance > r*2) {
float angle = PVector.angleBetween(new PVector(0, 1), new PVector(n.x-x, n.y-y));
if (n.x-x > 0) {
angle = TWO_PI - angle;
}
float _x = 0;
if (n.connectedTo.contains(this)) {
_x = -3;
}
pushMatrix();
translate(x, y);
rotate(angle);
float l = distance - r - n.r;
line(_x, r, _x, l+r);
line(_x-6, l+r-6, _x, l+r);
line(_x+6, l+r-6, _x, l+r);
popMatrix();
}
// Draw Edge to self
if (n == this) {
fill(0, 0);
arc(x, y-11, 20, 100, PI, TWO_PI);
line(x-10, y-11, x-10-6, y-11-6);
line(x-10, y-11, x-10+6, y-11-6);
}
}
}
}
import java.io.*;
void saveToFile() {
saveClear("Graph.txt");
for (Node n : g.nodes) {
saveAdd("Graph.txt", n.name + " (" + n.x + "," + n.y + ") ->");
for (Node subNode : n.connectedTo) {
saveAdd("Graph.txt", " " + subNode.name);
}
saveAdd("Graph.txt", "\n");
}
}
void saveClear(String name) {
try {
FileWriter writer = new FileWriter(sketchPath("")+name, false);
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
void saveAdd(String name, String content) {
try {
FileWriter writer = new FileWriter(sketchPath("")+name, true);
writer.write(content);
writer.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
void loadFile(String name) {
try {
FileReader reader = new FileReader(sketchPath("")+name);
BufferedReader bufferedReader = new BufferedReader(reader);
g.nodes.clear();
ArrayList<String> lines = new ArrayList<String>();
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
for (String l : lines) {
String[] sides = l.split(" -> ");
String[] nameCoords = sides[0].split(" ");
String coords[] = nameCoords[1].substring(1, nameCoords[1].length()-1).split(",");
float x = parseFloat(coords[0]);
float y = parseFloat(coords[1]);
Node newNode = new Node(x, y, nameCoords[0].charAt(0));
g.addNode(newNode);
}
for (String l : lines) {
String[] sides = l.split(" -> ");
if (sides.length>1) {
String[] pointsTo = sides[1].split(" ");
for (String s : pointsTo) {
g.connect(sides[0].split(" ")[0].charAt(0), s.charAt(0), false);
}
}
}
System.out.println("Loaded "+name);
reader.close();
}
catch (IOException e) {
System.out.println("File not Found!");
//e.printStackTrace();
}
}
A (327.0,302.0) -> B D E F
B (512.0,294.0) -> C G
C (418.0,119.0) -> A
D (138.0,404.0) -> A E F
E (329.0,519.0) -> D A
F (137.0,214.0) -> D A
G (661.0,301.0) -> H
H (603.0,429.0) -> I
I (713.0,429.0) -> G
import java.util.Collections;
ArrayList<Card> hands[];
ArrayList<Card> dealerHand = new ArrayList<Card>();
ArrayList<Card> deck = new ArrayList<Card>();
ArrayList<Card> discardPile = new ArrayList<Card>();
int currentPlayer;
int playerStates[];
int playerCount;
int totalCards;
boolean gameOver = false;
void setup() {
size(800, 600);
setCardNames();
setStates();
reset(1);
}