sfml dijkstra path finding in csharp

Here is an example implementation of Dijkstra's algorithm for path finding using SFML in C#:

main.cs
using System;
using System.Collections.Generic;
using SFML.Graphics;
using SFML.System;

namespace DijkstraPathfinding
{
    class Program
    {
        static void Main(string[] args)
        {
            // Set up the window
            RenderWindow window = new RenderWindow(new VideoMode(800, 600), "Dijkstra Pathfinding");
            window.Closed += delegate { window.Close(); };

            // Define the size of the grid and the size of each cell
            const int gridSize = 20;
            const int cellSize = 30;

            // Create the grid of nodes
            int width = window.Size.X / cellSize;
            int height = window.Size.Y / cellSize;
            Node[,] nodes = new Node[width, height];
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height; y++)
                {
                    nodes[x, y] = new Node(x, y, cellSize);
                }
            }

            // Set up the starting and ending nodes
            Node startNode = nodes[0, 0];
            Node endNode = nodes[width - 1, height - 1];

            // Define the list of open nodes and the list of closed nodes
            List<Node> openNodes = new List<Node>();
            List<Node> closedNodes = new List<Node>();

            // Add the starting node to the list of open nodes
            openNodes.Add(startNode);
            startNode.DistanceFromStart = 0;

            // Run the Dijkstra algorithm
            while (openNodes.Count > 0)
            {
                // Sort the open nodes by distance from start
                openNodes.Sort((n1, n2) => n1.DistanceFromStart.CompareTo(n2.DistanceFromStart));

                // Get the node with the lowest distance from start
                Node currentNode = openNodes[0];

                // Check if the current node is the end node
                if (currentNode == endNode)
                {
                    break;
                }

                // Remove the current node from the list of open nodes
                openNodes.RemoveAt(0);

                // Add the current node to the list of closed nodes
                closedNodes.Add(currentNode);

                // Check the unexplored neighbors of the current node
                foreach (Node neighbor in currentNode.Neighbors)
                {
                    // Skip the neighbor if it is already in the closed list
                    if (closedNodes.Contains(neighbor))
                    {
                        continue;
                    }

                    // Calculate the tentative distance from start to neighbor
                    float tentativeDistance = currentNode.DistanceFromStart + currentNode.CostToNeighbor(neighbor);

                    // Add the neighbor to the list of open nodes if it is not already in the list
                    if (!openNodes.Contains(neighbor))
                    {
                        openNodes.Add(neighbor);
                    }

                    // Update the neighbor's distance from start if the tentative distance is less than the current distance
                    if (tentativeDistance < neighbor.DistanceFromStart)
                    {
                        neighbor.DistanceFromStart = tentativeDistance;
                        neighbor.Parent = currentNode;
                    }
                }
            }

            // Draw the nodes and the path
            while (window.IsOpen)
            {
                // Process events
                window.DispatchEvents();

                // Clear the window
                window.Clear(Color.Black);

                // Draw the nodes
                for (int x = 0; x < width; x++)
                {
                    for (int y = 0; y < height; y++)
                    {
                        window.Draw(nodes[x, y].Shape);
                    }
                }

                // Draw the path
                Node current = endNode;
                while (current != startNode)
                {
                    current.Shape.FillColor = Color.Red;
                    current = current.Parent;
                }

                // Display the window
                window.Display();
            }
        }
    }

    class Node
    {
        public int X { get; set; }
        public int Y { get; set; }
        public float DistanceFromStart { get; set; }
        public Node Parent { get; set; }

        private int Size;
        public RectangleShape Shape { get; private set; }

        public List<Node> Neighbors { get; private set; }

        public Node(int x, int y, int size)
        {
            X = x;
            Y = y;
            Size = size;
            DistanceFromStart = float.MaxValue;

            Shape = new RectangleShape(new Vector2f(Size, Size));
            Shape.Position = new Vector2f(X * Size, Y * Size);
            Shape.FillColor = Color.White;
            Shape.OutlineThickness = 2;
            Shape.OutlineColor = Color.Black;

            Neighbors = new List<Node>();
            if (x > 0)
            {
                Neighbors.Add(Program.nodes[x - 1, y]);
            }
            if (x < Program.nodes.GetLength(0) - 1)
            {
                Neighbors.Add(Program.nodes[x + 1, y]);
            }
            if (y > 0)
            {
                Neighbors.Add(Program.nodes[x, y - 1]);
            }
            if (y < Program.nodes.GetLength(1) - 1)
            {
                Neighbors.Add(Program.nodes[x, y + 1]);
            }
        }

        public float CostToNeighbor(Node neighbor)
        {
            float cost = 1;
            return cost;
        }
    }
}
5713 chars
175 lines

Here, the grid of nodes is represented by a 2D array of Node objects, and each Node object has a list of neighboring nodes. The Dijkstra algorithm is implemented using the openNodes and closedNodes lists, and the DistanceFromStart property is used to keep track of the shortest distance from the start node to each node. Once the algorithm is run, the path is traced back from the end node to the start node by following each node's Parent property, and the path is drawn in red.

gistlibby LogSnag