Cleanup AI code

This commit is contained in:
Corentin Jabot 2021-10-05 11:58:06 +02:00
parent 61fd3bb5c0
commit d5379872ce
7 changed files with 71 additions and 54 deletions

View file

@ -80,7 +80,7 @@ void Canvas::renderMaze() {
void Canvas::renderPellets(const Pellets & pellets) { void Canvas::renderPellets(const Pellets & pellets) {
Sprite pellet = getSprite(pellets.currentSprite()); Sprite pellet = getSprite(pellets.currentSprite());
std::vector<GridPosition> pelletPositions = pellets.currentPositions(); std::vector<GridPosition> pelletPositions = pellets.allPellets();
for (const auto & pos : pelletPositions) { for (const auto & pos : pelletPositions) {
renderSprite(pellet, gridPositionToPosition(pos)); renderSprite(pellet, gridPositionToPosition(pos));
} }
@ -88,7 +88,7 @@ void Canvas::renderPellets(const Pellets & pellets) {
void Canvas::renderSuperPellets(const SuperPellets & superPellets) { void Canvas::renderSuperPellets(const SuperPellets & superPellets) {
Sprite pellet = getSprite(superPellets.currentSprite()); Sprite pellet = getSprite(superPellets.currentSprite());
std::vector<GridPosition> superPelletPositions = superPellets.currentPositions(); std::vector<GridPosition> superPelletPositions = superPellets.allPellets();
for (const auto & pos : superPelletPositions) { for (const auto & pos : superPelletPositions) {
renderSprite(pellet, gridPositionToPosition(pos)); renderSprite(pellet, gridPositionToPosition(pos));
} }

View file

@ -8,7 +8,10 @@ constexpr int POWER_PELLET_POINTS = 50;
void GameState::step(std::chrono::milliseconds delta) { void GameState::step(std::chrono::milliseconds delta) {
pacManAI.update(pacMan, pellets); pacManAI.update(pacMan, pellets);
pacMan.update(delta, inputState.direction()); pacMan.update(delta,
pacManAI.suggestedDirection()
//inputState.direction()
);
if (isPacManDying()) { if (isPacManDying()) {
handleDeathAnimation(delta); handleDeathAnimation(delta);
@ -59,6 +62,7 @@ void GameState::handleDeathAnimation(std::chrono::milliseconds delta) {
pinky.reset(); pinky.reset();
inky.reset(); inky.reset();
pacMan.reset(); pacMan.reset();
pacManAI.reset();
timeSinceDeath = std::chrono::milliseconds(0); timeSinceDeath = std::chrono::milliseconds(0);
} }
} }

View file

@ -4,44 +4,66 @@
#include <fmt/format.h> #include <fmt/format.h>
namespace pacman { namespace pacman {
void PacManAI::reset() {
pos = {};
direction = Direction::RIGHT;
}
Direction PacManAI::suggestedDirection() const { Direction PacManAI::suggestedDirection() const {
return direction; return direction;
} }
GridPosition PacManAI::pelletClosestToPacman(GridPosition pacmanGridPosition,
const Pellets & pellets) {
auto pelletPositions = pellets.allPellets();
auto pelletSort = [&pacmanGridPosition](GridPosition pelletA, GridPosition pelletB) {
double distanceA = positionDistance(pacmanGridPosition, pelletA);
double distanceB = positionDistance(pacmanGridPosition, pelletB);
return distanceA < distanceB;
};
std::sort(pelletPositions.begin(), pelletPositions.end(), pelletSort);
return pelletPositions[0];
}
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;
}
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) { void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
const GridPosition pacManGridPos = pacMan.positionInGrid(); const GridPosition pacManGridPos = pacMan.positionInGrid();
const GridPosition currentGridPos = positionToGridPosition(pos); const GridPosition currentGridPos = positionToGridPosition(pos);
if (currentGridPos == pacManGridPos) {
if (!isIntersection(pacManGridPos) || currentGridPos == pacManGridPos) {
return; return;
} }
pos = gridPositionToPosition(pacManGridPos); const auto & pelletPositions = pellets.allPellets();
if (!isIntersection(pacManGridPos)) {
return;
}
auto pelletPositions = pellets.currentPositions();
if (pelletPositions.empty()) { if (pelletPositions.empty()) {
return; return;
} }
auto pelletSort = [&pacManGridPos](GridPosition pelletA, GridPosition pelletB) {
double distanceA = positionDistance(pacManGridPos, pelletA);
double distanceB = positionDistance(pacManGridPos, pelletB);
return distanceA < distanceB;
};
std::sort(pelletPositions.begin(), pelletPositions.end(), pelletSort);
const auto & target = pelletPositions[0]; const GridPosition targetPos = pelletClosestToPacman(pacManGridPos, pellets);
const Position targetPos{ double(target.x), double(target.y) };
struct Move { const GridPosition currentPosition = pacMan.positionInGrid();
Direction direction = Direction::NONE;
Position position;
double distanceToTarget = std::numeric_limits<double>::infinity();
};
const Position currentPosition = { double(pacManGridPos.x), double(pacManGridPos.y) };
const auto [x, y] = currentPosition; const auto [x, y] = currentPosition;
std::array<Move, 4> possibleMoves = { std::array<Move, 4> possibleMoves = {
Move{ Direction::UP, { x, y - 1 } }, Move{ Direction::UP, { x, y - 1 } },
@ -51,30 +73,10 @@ void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
}; };
for (auto & move : possibleMoves) { for (auto & move : possibleMoves) {
const bool invalidPosition = (move.position.x < 0 || move.position.y < 0); if (!isValidMove(move))
if (invalidPosition) {
continue; continue;
}
const bool isOpposite = (move.direction == oppositeDirection(direction));
if (isOpposite) {
continue;
}
const GridPosition gridMove = { size_t(move.position.x), size_t(move.position.y) };
const bool canWalk = isWalkableForPacMan(gridMove);
if (!canWalk) {
continue;
}
move.distanceToTarget = positionDistance(move.position, targetPos); move.distanceToTarget = positionDistance(move.position, targetPos);
} }
direction = optimalDirection(possibleMoves);
const auto optimalMove = std::min_element(possibleMoves.begin(), possibleMoves.end(), [](const auto & a, const auto & b) {
return a.distanceToTarget < b.distanceToTarget;
});
const auto & move = *optimalMove;
direction = move.direction;
} }
} // namespace pacman } // namespace pacman

View file

@ -13,8 +13,19 @@ namespace pacman {
class PacManAI { class PacManAI {
public: public:
struct Move {
Direction direction = Direction::NONE;
GridPosition position;
double distanceToTarget = std::numeric_limits<double>::infinity();
};
void update(const PacMan & pacMan, const Pellets & pellets); void update(const PacMan & pacMan, const Pellets & pellets);
Direction suggestedDirection() const; Direction suggestedDirection() const;
GridPosition pelletClosestToPacman(GridPosition pacmanGridPosition,
const Pellets & pellets);
bool isValidMove(const Move & move);
Direction optimalDirection(const std::array<Move, 4> & moves);
void reset();
private: private:
Position pos; Position pos;

View file

@ -13,7 +13,7 @@ public:
return sprite; return sprite;
}; };
std::vector<GridPosition> currentPositions() const { std::vector<GridPosition> allPellets() const {
return positions; return positions;
} }

View file

@ -13,7 +13,7 @@ public:
return sprite; return sprite;
} }
std::vector<GridPosition> currentPositions() const { std::vector<GridPosition> allPellets() const {
return positions; return positions;
} }

View file

@ -4,7 +4,7 @@
template<typename T> template<typename T>
static void pelletInitTest(const T & pellets, size_t size, size_t spriteX, size_t spriteY) { static void pelletInitTest(const T & pellets, size_t size, size_t spriteX, size_t spriteY) {
REQUIRE(pellets.currentPositions().size() == size); REQUIRE(pellets.allPellets().size() == size);
REQUIRE(pellets.currentSprite().x == spriteX); REQUIRE(pellets.currentSprite().x == spriteX);
REQUIRE(pellets.currentSprite().y == spriteY); REQUIRE(pellets.currentSprite().y == spriteY);
} }
@ -19,13 +19,13 @@ TEST_CASE("Pellet initialization", "[pellets]") {
template<typename T> template<typename T>
static void pelletEatingTest(T & pellets, size_t countBefore, size_t countAfter, size_t x, size_t y) { static void pelletEatingTest(T & pellets, size_t countBefore, size_t countAfter, size_t x, size_t y) {
REQUIRE(pellets.currentPositions().size() == countBefore); REQUIRE(pellets.allPellets().size() == countBefore);
if (countBefore != countAfter) { if (countBefore != countAfter) {
REQUIRE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y })); REQUIRE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y }));
} else { } else {
REQUIRE_FALSE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y })); REQUIRE_FALSE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y }));
} }
REQUIRE(pellets.currentPositions().size() == countAfter); REQUIRE(pellets.allPellets().size() == countAfter);
} }
TEST_CASE("Eating pellets", "[pellets]") { TEST_CASE("Eating pellets", "[pellets]") {