C# class for a 2D grid-based pathfinder algorithm, lovingly named D*, DumbStar, or Dumpster.
Compile and run with printing as a command-line argument, e.g.:
Pathfinder.exe 1
to print, otherwise Pathfinder.exe
will run without terminal printing and simply output a stats.csv
file with the results of the average movement points remaining for the example currently set up in the code.
UNIX systems should use csc
for compilation and mono
to run the executable.
Walls must be edited manually in Pathfinder.cs
.
Currently simply prints the grid in the terminal with each move, where [ ]
is a free space, [X]
is the current position, and [*]
is a wall. Past moves are tracked and avoided to prevent getting stuck in loops.
- added CSV output to record the average number of movement points remaining for the alternate movement methods
- added
m_points
comparison to see ifMove()
reaches the target with fewer moves on average thanMove_alt()
(higher number means more moves remaining, i.e. fewer moves used to reach target) - now tracking past moves with a
HashSet
to prevent looping, rather than placing a wall at previous location - added
Move_alt()
for runtime analysis
- switch to using Tuples for all coordinates
- create graphical representation based on Godot's A* demo
There are two methods for finding the optimal path to the target position, Move()
and Move_alt()
. The former uses a dictionary to store all possible moves and sorts the keys to determine the best move, and the latter simply stores the best distance as a double
and compares with each free neighbor. The intention is to perform a runtime analysis to see which approach is more efficient and effective.
> Pathfinder.exe 1
where target is [4, 7]
Initializing Grid...
Initial state:
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][*][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][X][ ][*][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ 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