From f96fee2bfd0d902ede6228cfe01eeec39d2e4ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Tue, 19 Oct 2021 10:48:26 +0200 Subject: [PATCH] Ex 25: Implement PacManAI::optimalDirection(). --- lib/PacManAI.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/PacManAI.cpp b/lib/PacManAI.cpp index 523bdff..c605b6b 100644 --- a/lib/PacManAI.cpp +++ b/lib/PacManAI.cpp @@ -33,8 +33,11 @@ bool PacManAI::isValidMove(const Move & move) { // This function is not yet implemented. // You will implement it as part of module 25. -Direction PacManAI::optimalDirection(const std::array &) { - return Direction::NONE; +Direction PacManAI::optimalDirection(const std::array & moves) { + auto bestMove = std::min_element(moves.begin(), moves.end(), [](Move a, Move b) { + return a.distanceToTarget < b.distanceToTarget; + }); + return bestMove->direction; } void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {