2021-09-16 14:36:57 +00:00
|
|
|
#include "PacManAI.hpp"
|
|
|
|
|
2021-09-17 14:44:16 +00:00
|
|
|
#include "Board.hpp"
|
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2021-09-16 14:36:57 +00:00
|
|
|
namespace pacman {
|
2021-09-17 14:44:16 +00:00
|
|
|
Direction PacManAI::suggestedDirection() const {
|
2021-09-20 13:17:07 +00:00
|
|
|
return dir;
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-22 12:06:17 +00:00
|
|
|
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
|
2021-09-17 14:44:16 +00:00
|
|
|
const GridPosition pacManGridPos = pacMan.positionInGrid();
|
|
|
|
const GridPosition currentGridPos = positionToGridPosition(pos);
|
|
|
|
if (currentGridPos == pacManGridPos) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = gridPositionToPosition(pacManGridPos);
|
|
|
|
|
|
|
|
if (!isIntersection(pacManGridPos)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
auto pelletPositions = pellets.currentPositions();
|
|
|
|
if (pelletPositions.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto pelletSort = [&pacManGridPos](GridPosition pelletA, GridPosition pelletB) {
|
|
|
|
double distanceA = positionDistance(pacManGridPos, pelletA);
|
|
|
|
double distanceB = positionDistance(pacManGridPos, pelletB);
|
|
|
|
return distanceA < distanceB;
|
2021-09-17 14:44:16 +00:00
|
|
|
};
|
2021-09-20 13:17:07 +00:00
|
|
|
std::sort(pelletPositions.begin(), pelletPositions.end(), pelletSort);
|
2021-09-17 14:44:16 +00:00
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const auto & target = pelletPositions[0];
|
|
|
|
const Position targetPos{ double(target.x), double(target.y) };
|
|
|
|
|
|
|
|
struct Move {
|
|
|
|
Direction direction = Direction::NONE;
|
|
|
|
Position position;
|
|
|
|
double distanceToTarget = std::numeric_limits<double>::infinity();
|
2021-09-17 14:44:16 +00:00
|
|
|
};
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const Position currentPosition = { double(pacManGridPos.x), double(pacManGridPos.y) };
|
|
|
|
const auto [x, y] = currentPosition;
|
|
|
|
std::array<Move, 4> possibleMoves = {
|
|
|
|
Move{ Direction::UP, { x, y - 1 } },
|
|
|
|
Move{ Direction::LEFT, { x - 1, y } },
|
|
|
|
Move{ Direction::DOWN, { x, y + 1 } },
|
|
|
|
Move{ Direction::RIGHT, { x + 1, y } }
|
2021-09-17 14:44:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (auto & move : possibleMoves) {
|
2021-09-20 13:17:07 +00:00
|
|
|
const bool invalidPosition = (move.position.x < 0 || move.position.y < 0);
|
|
|
|
if (invalidPosition) {
|
2021-09-17 14:44:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const bool isOpposite = (move.direction == oppositeDirection(dir));
|
|
|
|
if (isOpposite) {
|
|
|
|
continue;
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const GridPosition gridMove = { size_t(move.position.x), size_t(move.position.y) };
|
|
|
|
const bool canWalk = isWalkableForPacMan(gridMove);
|
|
|
|
if (!canWalk) {
|
|
|
|
continue;
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
move.distanceToTarget = positionDistance(move.position, targetPos);
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const auto optimalMove = std::min_element(possibleMoves.begin(), possibleMoves.end(), [](const auto & a, const auto & b) {
|
|
|
|
return a.distanceToTarget < b.distanceToTarget;
|
|
|
|
});
|
2021-09-17 14:44:16 +00:00
|
|
|
|
2021-09-20 13:17:07 +00:00
|
|
|
const auto & move = *optimalMove;
|
|
|
|
dir = move.direction;
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
} // namespace pacman
|