Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 1 of 1

Thread: Java Newbie: How to Code Node and its Neighbors?

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Thanked 0 Times in 0 Posts

    Default Java Newbie: How to Code Node and its Neighbors?

    Hi guys

    I'm new to programming and as a School task i need to implement BFS,DFS and A* in java.

    in short i'm given a grid of say 4x4, with a given start and goal position and i need to go through each node/tile in the grid using the above algorithms til the goal is found.

    to begin with i don't know how to code the neighbors of a given node. For example tile 1 in grid as 2 and 9 as neighbors but i'm struggling to code that. I need the neighbours part so that i can move from start position to other parts of gird legally by moving horizontally or vertically.

    my node class is:

    class node {
        int value;
       LinkedList<node> neighbors;
       bool expanded;

    Simply i can't visualize how the above node class will work and i've been looking at it for hours.

    let's say i'm given a 8x8 grid right, So if i start the program with a grid of size 8x8 right :

    1 - my main will func will create an array of nodes using
     node[8][8] = new node.....
    2 - i then have to initialize each tile in the grid to represents its value of 1,2,3,4...64 by looping through all of the tiles(in case of 8x8 gird).
    3 - somehow i need to add the neighbors of each tile, for example tile 1 in grid has tiles 2,9 as neighbours, I can't just do it by manually and hard code it for each node because what if there is 16x16 grid or much larger grid , it'll take forever. I just don't know how to go about so if anyone can give me some details i would really appreciate it.

    Thank you
    Last edited by ke3pup; April 20th, 2010 at 01:03 AM.

Similar Threads

  1. TreeNode vs. Node
    By Kumarrrr in forum Java Theory & Questions
    Replies: 1
    Last Post: March 27th, 2010, 06:06 AM
  2. java newbie..simple mail server implementation
    By saurabh4dudes in forum Java Networking
    Replies: 0
    Last Post: March 12th, 2010, 08:53 AM
  3. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  4. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM
  5. Xml-Node Retrieval
    By prasb in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: December 4th, 2009, 12:44 PM