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-10-05 09:58:06 +00:00
|
|
|
|
|
|
|
void PacManAI::reset() {
|
|
|
|
pos = {};
|
|
|
|
direction = Direction::RIGHT;
|
|
|
|
}
|
|
|
|
|
2021-09-17 14:44:16 +00:00
|
|
|
Direction PacManAI::suggestedDirection() const {
|
2021-09-22 12:15:33 +00:00
|
|
|
return direction;
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 09:58:06 +00:00
|
|
|
GridPosition PacManAI::pelletClosestToPacman(GridPosition pacmanGridPosition,
|
2021-10-05 11:42:56 +00:00
|
|
|
std::vector<GridPosition> & pellets) {
|
|
|
|
|
2021-10-05 09:58:06 +00:00
|
|
|
auto pelletSort = [&pacmanGridPosition](GridPosition pelletA, GridPosition pelletB) {
|
|
|
|
double distanceA = positionDistance(pacmanGridPosition, pelletA);
|
|
|
|
double distanceB = positionDistance(pacmanGridPosition, pelletB);
|
|
|
|
return distanceA < distanceB;
|
|
|
|
};
|
2021-10-05 11:42:56 +00:00
|
|
|
std::sort(pellets.begin(), pellets.end(), pelletSort);
|
|
|
|
return pellets[0];
|
2021-10-05 09:58:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PacManAI::isValidMove(const Move & move) {
|
|
|
|
const bool isOpposite = (move.direction == oppositeDirection(direction));
|
|
|
|
if (isOpposite) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool canWalk = isWalkableForPacMan(move.position);
|
|
|
|
if (!canWalk) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves) {
|
|
|
|
const auto optimalMove = std::min_element(moves.begin(), moves.end(), [](const auto & a, const auto & b) {
|
|
|
|
return a.distanceToTarget < b.distanceToTarget;
|
|
|
|
});
|
|
|
|
|
|
|
|
const auto & move = *optimalMove;
|
|
|
|
return move.direction;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
2021-10-05 09:58:06 +00:00
|
|
|
if (!isIntersection(pacManGridPos) || currentGridPos == pacManGridPos) {
|
2021-09-17 14:44:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:42:56 +00:00
|
|
|
auto pelletPositions = pellets.allPellets();
|
2021-09-20 13:17:07 +00:00
|
|
|
if (pelletPositions.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 11:42:56 +00:00
|
|
|
const GridPosition targetPos = pelletClosestToPacman(pacManGridPos, pelletPositions);
|
2021-09-17 14:44:16 +00:00
|
|
|
|
2021-10-05 09:58:06 +00:00
|
|
|
const GridPosition currentPosition = pacMan.positionInGrid();
|
2021-09-20 13:17:07 +00:00
|
|
|
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-10-05 09:58:06 +00:00
|
|
|
if (!isValidMove(move))
|
2021-09-20 13:17:07 +00:00
|
|
|
continue;
|
|
|
|
move.distanceToTarget = positionDistance(move.position, targetPos);
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
2021-10-05 09:58:06 +00:00
|
|
|
direction = optimalDirection(possibleMoves);
|
2021-09-17 14:44:16 +00:00
|
|
|
}
|
2021-10-05 09:58:06 +00:00
|
|
|
} // namespace pacman
|