Create pellet_closest_to_pacman_simple

This commit is contained in:
Patricia Aas 2021-10-19 10:10:48 +02:00 committed by GitHub
parent c981f127ed
commit 18f05f0ef4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 0 deletions

View File

@ -0,0 +1,15 @@
GridPosition PacManAI::pelletClosestToPacman(GridPosition pacmanGridPosition,
std::vector<GridPosition> & pellets) {
GridPosition closestPellet = { 0, 0 };
double closestDistance = std::numeric_limits<double>::infinity();
for (const auto & pellet : pellets) {
const double distance = positionDistance(pacmanGridPosition, pellet);
if (distance < closestDistance) {
closestDistance = distance;
closestPellet = pellet;
}
}
return closestPellet;
}