<repo_name>Freggan/Portfolio-3<file_sep>/AStarGraph.java
package sample;
import java.util.ArrayList;
import java.util.PriorityQueue;

import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;

public class AStarGraph {
    private ArrayList<Vertex> vertices;
    public AStarGraph() {
        vertices=new ArrayList<Vertex>();
    }
    public void addvertex(Vertex v) {
        vertices.add(v);
    }
    public void newconnection(Vertex v1, Vertex v2, Double dist) {
        v1.addOutEdge(v2,dist);
        v2.addOutEdge(v1,dist);
    }
    public boolean A_Star(Vertex start, Vertex destination,Boolean Manhat)
    {   if (start==null || destination==null)
        return false;
        PriorityQueue<Vertex> Openlist = new PriorityQueue<Vertex>();
        ArrayList<Vertex> Closedlist = new ArrayList<Vertex>();
        Openlist.offer(start);
        Vertex Current;
        ArrayList<Vertex> CurrentNeighbors;
        ArrayList<Double> CurrentNeighborsDistance;
        Vertex Neighbor;
        //Initialize h with chosen heuristic
        if(Manhat) {
            for (int i = 0; i < vertices.size(); i++) {
                vertices.get(i).seth(Manhattan(vertices.get(i), destination));
            }
        }
        else {
            for (int i = 0; i < vertices.size(); i++) {
                vertices.get(i).seth(Euclidean(vertices.get(i), destination));
            }
        }
        start.setg(0.0);
        start.calculatef();
        //Start algorithm
        System.out.println("Start Algorithm");

        Current=start;
        while (Current.getid()!=destination.getid())
        {
            CurrentNeighbors=Current.getNeighbours();
            CurrentNeighborsDistance=Current.getNeighbourDistance();

            for (int i=0;i<CurrentNeighbors.size();i++)
            {
                Neighbor=CurrentNeighbors.get(i);

                if ((!Closedlist.contains(Neighbor)) && (!Openlist.contains(Neighbor)))
                {
                    Neighbor.setg(Current.getg() + CurrentNeighborsDistance.get(i));
                    Openlist.offer(Neighbor);

                }

                if((Current.getg()+CurrentNeighborsDistance.get(i)+Neighbor.geth()) < Neighbor.getf())
                {
                    Neighbor.setg(Current.getg() + CurrentNeighborsDistance.get(i));
                    Neighbor.calculatef();
                    Neighbor.setPrev(Current);
                }

            }
            if (!Openlist.isEmpty())
            {
                Closedlist.add(Current);
                Current = Openlist.poll();
            }
            else{return false;}

        }
        return true;
    }
    public Double Manhattan(Vertex from,Vertex goal){
        return Double.valueOf(abs(from.getx() - goal.getx()) + abs(from.gety() - goal.gety()));

    }
    public Double Euclidean(Vertex from,Vertex to){
        return (sqrt ( pow(from.getx()-to.getx(),2) + pow(from.gety()-to.gety(),2)));
    }
}

class Vertex implements Comparable<Vertex>{
    private String id;
    private ArrayList<Vertex> Neighbours=new ArrayList<Vertex>();
    private ArrayList<Double> NeighbourDistance =new ArrayList<Double>();
    private Double f;
    private Double g;
    private Double h;
    private Integer x;
    private Integer y;
    private Vertex prev =null;
    public Vertex(String id, int x_cor,int y_cor){
        this.id=id;
        this.x=x_cor;
        this.y = y_cor;
        f=Double.POSITIVE_INFINITY;
        g=Double.POSITIVE_INFINITY;
        h=0.0;
    }
    public void addOutEdge(Vertex toV, Double dist){
        Neighbours.add(toV);
        NeighbourDistance.add(dist);
    }
    public ArrayList<Vertex> getNeighbours(){
        return Neighbours;
    }
    public ArrayList<Double> getNeighbourDistance(){
        return NeighbourDistance;
    }
    public String getid(){ return id;};
    public Integer getx(){ return x; }
    public Integer gety(){return x; }
    public Double getf() { return f; }
    public void calculatef(){ f=g+h; }

    public Double getg() { return g; }

    public void setg(Double newg){ g=newg; }
    public Double geth(){ return h; }
    public void seth(Double estimate){ h=estimate; }
    public Vertex getPrev() { return prev; }
    public void setPrev(Vertex v){prev=v;}
    public void printVertex(){
        System.out.println(id + " g: "+g+ ", h: "+h+", f: "+f);
    }
    @Override
    public int compareTo(Vertex o) {
        if (o.getf()==f){return 0;}
        if (o.getf()<f){return 1;}
        else{return -1;}
    }
}
<file_sep>/Controller.java
package sample;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.text.*;
import java.io.*;
import java.util.Stack;

