How do I know my vertex has a connection?

223 views Asked by At

I am making a Hunt the Wumpus game using the graph data structure. Each vertex in it is an array of size 4 to represent each direction, north south east west. The directions are stored in an enum. When one vertex is connected with another, its adjacency is supposed to change so that it connects with the other vertex (I'm still confused about this- why is my vertex an array of size 4 but only has 1 direction).

When I connect one vertex with another and print out the arraylist of neighbors, I get a strange output. When I print the unconnected vertex I get [null, null, null, null] for neighbors; when I print the connected vertex I get [null, null, null, [null, null, null, null]. I don't know what the neighbors are supposed to be- should they print out as integers representing the direction enum index, or a direction? What should be printing out?

import java.util.ArrayList;
import java.util.Collection;

 enum Direction{
    EAST,
    WEST,
    NORTH,
    SOUTH
}
public class Vertex extends Agent implements Comparable<Vertex>{
    private Vertex[] connections;
    private Direction dir;
    private int cost;
    private boolean marked;


    public Vertex(double x0, double y0){
        super( x0,  y0);
        connections = new Vertex[4];
        this.dir = dir;
        this.cost = cost;
        this.marked = marked;
    }

    public Direction opposite(Direction dir) {
    //that returns the compass opposite of a direction (i.e. South for North...)
        if(dir == Direction.EAST){
            dir = Direction.WEST;
        }
        else if(dir == Direction.WEST){
            dir = Direction.EAST;
        }
        else if(dir == Direction.NORTH){
            dir = Direction.SOUTH;
        }
        else dir = Direction.NORTH;

        return dir;
    }
    void connect(Vertex other, Direction dir){
    //modify the object's adjacency list/map so that it connects with the other Vertex. This is a uni-directional link.
        connections[dir.ordinal()] = other;

        }

    Vertex getNeighbor(Direction dir){
    //returns the Vertex in the specified direction or null.
        return this.connections[dir.ordinal()];
        }

    Collection getNeighbors(){
    //returns a Collection, which could be an ArrayList, of all of the object's neighbors.
        ArrayList<Vertex> neighborList = new ArrayList<Vertex>();
            for (Direction dir: Direction.values()){
                neighborList.add(this.getNeighbor(dir));
            }

            return neighborList;
        }

    public int getCost(){
        return this.cost;
    }

    public int setCost(int c){
        return this.cost = c;
    }

    public boolean getMarked(){
        return this.marked;
    }

    public boolean setMarked(boolean m){
        return this.marked = m;
    }

    public int compareTo(Vertex other){
        return (this.cost - other.cost);
    }

    /*
    * returns a string containing the number of neighbors, the cost, and the marked flag
    */
    public String toString(){
        String s = "";
        s += this.getNeighbors() + "\n" ;
        s += "cost: " + this.cost + "\n";
        s += "marked: " + this.marked;
        return s;

    }

    public static void main (String args[]){
        Vertex newE = new Vertex(0.5, 0.5);
        Vertex newerE = new Vertex(0.123, 1.56);
        newE.connect(newerE, Direction.SOUTH);
        newE.setCost(1);
        newE.setMarked(true);
        System.out.println(newE.toString());
    }

}
1

There are 1 answers

0
AudioBubble On BEST ANSWER

Well, when you get the output [null, null, null, [null, null, null, null]], you know there is a reference to your connected node. So the second array is connected by the first, but not vice versa. Because your code don't connect the second node, but the first is connected.I think you should't print the List of nodes, this will lead to problems.

One case, the Nodes are connected together so you have Parent connected to Child and Child connected to Parent. When you now print this out, you will get a recursive execution.

[null, null, null, child[parent[[null, null, null, child[...]]], null, null, null]]

This will lead to an exception. You could use identifier for this, like a ID. So you just print out the ID for the connected objects, or maybe a position ->

[1,null,null,2]

[(x,y),(x,y),(x,y),(x,y)]

Just use something to identify the nodes, afterwards you can easily see if the node is there or null.

So another point is, that you know that you have 4 Points to connect, so why don't use:

private Node north;
private Node south;
private Node west;
private Node east;

This will be easier for later on, and you don't have to use this enum anymore. And you can also delete the direction in the Vertex class, by the way this is also confusing a Vertex is a point and points don't have a direction :)

Or you just use a 2x2 Grid for this, for example:

private Node[][] 

You can specify the size of the grid, through the positions you know where to access the neighbour. For example the coordinate [1,1] is connected by [0,1],[1,0],[2,1],[1,2]. And you also can print out the whole grid easily.

Isometric Grid Example

You can put this calculation into a algorithm, to check the neighbours. So you don't have to save nodes in every node class. The render Position you can get by accessing this object, for example (just if you want to render it):

node[1][2].getX();

And i also suggest that you have a look on Tree-Structures :)