Get rid of board class

This commit is contained in:
Corentin Jabot 2021-07-08 17:42:24 +02:00
parent e5e46a0e65
commit 370a57f454
6 changed files with 57 additions and 72 deletions

View file

@ -2,6 +2,18 @@
namespace pacman { namespace pacman {
const std::size_t ROWS = 31;
const std::size_t COLUMNS = 28;
enum class Cell {
wall = 0,
pellet = 1,
nothing = 2,
door = 3,
power_pellet = 4,
pen = 5,
};
// Legend // Legend
// 0 - wall // 0 - wall
// 1 - pellet // 1 - pellet
@ -11,7 +23,7 @@ namespace pacman {
// 5 - pen doors // 5 - pen doors
// clang-format off // clang-format off
std::array<std::array<int, COLUMNS>, ROWS> Board::board = {{ constexpr std::array<std::array<int, COLUMNS>, ROWS> 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 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 { 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
{ 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, // 1 { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 }, // 1
@ -47,28 +59,32 @@ std::array<std::array<int, COLUMNS>, ROWS> Board::board = {{
}}; }};
// clang-format on // clang-format on
bool Board::isWalkableForPacMan(GridPosition point) { static Cell cellAtPosition(GridPosition point) {
if (point.x >= COLUMNS || point.y >= ROWS)
return Cell::wall;
return Cell(board[point.y][point.x]);
}
bool isWalkableForPacMan(GridPosition point) {
return cellAtPosition(point) != Cell::wall; return cellAtPosition(point) != Cell::wall;
} }
bool Board::isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) { bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) {
Cell cell = cellAtPosition(point); Cell cell = cellAtPosition(point);
if (cell == Cell::wall) if (cell == Cell::wall)
return false; return false;
return isEyes || (isInPen(origin) || !isInPen(point)); return isEyes || (isInPen(origin) || !isInPen(point));
} }
bool Board::isInPen(GridPosition point) { bool isInPen(GridPosition point) {
return cellAtPosition(point) == Cell::pen; return cellAtPosition(point) == Cell::pen;
} }
Board::Cell Board::cellAtPosition(GridPosition point) { bool isPortal(GridPosition point) {
if (point.x >= COLUMNS || point.y >= ROWS) return cellAtPosition(point) == Cell::door;
return Cell::wall;
return Cell(board[point.y][point.x]);
} }
std::vector<GridPosition> Board::initialPelletPositions() { std::vector<GridPosition> initialPelletPositions() {
std::vector<GridPosition> positions; std::vector<GridPosition> positions;
for (std::size_t row = 0; row < ROWS; row++) { for (std::size_t row = 0; row < ROWS; row++) {
for (std::size_t column = 0; column < COLUMNS; column++) { for (std::size_t column = 0; column < COLUMNS; column++) {
@ -79,7 +95,7 @@ std::vector<GridPosition> Board::initialPelletPositions() {
return positions; return positions;
} }
std::vector<GridPosition> Board::initialSuperPelletPositions() { std::vector<GridPosition> initialSuperPelletPositions() {
std::vector<GridPosition> positions; std::vector<GridPosition> positions;
for (std::size_t row = 0; row < ROWS; row++) { for (std::size_t row = 0; row < ROWS; row++) {
for (std::size_t column = 0; column < COLUMNS; column++) { for (std::size_t column = 0; column < COLUMNS; column++) {

View file

@ -77,7 +77,7 @@ void Ghost::update(std::chrono::milliseconds time_delta) {
} }
bool Ghost::isInPen() const { bool Ghost::isInPen() const {
return pacman::Board::isInPen(positionInGrid()); return pacman::isInPen(positionInGrid());
} }
void Ghost::updatePosition(std::chrono::milliseconds time_delta) { void Ghost::updatePosition(std::chrono::milliseconds time_delta) {
@ -161,7 +161,7 @@ void Ghost::updateDirection() {
continue; continue;
const GridPosition grid_position = {size_t(move.position.x), size_t(move.position.y)}; const GridPosition grid_position = {size_t(move.position.x), size_t(move.position.y)};
const bool can_walk = pacman::Board::isWalkableForGhost(grid_position, current_grid_position, isEyes()); const bool can_walk = pacman::isWalkableForGhost(grid_position, current_grid_position, isEyes());
if (!can_walk) if (!can_walk)
continue; continue;
@ -181,8 +181,8 @@ Position Ghost::target() const {
if (state == State::Eyes) if (state == State::Eyes)
return startingPosition; return startingPosition;
if (pacman::Board::isInPen(positionInGrid())) if (pacman::isInPen(positionInGrid()))
return pacman::Board::penDoorPosition(); return pacman::penDoorPosition();
return scatterTarget; return scatterTarget;
} }
@ -196,19 +196,19 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) {
} }
Blinky::Blinky() Blinky::Blinky()
: Ghost(Atlas::Ghost::blinky, pacman::Board::initialBlinkyPosition(), pacman::Board::blinkyScatterTarget()) { : Ghost(Atlas::Ghost::blinky, pacman::initialBlinkyPosition(), pacman::blinkyScatterTarget()) {
} }
Speedy::Speedy() Speedy::Speedy()
: Ghost(Atlas::Ghost::speedy, pacman::Board::initialSpeedyPosition(), pacman::Board::speedyScatterTarget()) { : Ghost(Atlas::Ghost::speedy, pacman::initialSpeedyPosition(), pacman::speedyScatterTarget()) {
} }
Inky::Inky() Inky::Inky()
: Ghost(Atlas::Ghost::inky, pacman::Board::initialInkyPosition(), pacman::Board::inkyScatterTarget()) { : Ghost(Atlas::Ghost::inky, pacman::initialInkyPosition(), pacman::inkyScatterTarget()) {
} }
Clyde::Clyde() Clyde::Clyde()
: Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) { : Ghost(Atlas::Ghost::clyde, pacman::initialClydePosition(), pacman::clydeScatterTarget()) {
} }
} // namespace pacman } // namespace pacman

View file

@ -4,7 +4,7 @@
namespace pacman { namespace pacman {
PacMan::PacMan() PacMan::PacMan()
: pos(Board::initialPacManPosition()) {} : pos(initialPacManPosition()) {}
GridPosition PacMan::currentSprite() const { GridPosition PacMan::currentSprite() const {
return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction); return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction);
@ -28,7 +28,7 @@ void PacMan::eat() {
void PacMan::reset() { void PacMan::reset() {
eaten = false; eaten = false;
direction = Direction::NONE; direction = Direction::NONE;
pos = pacman::Board::initialPacManPosition(); pos = pacman::initialPacManPosition();
} }
void PacMan::update(std::chrono::milliseconds time_delta, Direction input_direction) { void PacMan::update(std::chrono::milliseconds time_delta, Direction input_direction) {
@ -54,17 +54,9 @@ void PacMan::updateAnimationPosition(std::chrono::milliseconds time_delta, bool
void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) { void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
// Handle teleport if(isPortal(positionInGrid())) {
const size_t right = COLUMNS - 1; // TODO: patricia
const size_t left = 0; }
if (std::size_t(pos.x) == right && direction == Direction::RIGHT) {
pos.x = left;
return;
} else if (std::size_t(pos.x) == left && direction == Direction::LEFT) {
pos.x = right;
return;
}
const double position_delta = 0.004 * time_delta.count(); const double position_delta = 0.004 * time_delta.count();
const auto pacman_size = 1; const auto pacman_size = 1;
@ -86,7 +78,7 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
}; };
auto canGo = [&](Direction move_direction) { auto canGo = [&](Direction move_direction) {
return pacman::Board::isWalkableForPacMan(moveToPosition(pos, move_direction)); return pacman::isWalkableForPacMan(moveToPosition(pos, move_direction));
}; };
if (canGo(desired_direction)) { if (canGo(desired_direction)) {

View file

@ -4,7 +4,7 @@
namespace pacman { namespace pacman {
Pellets::Pellets() Pellets::Pellets()
: positions(pacman::Board::initialPelletPositions()) {} : positions(initialPelletPositions()) {}
bool Pellets::eatPelletAtPosition(GridPosition p) { bool Pellets::eatPelletAtPosition(GridPosition p) {
auto it = std::find(positions.begin(), positions.end(), p); auto it = std::find(positions.begin(), positions.end(), p);

View file

@ -4,7 +4,7 @@
namespace pacman { namespace pacman {
SuperPellets::SuperPellets() SuperPellets::SuperPellets()
: positions(pacman::Board::initialSuperPelletPositions()) {} : positions(initialSuperPelletPositions()) {}
bool SuperPellets::eatPelletAtPosition(GridPosition p) { bool SuperPellets::eatPelletAtPosition(GridPosition p) {
auto it = std::find(positions.begin(), positions.end(), p); auto it = std::find(positions.begin(), positions.end(), p);

View file

@ -10,50 +10,27 @@
namespace pacman { namespace pacman {
const std::size_t ROWS = 31; [[nodiscard]] bool isWalkableForPacMan(GridPosition point);
const std::size_t COLUMNS = 28; [[nodiscard]] bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes);
[[nodiscard]] bool isInPen(GridPosition point);
class Board { [[nodiscard]] std::vector<GridPosition> initialPelletPositions();
public:
enum class Cell {
wall = 0,
pellet = 1,
nothing = 2,
door = 3,
power_pellet = 4,
pen = 5,
};
[[nodiscard]] static bool isWalkableForPacMan(GridPosition point); [[nodiscard]] std::vector<GridPosition> initialSuperPelletPositions();
[[nodiscard]] static bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes);
[[nodiscard]] static bool isInPen(GridPosition point);
[[nodiscard]] static std::vector<GridPosition> initialPelletPositions(); inline Position penDoorPosition() { return { 13, 11 }; }
inline Position initialPacManPosition() { return { 13.5, 23 }; }
[[nodiscard]] static std::vector<GridPosition> initialSuperPelletPositions(); inline Position initialBlinkyPosition() { return { 13.5, 11 }; }
inline Position blinkyScatterTarget() { return { 25, -3 }; }
static Position initialPacManPosition() { return { 13.5, 23 }; } inline Position initialSpeedyPosition() { return { 11.5, 14 }; }
inline Position speedyScatterTarget() { return { 3, -2 }; }
static Position initialBlinkyPosition() { return { 13.5, 11 }; } inline Position initialInkyPosition() { return { 13.5, 14 }; }
static Position penDoorPosition() { return { 13, 11 }; } inline Position inkyScatterTarget() { return { 27, 30 }; }
static Position blinkyScatterTarget() { return { 25, -3 }; } inline Position initialClydePosition() { return { 15.5, 14 }; }
inline Position clydeScatterTarget() { return { 0, 30 }; }
static Position initialSpeedyPosition() { return { 11.5, 14 }; }
static Position speedyScatterTarget() { return { 3, -2 }; }
static Position initialInkyPosition() { return { 13.5, 14 }; }
static Position inkyScatterTarget() { return { 27, 30 }; }
static Position initialClydePosition() { return { 15.5, 14 }; }
static Position clydeScatterTarget() { return { 0, 30 }; }
private:
[[nodiscard]] static Cell cellAtPosition(GridPosition point);
static std::array<
std::array<int, COLUMNS>,
ROWS>
board;
};
} // namespace pacman } // namespace pacman