From a951c19a05c77767e9010af0c686052e1ea2cfa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=93lafur=20Waage?= Date: Wed, 22 Sep 2021 14:15:33 +0200 Subject: [PATCH] Changes after code review. --- lib/Board.cpp | 64 ---------------------------------------- lib/Clyde.cpp | 2 +- lib/Ghost.cpp | 26 ++++++++-------- lib/Inky.cpp | 2 +- lib/PacManAI.cpp | 6 ++-- lib/include/Board.hpp | 2 -- lib/include/Ghost.hpp | 4 +-- lib/include/PacManAI.hpp | 2 +- 8 files changed, 21 insertions(+), 87 deletions(-) diff --git a/lib/Board.cpp b/lib/Board.cpp index abf561e..e0950e4 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -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 diff --git a/lib/Clyde.cpp b/lib/Clyde.cpp index e851fd7..269185e 100644 --- a/lib/Clyde.cpp +++ b/lib/Clyde.cpp @@ -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; diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index 8ce3de5..69622f0 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -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(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; } diff --git a/lib/Inky.cpp b/lib/Inky.cpp index 1f9baf2..56c3a48 100644 --- a/lib/Inky.cpp +++ b/lib/Inky.cpp @@ -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 diff --git a/lib/PacManAI.cpp b/lib/PacManAI.cpp index cf9898b..f4dfece 100644 --- a/lib/PacManAI.cpp +++ b/lib/PacManAI.cpp @@ -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 \ No newline at end of file diff --git a/lib/include/Board.hpp b/lib/include/Board.hpp index 2684a49..56829c3 100644 --- a/lib/include/Board.hpp +++ b/lib/include/Board.hpp @@ -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 initialPelletPositions(); @@ -26,7 +25,6 @@ inline Position initialPacManPosition() { return { 13.5, 23 }; } -bool isWalkableStraightLine(GridPosition pointA, GridPosition pointB); bool isIntersection(GridPosition point); } // namespace pacman diff --git a/lib/include/Ghost.hpp b/lib/include/Ghost.hpp index ceac37e..cbee277 100644 --- a/lib/include/Ghost.hpp +++ b/lib/include/Ghost.hpp @@ -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; diff --git a/lib/include/PacManAI.hpp b/lib/include/PacManAI.hpp index 6600983..0ac6318 100644 --- a/lib/include/PacManAI.hpp +++ b/lib/include/PacManAI.hpp @@ -19,7 +19,7 @@ public: private: Position pos; - Direction dir = Direction::RIGHT; + Direction direction = Direction::RIGHT; }; } // namespace pacman