public class Controller {

    @FXML
    ChoiceBox OriginPick = new ChoiceBox();

    @FXML
    ChoiceBox DestinationPick = new ChoiceBox();

    @FXML
    ChoiceBox HeuristicPick = new ChoiceBox();

    @FXML
    Button Run = new Button();

    @FXML
    TextArea Results = new TextArea();

    public void buttonPressRun(ActionEvent event) {
        Boolean Manhat;
        if(HeuristicPick.getValue() == "Manhattan") {
            Manhat = true;
        }
        else {
            Manhat = false;
        }
        AStarGraph MyMaze = new AStarGraph();

        Vertex x=new Vertex("x",0,0); Vertex y=new Vertex("y",0,0);
        Vertex A=new Vertex("A",0,4); Vertex B=new Vertex("B",1,7);
        Vertex C=new Vertex("C",4,0); Vertex D=new Vertex("D",3,7);
        Vertex E=new Vertex("E",3,3); Vertex F=new Vertex("F",6,6);
        Vertex G=new Vertex("G",7,2); Vertex H=new Vertex("H",8,7);
        Vertex I=new Vertex("I",9,2); Vertex J=new Vertex("J",11,5);

        MyMaze.addvertex(A); MyMaze.addvertex(B);
        MyMaze.addvertex(C); MyMaze.addvertex(D);
        MyMaze.addvertex(E); MyMaze.addvertex(F);
        MyMaze.addvertex(G); MyMaze.addvertex(H);
        MyMaze.addvertex(I); MyMaze.addvertex(J);

        MyMaze.newconnection(A,B,3.41); MyMaze.newconnection(A,C,6.82);
        MyMaze.newconnection(B,D,2.00); MyMaze.newconnection(C,G,4.41);
        MyMaze.newconnection(C,I,4.82); MyMaze.newconnection(D,E,4.00);
        MyMaze.newconnection(E,F,6.23); MyMaze.newconnection(F,G,4.41);
        MyMaze.newconnection(F,H,3.82); MyMaze.newconnection(G,H,5.41);
        MyMaze.newconnection(G,I,2.82); MyMaze.newconnection(H,J,4.41);
        MyMaze.newconnection(I,J,3.82);

        if(OriginPick.getValue().equals("A")) {
            x = A;
        } else if(OriginPick.getValue().equals("B")) {
            x = B;
        } else if(OriginPick.getValue().equals("C")) {
            x = C;
        } else if(OriginPick.getValue().equals("D")) {
            x = D;
        } else if(OriginPick.getValue().equals("E")) {
            x = E;
        } else if(OriginPick.getValue().equals("F")) {
            x = F;
        } else if(OriginPick.getValue().equals("G")) {
            x = G;
        } else if(OriginPick.getValue().equals("H")) {
            x = H;
        } else if(OriginPick.getValue().equals("I")) {
            x = I;
        } else {
            x = J;
        }

        if(DestinationPick.getValue().equals("A")) {
            y = A;
        } else if(DestinationPick.getValue().equals("B")) {
            y = B;
        } else if(DestinationPick.getValue().equals("C")) {
            y = C;
        } else if(DestinationPick.getValue().equals("D")) {
            y = D;
        } else if(DestinationPick.getValue().equals("E")) {
            y = E;
        } else if(DestinationPick.getValue().equals("F")) {
            y = F;
        } else if(DestinationPick.getValue().equals("G")) {
            y = G;
        } else if(DestinationPick.getValue().equals("H")) {
            y = H;
        } else if(DestinationPick.getValue().equals("I")) {
            y = I;
        } else {
            y = J;
        }

        if(MyMaze.A_Star(x,y,Manhat))
        {
            String message = "Found a path: \n" ;
            Vertex pvertex=y;
            Stack<Vertex> Path = new Stack();
            int limit=0;
            while (pvertex!=null)
            {
                Path.push(pvertex);
                pvertex=pvertex.getPrev();
            }
            if(!Path.isEmpty())
                limit =Path.size();
            for(int i=0;i<limit-1;i++)
                message = message + Path.pop().getid() +" - > ";
            if (limit>0)
                message = message + Path.pop().getid();
                Results.setText(message);

        }
        else
            Results.setText("DID NOT FIND A PATH!!");



    }

}
