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:
Code :
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
Code :
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