I’m trying to make an small tower defender game in Java. I have a grid that is made up of a Point2D.Double array named:
FieldArr[h][v]
h represents horizontal fields, v the vertical vertical fields
this makes a grid like this
+ + + + + + +
S + X + + + +
+ + + X + + +
+ X + + + + F
+ + X + + + +
S represents start, F represents Finish, X represents Towers
now I want to calculate the shortest route for but i don’t have any clue how to start on this
Towers have the following vars for location:
HorizontalNr and VerticalNr.
for paint I do then:
public void paint(Graphics2D g2) {
int Xpos = HorizontalNr * playfield.getSquarewidth() + playfield.GetinitialXPos();
int Ypos = VerticalNr * playfield.getSquarewidth() + playfield.GetinitialYPos();
g2.fillRect(Xpos, Ypos, 50, 50);
}
Anyone have any tips, on how I should make my enemy class, so I won’t get in any problem with the algorithm?
and/or have tips on how to calculate the shortest path?
already thanks
grt kiwi
As Mark said, this is Shortest Path Problem, which can be solved easily and efficiently.
Note, that since your problem is not weighted, you can use a BFS here, which is pretty easy to implement and also guarantees finding sortest path for unweighted graphs.
Pseudo code for BFS:
(*) Extracting the path from the map is simple: just follow the
current <- parent.get(current)until you getnull, this way you extract the exact path you will use.Note that even faster solution [in most cases] will be A* with the manhattan distance heuristic, but it is much more complex to implement it.