Ex 25: Implement pelletClosestToPacman().

This commit is contained in:
Dag-Erling Smørgrav 2021-10-19 10:07:57 +02:00
parent 980a26587d
commit 4b58b4115c
2 changed files with 8 additions and 4 deletions

View file

@ -16,10 +16,13 @@ Direction PacManAI::suggestedDirection() const {
// This function is not yet implemented.
// You will implement it as part of module 25.
GridPosition PacManAI::pelletClosestToPacman(GridPosition,
std::vector<GridPosition> &) {
return {0, 0};
GridPosition PacManAI::pelletClosestToPacman(GridPosition position,
std::vector<GridPosition> & pellets) {
if (pellets.empty())
return { 0, 0 };
return *std::min_element(pellets.begin(), pellets.end(), [position](GridPosition a, GridPosition b) {
return positionDistance(a, position) < positionDistance(b, position);
});
}
// This function is not yet implemented.

View file

@ -7,6 +7,7 @@ TEST_CASE("Find pellet closest to PacMan", "[AI]") {
PacManAI AI;
using TestData = std::tuple<GridPosition, std::vector<GridPosition>, GridPosition>;
auto data = GENERATE(
TestData{{5, 5}, {}, {0, 0}},
TestData{{5, 5}, {{5, 6}}, {5, 6}},
TestData{{5, 5}, {{5, 5}}, {5, 5}},
TestData{{5, 5}, {{0, 0}, {5, 6}}, {5, 6}},