Changes after code review.

This commit is contained in:
Ólafur Waage 2021-09-22 14:15:33 +02:00
parent 260adb54db
commit a951c19a05
8 changed files with 21 additions and 87 deletions

View file

@ -79,36 +79,6 @@ bool isPortal(GridPosition point, Direction direction) {
(cellAtPosition(point) == Cell::right_portal && direction == Direction::RIGHT);
}
GridPosition iterateGridPosition(GridPosition point, Direction direction) {
switch (direction) {
case Direction::LEFT: {
if (point.x != 0) {
point.x -= 1;
}
break;
}
case Direction::RIGHT: {
point.x += 1;
break;
}
case Direction::UP: {
if (point.y != 0) {
point.y -= 1;
}
break;
}
case Direction::DOWN: {
point.y += 1;
break;
}
case Direction::NONE:
default:
break;
}
return point;
}
GridPosition teleport(GridPosition point) {
size_t right = COLUMNS - 1;
size_t left = 0;
@ -176,38 +146,4 @@ bool isIntersection(GridPosition point) {
return (topWalkable && rightWalkable) || (rightWalkable && bottomWalkable) || (bottomWalkable && leftWalkable) || (leftWalkable && topWalkable);
}
bool isWalkableStraightLine(GridPosition pointA, GridPosition pointB) {
// Points with no shared x,y have no straight line between them
if (pointA.x != pointB.x && pointA.y != pointB.y) {
return false;
}
// this is std::all_of
if (pointA.x == pointB.x) {
const size_t startY = (pointA.y > pointB.y ? pointB.y : pointA.y);
const size_t endY = (pointA.y > pointB.y ? pointA.y : pointB.y);
for (size_t y = startY; y <= endY; y++) {
const GridPosition test{ pointA.x, y };
if (!isWalkableForPacMan(test)) {
return false;
}
}
return true;
}
if (pointA.y == pointB.y) {
const size_t startX = (pointA.x > pointB.x ? pointB.x : pointA.x);
const size_t endX = (pointA.x > pointB.x ? pointA.x : pointB.x);
for (size_t x = startX; x <= endX; x++) {
const GridPosition test{ x, pointA.y };
if (!isWalkableForPacMan(test)) {
return false;
}
}
return true;
}
return false;
}
} // namespace pacman

View file

