Browse Source

Ex 25: Implement pelletClosestToPacman().

des/ndctt2021
Dag-Erling Smørgrav 10 months ago
parent
commit
4b58b4115c
  1. 11
      lib/PacManAI.cpp
  2. 1
      test/testPacmanAI.cpp

11
lib/PacManAI.cpp

@ -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.

1
test/testPacmanAI.cpp

@ -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}},

Loading…
Cancel
Save