pacman/lib/PacManAI.cpp

69 lines
1.8 KiB
C++
Raw Normal View History

#include "PacManAI.hpp"
#include "Board.hpp"
#include <fmt/format.h>
namespace pacman {
2021-10-05 09:58:06 +00:00
void PacManAI::reset() {
pos = {};
direction = Direction::RIGHT;
}
Direction PacManAI::suggestedDirection() const {
2021-09-22 12:15:33 +00:00
return direction;
}
2021-10-05 13:46:06 +00:00
// This function is not yet implemented.
// You will implement it as part of module 25.
2021-10-06 09:37:48 +00:00
GridPosition PacManAI::pelletClosestToPacman(GridPosition,
std::vector<GridPosition> &) {
2021-10-05 11:42:56 +00:00
2021-10-05 13:46:06 +00:00
return {0, 0};
2021-10-05 09:58:06 +00:00
}
2021-10-05 13:46:06 +00:00
// This function is not yet implemented.
// You will implement it as part of module 25.
2021-10-06 09:37:48 +00:00
bool PacManAI::isValidMove(const Move &) {
2021-10-05 09:58:06 +00:00
return false;
}
2021-10-05 13:46:06 +00:00
// This function is not yet implemented.
// You will implement it as part of module 25.
2021-10-06 09:37:48 +00:00
Direction PacManAI::optimalDirection(const std::array<Move, 4> &) {
2021-10-05 13:46:06 +00:00
return Direction::NONE;
2021-10-05 09:58:06 +00:00
}
2021-09-22 12:06:17 +00:00
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
const GridPosition pacManGridPos = pacMan.positionInGrid();
const GridPosition currentGridPos = positionToGridPosition(pos);
2021-10-05 09:58:06 +00:00
if (!isIntersection(pacManGridPos) || currentGridPos == pacManGridPos) {
return;
}
2021-10-05 11:42:56 +00:00
auto pelletPositions = pellets.allPellets();
if (pelletPositions.empty()) {
return;
}
2021-10-05 11:42:56 +00:00
const GridPosition targetPos = pelletClosestToPacman(pacManGridPos, pelletPositions);
2021-10-05 09:58:06 +00:00
const GridPosition currentPosition = pacMan.positionInGrid();
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 } }
};
for (auto & move : possibleMoves) {
2021-10-05 09:58:06 +00:00
if (!isValidMove(move))
continue;
move.distanceToTarget = positionDistance(move.position, targetPos);
}
2021-10-05 09:58:06 +00:00
direction = optimalDirection(possibleMoves);
}
2021-10-05 09:58:06 +00:00
} // namespace pacman