@ -29,7 +29,7 @@ Position Clyde::target(const GameState & gameState) const {
return targetPosition;
const auto pacManPosition = gameState.pacMan.position();
auto distanceFomPacMan = positionDistance(pos, pacManPosition);
const auto distanceFomPacMan = std::hypot(pos.x - double(pacManPosition.x), pos.y - double(pacManPosition.y));
if (distanceFomPacMan > 8)
targetPosition = pacManPosition;

View file

@ -14,7 +14,7 @@ void Ghost::frighten() {
if (state > State::Scatter)
return;
dir = oppositeDirection(dir);
direction = oppositeDirection(direction);
state = State::Frightened;
timeFrighten = {};
}
@ -31,7 +31,7 @@ void Ghost::die() {
if (state == State::Eyes)
return;
dir = oppositeDirection(dir);
direction = oppositeDirection(direction);
state = State::Eyes;
timeFrighten = {};
timeChase = {};
@ -47,9 +47,9 @@ void Ghost::reset() {
GridPosition Ghost::currentSprite() const {
switch (state) {
default:
return Atlas::ghostSprite(spriteSet, dir, (animationIndex % 2) == 0);
return Atlas::ghostSprite(spriteSet, direction, (animationIndex % 2) == 0);
case State::Eyes:
return Atlas::eyeSprite(dir);
return Atlas::eyeSprite(direction);
case State::Frightened:
if (timeFrighten.count() < 3500)
return Atlas::initialFrightened(animationIndex);
@ -66,8 +66,8 @@ GridPosition Ghost::positionInGrid() const {
return positionToGridPosition(pos);
}
Direction Ghost::direction() const {
return dir;
Direction Ghost::currentDirection() const {
return direction;
}
void Ghost::update(std::chrono::milliseconds time_delta, const GameState & gameState) {
@ -84,7 +84,7 @@ void Ghost::update(std::chrono::milliseconds time_delta, const GameState & gameS
timeChase += time_delta;
const auto newState = defaultStateAtDuration(std::chrono::duration_cast<std::chrono::seconds>(timeChase));
if (newState != state) {
dir = oppositeDirection(dir);
direction = oppositeDirection(direction);
state = newState;
}
}
@ -105,7 +105,7 @@ void Ghost::updatePosition(std::chrono::milliseconds time_delta, const GameState
const auto old_position = pos;
const GridPosition old_grid_position = positionToGridPosition(old_position);
switch (dir) {
switch (direction) {
case Direction::NONE:
break;
case Direction::LEFT:
@ -126,11 +126,11 @@ void Ghost::updatePosition(std::chrono::milliseconds time_delta, const GameState
break;
}
if (isPortal(positionInGrid(), dir)) {
if (isPortal(positionInGrid(), direction)) {
pos = gridPositionToPosition(teleport(positionInGrid()));
} else if (!isWalkableForGhost(positionInGrid(), old_grid_position, isEyes())) {
pos = old_position;
dir = oppositeDirection(dir);
direction = oppositeDirection(direction);
}
}
@ -180,7 +180,7 @@ void Ghost::updateDirection(const GameState & gameState) {
if (invalid_position)
continue;
const bool opposite_direction = (move.direction == oppositeDirection(dir));
const bool opposite_direction = (move.direction == oppositeDirection(direction));
if (opposite_direction)
continue;
@ -189,7 +189,7 @@ void Ghost::updateDirection(const GameState & gameState) {
if (!can_walk)
continue;
move.distance_to_target = positionDistance(move.position, target_position);
move.distance_to_target = std::hypot(move.position.x - target_position.x, move.position.y - target_position.y);
}
const auto optimal_move = std::min_element(possible_moves.begin(), possible_moves.end(), [](const auto & a, const auto & b) {
@ -197,7 +197,7 @@ void Ghost::updateDirection(const GameState & gameState) {
});
const auto & move = *optimal_move;
dir = move.direction;
direction = move.direction;
last_grid_position = current_grid_position;
}

View file

@ -49,7 +49,7 @@ Position Inky::target(const GameState & gameState) const {
// Then it calculates the distance between Blinky and this position
const auto & blinkyPosition = gameState.blinky.positionInGrid();
const double distanceBetweenBlinkyAndTarget = positionDistance(blinkyPosition, targetPosition);
const double distanceBetweenBlinkyAndTarget = std::hypot(blinkyPosition.x - targetPosition.x, blinkyPosition.y - targetPosition.y);
// And selects a point on the line crossing blinky and this position that is at twice that distance
// away from blinky

View file

@ -5,7 +5,7 @@
namespace pacman {
Direction PacManAI::suggestedDirection() const {
return dir;
return direction;
}
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
@ -56,7 +56,7 @@ void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
continue;
}
const bool isOpposite = (move.direction == oppositeDirection(dir));
const bool isOpposite = (move.direction == oppositeDirection(direction));
if (isOpposite) {
continue;
}
@ -75,6 +75,6 @@ void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
});
const auto & move = *optimalMove;
dir = move.direction;
direction = move.direction;
}
} // namespace pacman

View file

@ -12,7 +12,6 @@ bool isWalkableForPacMan(GridPosition point);
bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes);
bool isInPen(GridPosition point);
bool isPortal(GridPosition point, Direction direction);
GridPosition iterateGridPosition(GridPosition point, Direction direction);
GridPosition teleport(GridPosition point);
std::vector<GridPosition> initialPelletPositions();
@ -26,7 +25,6 @@ inline Position initialPacManPosition() {
return { 13.5, 23 };
}
bool isWalkableStraightLine(GridPosition pointA, GridPosition pointB);
bool isIntersection(GridPosition point);
} // namespace pacman

View file

@ -25,7 +25,7 @@ public:
GridPosition currentSprite() const;
Position position() const;
GridPosition positionInGrid() const;
Direction direction() const;
Direction currentDirection() const;
void update(std::chrono::milliseconds time_delta, const GameState & gameState);
void frighten();
@ -41,7 +41,7 @@ private:
protected:
Atlas::Ghost spriteSet;
Direction dir = Direction::NONE;
Direction direction = Direction::NONE;
double timeForAnimation = 0;
std::size_t animationIndex = 0;
State state = State::Chase;

View file

@ -19,7 +19,7 @@ public:
private:
Position pos;
Direction dir = Direction::RIGHT;
Direction direction = Direction::RIGHT;
};
} // namespace pacman