Skip to content

milescajus/2D_pathfinder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

C# 2D Pathfinder

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.

Changelog

  • added CSV output to record the average number of movement points remaining for the alternate movement methods
  • added m_points comparison to see if Move() reaches the target with fewer moves on average than Move_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

TODO

  • switch to using Tuples for all coordinates
  • create graphical representation based on Godot's A* demo

Runtime Comparison

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.

Example Output

> Pathfinder.exe 1 where target is [4, 7]

Initializing Grid...
Initial state:
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][*][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][X][ ][*][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][ ][ 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][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
Remaining moves: 1

[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][*][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][*][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][*][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][X][ ][ ][ ][ ][ ]
[ ][ ][*][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][*][*][*][*][*][*][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ][ ][ ][ ]
Remaining moves: 0

About

C# 2D Pathfinder

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages