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

View file

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

View file

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

View file

@ -13,8 +13,19 @@ namespace pacman {
class PacManAI {
public:
struct Move {
Direction direction = Direction::NONE;
GridPosition position;
double distanceToTarget = std::numeric_limits<double>::infinity();
};
void update(const PacMan & pacMan, const Pellets & pellets);
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:
Position pos;

View file

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

View file

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

View file

@ -4,7 +4,7 @@
template<typename T>
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().y == spriteY);
}
@ -19,13 +19,13 @@ TEST_CASE("Pellet initialization", "[pellets]") {
template<typename T>
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) {
REQUIRE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y }));
} else {
REQUIRE_FALSE(pellets.eatPelletAtPosition(pacman::GridPosition{ x, y }));
}
REQUIRE(pellets.currentPositions().size() == countAfter);
REQUIRE(pellets.allPellets().size() == countAfter);
}
TEST_CASE("Eating pellets", "[pellets]") {