Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm building an application that implements the A* algorithm to calculate a route between two rooms. I am trying to create a graph which the algorithm can operate on and I am not sure if this is the right way to do it.

Here's what I've done so far:

Vect2.java:

package myalgorithm;

public class Vect2 {

    private int x;
    private int y;


    public Vect2(int x,int y ){

        this.x = x;
        this.y = y;

        }

}

Node.java

package myalgorithm;




public class Node {

    Node parent;
    Vect2 vector;
    public int x;
    public int y;

    public double f;
    public double g;
    public double h;


    public Node( int x,int y,Node parent, double g,double h){

        this.x = x;
        this.y = y;
        this.parent= parent;
        this.g = g;
        this.h = h;
        this.f= this.g + this.h;

    }
}

NodeGraph.java

package myalgorithm;
import java.util.Arrays;

public class NodeGraph {

    Node a = new Node(88,623,null,0,0);
    Node b = new Node(727,627,null,0,0);
    Node c = new Node(723,93,null,0,0);
    Node d = new Node(90,92,null,0,0);
    Node e = new Node(94,349,null,0,0);
    Node f = new Node(397,358,null,0,0);
    Node g = new Node(722,339,null,0,0);
    Node[] arr = new Node[7];


public NodeGraph init(){


    arr[0]= a;
    arr[0]= b;
    arr[0]= c;
    arr[0]= d;
    arr[0]= e;
    arr[0]= f;
    arr[0]= g;

    return this;
}
public void createMatrx(){
boolean[][] matrix = new boolean[7][];
for (int i=0; i<matrix.length; i++) matrix[i] = new boolean[7];

int nodeA = Arrays.binarySearch(arr, a);
int nodeB = Arrays.binarySearch(arr, b);
int nodeC = Arrays.binarySearch(arr, c);
int nodeD = Arrays.binarySearch(arr, d);
int nodeE = Arrays.binarySearch(arr, e);
int nodeF = Arrays.binarySearch(arr, f);
int nodeG = Arrays.binarySearch(arr, g);

matrix[nodeA][nodeB] = true;
matrix[nodeA][nodeE] = true;
matrix[nodeB][nodeA] = true;
matrix[nodeB][nodeG] = true;
// A is connected to D
matrix[nodeC][nodeG] = true;
matrix[nodeC][nodeD] = true;
// B is connected to D
matrix[nodeD][nodeC] = true;
matrix[nodeD][nodeE] = true;
matrix[nodeE][nodeD] = true;
// C is connected to D
matrix[nodeE][nodeF] = true;
matrix[nodeE][nodeA] = true;
matrix[nodeF][nodeE] = true;
matrix[nodeE][nodeG] = true;

matrix[nodeG][nodeF] = true;
matrix[nodeE][nodeB] = true;
matrix[nodeE][nodeC] = true;

matrix[nodeD][nodeC] = true;
}
}
share|improve this question
    
Welcome to Code Review! Does this code work as you intend it to or are you experiencing problems with it? Have you tested the code? –  Simon Forsberg Feb 16 at 12:07
    
@SimonAndréForsberg André Forsberg thank you! I am very new to coding and I' not sure what would be the best way to test it –  new_to_coding Feb 16 at 15:09
    
In general, the best - and only - way to test the code is to use and/or run the code and see if produces the output you are expecting. –  Simon Forsberg Feb 16 at 15:16
    
@SimonAndréForsberg thank you! I will try and see what happens –  new_to_coding Feb 16 at 15:25

1 Answer 1

In general, you have some problems being consisten in your coding.

1) Formatting:

In your NodeGraph class you are not consistent in your indentation. Your createMatrx (which has a typo and should be called createMatrix) you are not using any indentation.

2) Access modifiers:

In your class Node2 you set your members private, as expected.

In your class Node you don't specifically set the access modifier as private for some members, and you set them explicitly publbic for others. I would expect all members to be private.

3) Naming:

Single character variable names are not very descriptive. Name your variables according to their purpose.

share|improve this answer
    
thank you very much! –  new_to_coding Feb 16 at 15:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.