From 79506d218ecbf21ecc969f21deca29c4fb645916 Mon Sep 17 00:00:00 2001 From: Corentin Jabot Date: Thu, 24 Jun 2021 11:40:49 +0200 Subject: [PATCH] Make a ghost pen --- lib/Board.cpp | 49 +++++++++++++++++++++++++++++++------------------ lib/Board.hpp | 4 +++- lib/PacMan.cpp | 4 ++-- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/lib/Board.cpp b/lib/Board.cpp index 23cb6d1..f9429ed 100644 --- a/lib/Board.cpp +++ b/lib/Board.cpp @@ -6,6 +6,7 @@ // 2 - nothing // 3 - door // 4 - superpower +// 5 - pen doors static const uint8_t board[ROWS][COLUMNS] = { // 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 @@ -21,10 +22,10 @@ static const uint8_t board[ROWS][COLUMNS] = { { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 9 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 10 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 11 - { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 3, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 12 - { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 13 - { 3, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 3 }, // 14 - { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 15 + { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 5, 5, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 12 + { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 13 + { 3, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 3 }, // 14 + { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 15 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 16 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 17 { 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 18 @@ -48,23 +49,35 @@ Board::Board() { board_state[row][column] = board[row][column]; } -bool Board::isWalkable(Position point, float position_delta, Direction direction) const { +bool Board::isWalkableForPacMan(Position point, float d, Direction direction) const { + return isWalkable(point, d, direction, true); +} + +bool Board::isWalkableForGhost(Position point, float d, Direction direction) const { + return isWalkable(point, d, direction, false); +} + +bool Board::isWalkable(Position point, float position_delta, Direction direction, bool pacman) const { if (point.x <= 0 || point.x >= COLUMNS - 1) return true; - switch (direction) { - case Direction::LEFT: - return board_state[int(point.y)][int(point.x - position_delta)] != 0; - case Direction::RIGHT: - return board_state[int(point.y)][int(point.x) + 1] != 0; - case Direction::UP: - return board_state[int(point.y - position_delta)][int(point.x)] != 0; - case Direction::DOWN: - return board_state[int(point.y) + 1][int(point.x)] != 0; - case Direction::NONE: - default: - return true; - } + auto cellAtPosition = [&](Position point, float position_delta, Direction direction) { + switch (direction) { + case Direction::LEFT: + return board_state[int(point.y)][int(point.x - position_delta)]; + case Direction::RIGHT: + return board_state[int(point.y)][int(point.x) + 1]; + case Direction::UP: + return board_state[int(point.y - position_delta)][int(point.x)]; + case Direction::DOWN: + return board_state[int(point.y) + 1][int(point.x)]; + case Direction::NONE: + default: + return uint8_t(0); + } + }; + auto cell = cellAtPosition(point, position_delta, direction); + return pacman ? cell != 0 : cell != 0 && cell != 5; } std::vector Board::initialPelletPositions() const { diff --git a/lib/Board.hpp b/lib/Board.hpp index 2b6a9d8..90fd82f 100644 --- a/lib/Board.hpp +++ b/lib/Board.hpp @@ -14,7 +14,8 @@ class Board { public: Board(); - [[nodiscard]] bool isWalkable(Position point, float d, Direction direction) const; + [[nodiscard]] bool isWalkableForPacMan(Position point, float d, Direction direction) const; + [[nodiscard]] bool isWalkableForGhost(Position point, float d, Direction direction) const; [[nodiscard]] std::vector initialPelletPositions() const; @@ -23,5 +24,6 @@ public: static Position initialPacManPosition() { return { 14, 23 }; } private: + [[nodiscard]] bool isWalkable(Position point, float d, Direction direction, bool pacman) const; uint8_t board_state[ROWS][COLUMNS]{}; }; diff --git a/lib/PacMan.cpp b/lib/PacMan.cpp index 0164a2a..8158640 100644 --- a/lib/PacMan.cpp +++ b/lib/PacMan.cpp @@ -55,11 +55,11 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar pos.x = COLUMNS; } - else if (board.isWalkable(pos, position_delta, desired_direction)) { + else if (board.isWalkableForPacMan(pos, position_delta, desired_direction)) { direction = desired_direction; } - if (board.isWalkable(pos, position_delta, direction)) { + if (board.isWalkableForPacMan(pos, position_delta, direction)) { switch (direction) { case Direction::NONE: break;