-1

I am programming a little game, like dwarf fortress in C#. I have an array, with objects, these do have a property, indicating wheter the object is passable or not, meaning you have to walk around it. I want now to give the player the oppertunity to say that a dwarf should walk from the Position x,y to x,y. But this dwarf has to find the fastest (or at least one) way to go from the current location to the desired location. I just don't know how to implement this feature, that dwarfs will automatically find a way from their current location to a desired location. Because it could be that they have to pass a long hallway with many curves, and this hallway maybe also goes back and then forward again and after the hallway there is maybe free land, but then it has to pass again a difficult hallway to the desired location. I know it is not described very well, because english is not my mature language and it is really difficult to explain what I mean. But just ask me if you want some more information, i will do my best.

So now again summarized: I want to have a method which finds a way from one location to another, no matter how strong the way ist, even if it is a maze, it has to do it. I mean, this i programmed in most of the games, like settlers and dwarf fortress etc.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Terajoel
  • 61
  • 7
  • 3
    You want a pathfinding algorithm. I suggest trying the A* ("A star") algorithm. [Wikipedia](http://en.wikipedia.org/wiki/A*_search_algorithm) – Blorgbeard May 18 '15 at 20:52
  • Oh yes that seems to be right the algorithm I was searching for, thank you, I will check that @Blorgbeard – Terajoel May 18 '15 at 21:08
  • Is guaranteed that there is a way from a to b? I suggest to use genetic algorithms or at least algorithm x. – Mare Infinitus May 18 '15 at 21:41
  • @MareInfinitus A genetic algorithm to find a path in a hard real time system? Thats a terrible suggestion. Genetic algorithms are meant to run over the course of many minutes (or longer, given the problem size). – Jeremy Barnes May 18 '15 at 21:57
  • Where is there some hint on "real time system"? – Mare Infinitus May 18 '15 at 22:01

2 Answers2

1

I would treat this array as a graph and traverse it using Depth-first search or Breadth-first search algorithm.

Here are also couple hints on using DFS with 2D array

Community
  • 1
  • 1
Pajdziu
  • 911
  • 1
  • 9
  • 22
1

The Answer is to use the A* (A Star) Algorithm

Terajoel
  • 61
  • 7