From d166f552f4bf2c718183ef1712fa5fd5912ebab9 Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Mon, 5 Jul 2021 13:54:54 +0200 Subject: [PATCH] Simplify board code --- lib/Board.cpp | 58 +++++++++++++----------------------- lib/Ghost.cpp | 8 ++--- lib/PacMan.cpp | 25 ++++++++++++---- lib/Pellets.cpp | 2 +- lib/SuperPellets.cpp | 2 +- lib/include/Board.hpp | 10 +++---- lib/include/Ghost.hpp | 4 +-- lib/include/PacMan.hpp | 2 +- lib/include/Pellets.hpp | 2 +- lib/include/Position.hpp | 6 +++- lib/include/SuperPellets.hpp | 2 +- 11 files changed, 61 insertions(+), 60 deletions(-) diff --git a/lib/Board.cpp b/lib/Board.cpp index 3006380..52e94a8 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -8,6 +8,7 @@ // 4 - superpower // 5 - pen doors +// clang-format off std::array, ROWS> Board::board = {{ // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 0 @@ -42,52 +43,35 @@ std::array, ROWS> Board::board = {{ { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, // 29 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, // 30 }}; +// clang-format on -bool Board::isWalkableForPacMan(Position point, double d, Direction direction) { - return isWalkable(point, d, direction, true); +bool Board::isWalkableForPacMan(GridPosition point) { + return cellAtPosition(point) != Cell::wall; } -bool Board::isWalkable(Position point) { - return board[int(point.y)][int(point.x)] != int(Cell::wall); +bool Board::isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes) { + Cell cell = cellAtPosition(point); + if (cell == Cell::wall) + return false; + return isEyes || (isInPen(origin) || !isInPen(point)); } -bool Board::isWalkableForGost(Position point, Position origin, bool isEyes) { - return isWalkable(point) && (isEyes || (isInPen(origin) || !isInPen(point))); +bool Board::isInPen(GridPosition point) { + return cellAtPosition(point) == Cell::pen; } -bool Board::isInPen(Position point) { - return board[int(point.y)][int(point.x)] == int(Cell::pen); -} - -bool Board::isWalkable(Position point, double position_delta, Direction direction, bool pacman) { - if (point.x <= 0 || point.x >= COLUMNS - 1) - return true; - - auto cellAtPosition = [&](Position point, double position_delta, Direction direction) { - switch (direction) { - case Direction::LEFT: - return board[int(point.y)][int(point.x - position_delta)]; - case Direction::RIGHT: - return board[int(point.y)][int(point.x) + 1]; - case Direction::UP: - return board[int(point.y - position_delta)][int(point.x)]; - case Direction::DOWN: - return board[int(point.y) + 1][int(point.x)]; - case Direction::NONE: - default: - return int(Cell::wall); - } - }; - Cell cell = Cell(cellAtPosition(point, position_delta, direction)); - return pacman ? cell != Cell::wall : cell != Cell::wall && cell != Cell::pen; +Board::Cell Board::cellAtPosition(GridPosition point) { + if (point.x < 0 || point.x >= int(COLUMNS) || point.y < 0 || point.y >= int(ROWS)) + return Cell::wall; + return Cell(board[point.y][point.x]); } std::vector Board::initialPelletPositions() { std::vector positions; - for (int row = 0; row < ROWS; row++) { - for (int column = 0; column < COLUMNS; column++) { + for (std::size_t row = 0; row < ROWS; row++) { + for (std::size_t column = 0; column < COLUMNS; column++) { if (board[row][column] == int(Cell::pellet)) - positions.push_back({ column, row }); + positions.push_back({ int(column), int(row) }); } } return positions; @@ -95,10 +79,10 @@ std::vector Board::initialPelletPositions() { std::vector Board::initialSuperPelletPositions() { std::vector positions; - for (int row = 0; row < ROWS; row++) { - for (int column = 0; column < COLUMNS; column++) { + for (std::size_t row = 0; row < ROWS; row++) { + for (std::size_t column = 0; column < COLUMNS; column++) { if (board[row][column] == int(Cell::power_pellet)) - positions.push_back({ column, row }); + positions.push_back({ int(column), int(row) }); } } return positions; diff --git a/lib/Ghost.cpp b/lib/Ghost.cpp index 725da05..ea1ca2c 100644 --- a/lib/Ghost.cpp +++ b/lib/Ghost.cpp @@ -56,8 +56,8 @@ Position Ghost::position() const { return pos; } -Position Ghost::positionInGrid() const { - return { std::round(pos.x), std::round(pos.y) }; +GridPosition Ghost::positionInGrid() const { + return { int(std::round(pos.x)), int(std::round(pos.y)) }; } void Ghost::update(std::chrono::milliseconds time_delta, const Board & board) { @@ -119,8 +119,8 @@ void Ghost::updateDirection(const Board & board) { return; struct NewDirection { - Direction direction; - Position position; + Direction direction; + GridPosition position; double distance; }; diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index 3be89a2..c889fa1 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -12,8 +12,8 @@ Position PacMan::position() const { return pos; } -Position PacMan::positionInGrid() const { - return { std::round(pos.x), std::round(pos.y) }; +GridPosition PacMan::positionInGrid() const { + return { int(std::round(pos.x)), int(std::round(pos.y)) }; } void PacMan::eat() { @@ -63,18 +63,33 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Board & board) { double position_delta = 0.004 * time_delta.count(); + auto cellAtPosition = [&](Position point, double position_delta, Direction direction) { + switch (direction) { + case Direction::LEFT: + return GridPosition{int(point.x - position_delta), int(point.y)}; + case Direction::RIGHT: + return GridPosition{int(point.x) + 1, int(point.y)}; + case Direction::UP: + return GridPosition{int(point.x), int(point.y - position_delta)}; + case Direction::DOWN: + return GridPosition{int(point.x), int(point.y) + 1}; + case Direction::NONE: + default: + return positionInGrid(); + } + }; + // Handle teleport if (pos.x >= COLUMNS - 1 && direction == Direction::RIGHT) { pos.x = -1; } else if (pos.x <= 0 && direction == Direction::LEFT) { pos.x = COLUMNS; } - - else if (board.isWalkableForPacMan(pos, position_delta, desired_direction)) { + else if (board.isWalkableForPacMan(cellAtPosition(pos, position_delta, desired_direction))) { direction = desired_direction; } - if (board.isWalkableForPacMan(pos, position_delta, direction)) { + if (board.isWalkableForPacMan(cellAtPosition(pos, position_delta, direction))) { switch (direction) { case Direction::NONE: break; diff --git a/lib/Pellets.cpp b/lib/Pellets.cpp index 8b38719..bd43f70 100644 --- a/lib/Pellets.cpp +++ b/lib/Pellets.cpp @@ -4,7 +4,7 @@ Pellets::Pellets(const Board & board) : positions(board.initialPelletPositions()) {} -bool Pellets::eatPelletAtPosition(Position p) { +bool Pellets::eatPelletAtPosition(GridPosition p) { auto it = std::find(positions.begin(), positions.end(), p); if (it == positions.end()) return false; diff --git a/lib/SuperPellets.cpp b/lib/SuperPellets.cpp index 3f6fd3c..d49861e 100644 --- a/lib/SuperPellets.cpp +++ b/lib/SuperPellets.cpp @@ -4,7 +4,7 @@ SuperPellets::SuperPellets(const Board & board) : positions(board.initialSuperPelletPositions()) {} -bool SuperPellets::eatPelletAtPosition(Position p) { +bool SuperPellets::eatPelletAtPosition(GridPosition p) { auto it = std::find(positions.begin(), positions.end(), p); if (it == positions.end()) return false; diff --git a/lib/include/Board.hpp b/lib/include/Board.hpp index de6a02c..7af4be4 100644 --- a/lib/include/Board.hpp +++ b/lib/include/Board.hpp @@ -22,11 +22,9 @@ public: pen = 5, }; - [[nodiscard]] static bool isWalkableForPacMan(Position point, double d, Direction direction) ; - - [[nodiscard]] static bool isWalkableForGost(Position point, Position origin, bool isEyes) ; - [[nodiscard]] static bool isWalkable(Position point) ; - [[nodiscard]] static bool isInPen(Position point) ; + [[nodiscard]] static bool isWalkableForPacMan(GridPosition point); + [[nodiscard]] static bool isWalkableForGost(GridPosition point, GridPosition origin, bool isEyes) ; + [[nodiscard]] static bool isInPen(GridPosition point) ; [[nodiscard]] static std::vector initialPelletPositions() ; @@ -49,7 +47,7 @@ public: static Position clydeScatterTarget() { return { 0, 30 }; } private: - [[nodiscard]] static bool isWalkable(Position point, double d, Direction direction, bool pacman) ; + [[nodiscard]] static Cell cellAtPosition(GridPosition point); static std::array< std::array, ROWS> diff --git a/lib/include/Ghost.hpp b/lib/include/Ghost.hpp index 7aad475..647c8b5 100644 --- a/lib/include/Ghost.hpp +++ b/lib/include/Ghost.hpp @@ -21,7 +21,7 @@ public: [[nodiscard]] Position position() const; - [[nodiscard]] Position positionInGrid() const; + [[nodiscard]] GridPosition positionInGrid() const; void update(std::chrono::milliseconds time_delta, const Board & board); void frighten(); @@ -48,7 +48,7 @@ protected: Position pos; Position startingPosition; Position scatterTarget; - Position lastIntersection = { -1, -1 }; + GridPosition lastIntersection = { -1, -1 }; bool isInPen(const Board & board) const; }; diff --git a/lib/include/PacMan.hpp b/lib/include/PacMan.hpp index c150dba..d63134f 100644 --- a/lib/include/PacMan.hpp +++ b/lib/include/PacMan.hpp @@ -17,7 +17,7 @@ public: [[nodiscard]] Position position() const; - [[nodiscard]] Position positionInGrid() const; + [[nodiscard]] GridPosition positionInGrid() const; void update(std::chrono::milliseconds time_delta, InputState state, const Board & board); diff --git a/lib/include/Pellets.hpp b/lib/include/Pellets.hpp index 20e9e1e..0790411 100644 --- a/lib/include/Pellets.hpp +++ b/lib/include/Pellets.hpp @@ -15,7 +15,7 @@ public: return positions; } - bool eatPelletAtPosition(Position p); + bool eatPelletAtPosition(GridPosition p); private: const GridPosition sprite = { 1, 9 }; diff --git a/lib/include/Position.hpp b/lib/include/Position.hpp index 0e44cb9..08dc616 100644 --- a/lib/include/Position.hpp +++ b/lib/include/Position.hpp @@ -16,7 +16,11 @@ using Rect = sf::Rect; using Sprite = sf::Sprite; -inline bool operator==(const GridPosition & b, const Position & a) { +inline bool operator==(const GridPosition & a, const GridPosition & b) { + return a.x == b.x && a.y == b.y; +} + +inline bool operator==(const GridPosition & a, const Position & b) { return a.x == b.x && a.y == b.y; } diff --git a/lib/include/SuperPellets.hpp b/lib/include/SuperPellets.hpp index d5e736a..7f02d31 100644 --- a/lib/include/SuperPellets.hpp +++ b/lib/include/SuperPellets.hpp @@ -15,7 +15,7 @@ public: return positions; } - bool eatPelletAtPosition(Position p); + bool eatPelletAtPosition(GridPosition p); private: const GridPosition sprite = { 0, 9 };