Skip to content
Saarrthi Rana

School project / Fall 2025

A* pathfinding

A 2D tile game in C++ and SDL3 for my Artificial Intelligence course.

Role
Solo
Team
Artificial Intelligence course
When
Fall 2025
C++SDL3A*Enemy AI
Video
Clicking around the map while the player finds a way around walls and water and the enemies patrol and chase. Yellow squares are the path and white squares are the tiles the search checked. The last part turns on the view that shows every checked tile.
What it is

Characters find their own way around walls and water, and their paths are smoothed so they don't zigzag.

You click a tile and the player walks there, going around walls and water. Sand is slower to cross than grass, and some tiles only let you go one way. I added path smoothing, a fireball that curves to its target, and two enemies that use the same pathfinding to patrol and chase you.

What I did
  1. Wrote the A* search that finds the shortest path across the map.
  2. Made sand cost more to cross than grass, and got the one way tiles working.
  3. Smoothed the path, so the player walks in a straight line when nothing's in the way.
  4. Made the player's fireball fly to its target along a curve.
  5. Wrote a melee enemy that patrols and chases you when you get close, and a ranged enemy that stops and shoots.
  6. Added a view that shows every tile the search checked.
How it works

How the search works

A* keeps checking the tile that looks closest to the goal, counting how far it's come and how far it still has to go. When it reaches the goal, it walks back the way it came to build the path.

Straighter paths

For each point on the path, I look for the furthest point ahead with a clear line to it and skip everything in between. The player still moves tile by tile, but in a straight line.

Chase range

The melee enemy starts chasing at 6 tiles and only gives up past 7.2, so it doesn't keep switching back and forth when you stand at the edge.

Code

Code I wrote

Short parts of my own code from this project. I left out anything the course gave us.

The A* search

AStar.cpp, C++ and SDL3

The main loop of my search. The white squares in the video come from the list of tiles it visits.
std::vector<Node*> AStar::findPath(Node* start, Node* goal)
{
    // ...
    gScore[start] = 0.0f;
    fScore[start] = heuristic(start, goal);
    openSet.push({ start, fScore[start] });

    while (!openSet.empty())
    {
        Node* current = openSet.top().node;
        openSet.pop();

        visitedOrder_.push_back(current); // for white squares

        if (current == goal)
        {
            // Reconstruct path
            std::vector<Node*> path;
            // ...
            return path;
        }

        for (Edge* edge : current->getEdges())
        {
            Node* neighbour = edge->getTo();
            float weight = edge->getWeight();

            float tentativeG = gScore[current] + weight;

            auto gIt = gScore.find(neighbour);
            if (gIt == gScore.end() || tentativeG < gIt->second)
            {
                cameFrom[neighbour] = current;
                gScore[neighbour] = tentativeG;
                float f = tentativeG + heuristic(neighbour, goal);
                fScore[neighbour] = f;
                openSet.push({ neighbour, f });
            }
        }
    }

    // No path
    return emptyPath;
}

Smoothing the path

Main.cpp, C++ and SDL3

Jumps ahead to the furthest tile with a clear line, then fills in the tiles along that line.
std::vector<Node*> smoothPath(const std::vector<Node*>& original,
    Graph& graph,
    const std::vector<TileType>& tilemap,
    int width, int height)
    // ...
    std::vector<Node*> result;

    size_t i = 0;
    while (i < original.size() - 1)
    {
        Node* start = original[i];

        // Find furthest node we can connect in a straight line
        size_t furthest = i + 1;
        for (size_t j = original.size() - 1; j > i + 1; --j)
        {
            if (hasLineOfSight(start, original[j], tilemap, width, height))
            {
                furthest = j;
                break;
            }
        }

        Node* end = original[furthest];

        // Fill segment tiles with Bresenham
        auto pts = bresenhamLine(start->getX(), start->getY(),
            end->getX(), end->getY());

        for (auto& p : pts)
        {
            int x = p.first;
            int y = p.second;
            Node* n = graph.getNodeAt(x, y);
            if (!n) continue;

            if (!result.empty() && result.back() == n)
                continue; // avoid duplicates

            result.push_back(n);
        }

        i = furthest;
    }
    // ...

The tile map and one header file came with the assignment. I wrote the search, the path smoothing, the enemies and the fireballs.

Next school project

Team deathmatch

A networking assignment made with Blueprints in Unreal Engine 5.