Using heuristics in a game
Adding heuristics in a game means to define rules. We need to define a set of rules for the AI agent so that it can move to its destination in the best possible way. For example, if we want to write a pathfinding algorithm, and define only its start and end positions, it may get there in many different ways. However, if we want the agent to reach the goal in a particular way, we need to establish a heuristic function for it.
Getting ready
You need a Windows machine and a working copy of Visual Studio. No other prerequisites are required.
How to do it…
In this recipe, we will find out how easy it is to add a heuristic function to our game for pathfinding. Add a source file called Source.cpp
and add the following code to it:
for (auto next : graph.neighbors(current)) { int new_cost = cost_so_far[current] + graph.cost(current, next); if (!cost_so_far.count(next) || new_cost < cost_so_far[next]) { cost_so_far[next] = new_cost; int priority...