Changes after code review.
This commit is contained in:
parent
260adb54db
commit
a951c19a05
8 changed files with 21 additions and 87 deletions
|
@ -79,36 +79,6 @@ bool isPortal(GridPosition point, Direction direction) {
|
||||||
(cellAtPosition(point) == Cell::right_portal && direction == Direction::RIGHT);
|
(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) {
|
GridPosition teleport(GridPosition point) {
|
||||||
size_t right = COLUMNS - 1;
|
size_t right = COLUMNS - 1;
|
||||||
size_t left = 0;
|
size_t left = 0;
|
||||||
|
@ -176,38 +146,4 @@ bool isIntersection(GridPosition point) {
|
||||||
return (topWalkable && rightWalkable) || (rightWalkable && bottomWalkable) || (bottomWalkable && leftWalkable) || (leftWalkable && topWalkable);
|
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
|
} // namespace pacman
|
||||||
|
|
|
@ -29,7 +29,7 @@ Position Clyde::target(const GameState & gameState) const {
|
||||||
return targetPosition;
|
return targetPosition;
|
||||||
|
|
||||||
const auto pacManPosition = gameState.pacMan.position();
|
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)
|
if (distanceFomPacMan > 8)
|
||||||
targetPosition = pacManPosition;
|
targetPosition = pacManPosition;
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ void Ghost::frighten() {
|
||||||
if (state > State::Scatter)
|
if (state > State::Scatter)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
dir = oppositeDirection(dir);
|
direction = oppositeDirection(direction);
|
||||||
state = State::Frightened;
|
state = State::Frightened;
|
||||||
timeFrighten = {};
|
timeFrighten = {};
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ void Ghost::die() {
|
||||||
if (state == State::Eyes)
|
if (state == State::Eyes)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
dir = oppositeDirection(dir);
|
direction = oppositeDirection(direction);
|
||||||
state = State::Eyes;
|
state = State::Eyes;
|
||||||
timeFrighten = {};
|
timeFrighten = {};
|
||||||
timeChase = {};
|
timeChase = {};
|
||||||
|
@ -47,9 +47,9 @@ void Ghost::reset() {
|
||||||
GridPosition Ghost::currentSprite() const {
|
GridPosition Ghost::currentSprite() const {
|
||||||
switch (state) {
|
switch (state) {
|
||||||
default:
|
default:
|
||||||
return Atlas::ghostSprite(spriteSet, dir, (animationIndex % 2) == 0);
|
return Atlas::ghostSprite(spriteSet, direction, (animationIndex % 2) == 0);
|
||||||
case State::Eyes:
|
case State::Eyes:
|
||||||
return Atlas::eyeSprite(dir);
|
return Atlas::eyeSprite(direction);
|
||||||
case State::Frightened:
|
case State::Frightened:
|
||||||
if (timeFrighten.count() < 3500)
|
if (timeFrighten.count() < 3500)
|
||||||
return Atlas::initialFrightened(animationIndex);
|
return Atlas::initialFrightened(animationIndex);
|
||||||
|
@ -66,8 +66,8 @@ GridPosition Ghost::positionInGrid() const {
|
||||||
return positionToGridPosition(pos);
|
return positionToGridPosition(pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
Direction Ghost::direction() const {
|
Direction Ghost::currentDirection() const {
|
||||||
return dir;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Ghost::update(std::chrono::milliseconds time_delta, const GameState & gameState) {
|
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;
|
timeChase += time_delta;
|
||||||
const auto newState = defaultStateAtDuration(std::chrono::duration_cast<std::chrono::seconds>(timeChase));
|
const auto newState = defaultStateAtDuration(std::chrono::duration_cast<std::chrono::seconds>(timeChase));
|
||||||
if (newState != state) {
|
if (newState != state) {
|
||||||
dir = oppositeDirection(dir);
|
direction = oppositeDirection(direction);
|
||||||
state = newState;
|
state = newState;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +105,7 @@ void Ghost::updatePosition(std::chrono::milliseconds time_delta, const GameState
|
||||||
const auto old_position = pos;
|
const auto old_position = pos;
|
||||||
const GridPosition old_grid_position = positionToGridPosition(old_position);
|
const GridPosition old_grid_position = positionToGridPosition(old_position);
|
||||||
|
|
||||||
switch (dir) {
|
switch (direction) {
|
||||||
case Direction::NONE:
|
case Direction::NONE:
|
||||||
break;
|
break;
|
||||||
case Direction::LEFT:
|
case Direction::LEFT:
|
||||||
|
@ -126,11 +126,11 @@ void Ghost::updatePosition(std::chrono::milliseconds time_delta, const GameState
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPortal(positionInGrid(), dir)) {
|
if (isPortal(positionInGrid(), direction)) {
|
||||||
pos = gridPositionToPosition(teleport(positionInGrid()));
|
pos = gridPositionToPosition(teleport(positionInGrid()));
|
||||||
} else if (!isWalkableForGhost(positionInGrid(), old_grid_position, isEyes())) {
|
} else if (!isWalkableForGhost(positionInGrid(), old_grid_position, isEyes())) {
|
||||||
pos = old_position;
|
pos = old_position;
|
||||||
dir = oppositeDirection(dir);
|
direction = oppositeDirection(direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -180,7 +180,7 @@ void Ghost::updateDirection(const GameState & gameState) {
|
||||||
if (invalid_position)
|
if (invalid_position)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
const bool opposite_direction = (move.direction == oppositeDirection(dir));
|
const bool opposite_direction = (move.direction == oppositeDirection(direction));
|
||||||
if (opposite_direction)
|
if (opposite_direction)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ void Ghost::updateDirection(const GameState & gameState) {
|
||||||
if (!can_walk)
|
if (!can_walk)
|
||||||
continue;
|
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) {
|
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;
|
const auto & move = *optimal_move;
|
||||||
dir = move.direction;
|
direction = move.direction;
|
||||||
last_grid_position = current_grid_position;
|
last_grid_position = current_grid_position;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ Position Inky::target(const GameState & gameState) const {
|
||||||
|
|
||||||
// Then it calculates the distance between Blinky and this position
|
// Then it calculates the distance between Blinky and this position
|
||||||
const auto & blinkyPosition = gameState.blinky.positionInGrid();
|
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
|
// And selects a point on the line crossing blinky and this position that is at twice that distance
|
||||||
// away from blinky
|
// away from blinky
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
|
|
||||||
namespace pacman {
|
namespace pacman {
|
||||||
Direction PacManAI::suggestedDirection() const {
|
Direction PacManAI::suggestedDirection() const {
|
||||||
return dir;
|
return direction;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
|
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
|
||||||
|
@ -56,7 +56,7 @@ void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const bool isOpposite = (move.direction == oppositeDirection(dir));
|
const bool isOpposite = (move.direction == oppositeDirection(direction));
|
||||||
if (isOpposite) {
|
if (isOpposite) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,6 @@ void PacManAI::update(const PacMan & pacMan, const Pellets & pellets) {
|
||||||
});
|
});
|
||||||
|
|
||||||
const auto & move = *optimalMove;
|
const auto & move = *optimalMove;
|
||||||
dir = move.direction;
|
direction = move.direction;
|
||||||
}
|
}
|
||||||
} // namespace pacman
|
} // namespace pacman
|
|
@ -12,7 +12,6 @@ bool isWalkableForPacMan(GridPosition point);
|
||||||
bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes);
|
bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes);
|
||||||
bool isInPen(GridPosition point);
|
bool isInPen(GridPosition point);
|
||||||
bool isPortal(GridPosition point, Direction direction);
|
bool isPortal(GridPosition point, Direction direction);
|
||||||
GridPosition iterateGridPosition(GridPosition point, Direction direction);
|
|
||||||
GridPosition teleport(GridPosition point);
|
GridPosition teleport(GridPosition point);
|
||||||
|
|
||||||
std::vector<GridPosition> initialPelletPositions();
|
std::vector<GridPosition> initialPelletPositions();
|
||||||
|
@ -26,7 +25,6 @@ inline Position initialPacManPosition() {
|
||||||
return { 13.5, 23 };
|
return { 13.5, 23 };
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isWalkableStraightLine(GridPosition pointA, GridPosition pointB);
|
|
||||||
bool isIntersection(GridPosition point);
|
bool isIntersection(GridPosition point);
|
||||||
|
|
||||||
} // namespace pacman
|
} // namespace pacman
|
||||||
|
|
|
@ -25,7 +25,7 @@ public:
|
||||||
GridPosition currentSprite() const;
|
GridPosition currentSprite() const;
|
||||||
Position position() const;
|
Position position() const;
|
||||||
GridPosition positionInGrid() const;
|
GridPosition positionInGrid() const;
|
||||||
Direction direction() const;
|
Direction currentDirection() const;
|
||||||
|
|
||||||
void update(std::chrono::milliseconds time_delta, const GameState & gameState);
|
void update(std::chrono::milliseconds time_delta, const GameState & gameState);
|
||||||
void frighten();
|
void frighten();
|
||||||
|
@ -41,7 +41,7 @@ private:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Atlas::Ghost spriteSet;
|
Atlas::Ghost spriteSet;
|
||||||
Direction dir = Direction::NONE;
|
Direction direction = Direction::NONE;
|
||||||
double timeForAnimation = 0;
|
double timeForAnimation = 0;
|
||||||
std::size_t animationIndex = 0;
|
std::size_t animationIndex = 0;
|
||||||
State state = State::Chase;
|
State state = State::Chase;
|
||||||
|
|
|
@ -19,7 +19,7 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Position pos;
|
Position pos;
|
||||||
Direction dir = Direction::RIGHT;
|
Direction direction = Direction::RIGHT;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace pacman
|
} // namespace pacman
|
||||||
|
|
Loading…
Reference in a new issue