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 {
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
// 0 - wall
// 1 - pellet
@ -11,7 +23,7 @@ namespace pacman {
// 5 - pen doors
// 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, 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
@ -47,28 +59,32 @@ std::array<std::array<int, COLUMNS>, ROWS> Board::board = {{
}};
// 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;
}
bool Board::isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) {
bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) {
Cell cell = cellAtPosition(point);
if (cell == Cell::wall)
return false;
return isEyes || (isInPen(origin) || !isInPen(point));
}
bool Board::isInPen(GridPosition point) {
bool isInPen(GridPosition point) {
return cellAtPosition(point) == Cell::pen;
}
Board::Cell Board::cellAtPosition(GridPosition point) {
if (point.x >= COLUMNS || point.y >= ROWS)
return Cell::wall;
return Cell(board[point.y][point.x]);
bool isPortal(GridPosition point) {
return cellAtPosition(point) == Cell::door;
}
std::vector<GridPosition> Board::initialPelletPositions() {
std::vector<GridPosition> initialPelletPositions() {
std::vector<GridPosition> positions;
for (std::size_t row = 0; row < ROWS; row++) {
for (std::size_t column = 0; column < COLUMNS; column++) {
@ -79,7 +95,7 @@ std::vector<GridPosition> Board::initialPelletPositions() {
return positions;
}
std::vector<GridPosition> Board::initialSuperPelletPositions() {
std::vector<GridPosition> initialSuperPelletPositions() {
std::vector<GridPosition> positions;
for (std::size_t row = 0; row < ROWS; row++) {
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 {
return pacman::Board::isInPen(positionInGrid());
return pacman::isInPen(positionInGrid());
}
void Ghost::updatePosition(std::chrono::milliseconds time_delta) {
@ -161,7 +161,7 @@ void Ghost::updateDirection() {
continue;
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)
continue;
@ -181,8 +181,8 @@ Position Ghost::target() const {
if (state == State::Eyes)
return startingPosition;
if (pacman::Board::isInPen(positionInGrid()))
return pacman::Board::penDoorPosition();
if (pacman::isInPen(positionInGrid()))
return pacman::penDoorPosition();
return scatterTarget;
}
@ -196,19 +196,19 @@ void Ghost::updateAnimation(std::chrono::milliseconds time_delta) {
}
Blinky::Blinky()
: Ghost(Atlas::Ghost::blinky, pacman::Board::initialBlinkyPosition(), pacman::Board::blinkyScatterTarget()) {
: Ghost(Atlas::Ghost::blinky, pacman::initialBlinkyPosition(), pacman::blinkyScatterTarget()) {
}
Speedy::Speedy()
: Ghost(Atlas::Ghost::speedy, pacman::Board::initialSpeedyPosition(), pacman::Board::speedyScatterTarget()) {
: Ghost(Atlas::Ghost::speedy, pacman::initialSpeedyPosition(), pacman::speedyScatterTarget()) {
}
Inky::Inky()
: Ghost(Atlas::Ghost::inky, pacman::Board::initialInkyPosition(), pacman::Board::inkyScatterTarget()) {
: Ghost(Atlas::Ghost::inky, pacman::initialInkyPosition(), pacman::inkyScatterTarget()) {
}
Clyde::Clyde()
: Ghost(Atlas::Ghost::clyde, pacman::Board::initialClydePosition(), pacman::Board::clydeScatterTarget()) {
: Ghost(Atlas::Ghost::clyde, pacman::initialClydePosition(), pacman::clydeScatterTarget()) {
}
} // namespace pacman

View File

@ -4,7 +4,7 @@
namespace pacman {
PacMan::PacMan()
: pos(Board::initialPacManPosition()) {}
: pos(initialPacManPosition()) {}
GridPosition PacMan::currentSprite() const {
return eaten ? pacManAnimation.deathAnimationFrame() : pacManAnimation.animationFrame(direction);
@ -28,7 +28,7 @@ void PacMan::eat() {
void PacMan::reset() {
eaten = false;
direction = Direction::NONE;
pos = pacman::Board::initialPacManPosition();
pos = pacman::initialPacManPosition();
}
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) {
// Handle teleport
const size_t right = COLUMNS - 1;
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;
}
if(isPortal(positionInGrid())) {
// TODO: patricia
}
const double position_delta = 0.004 * time_delta.count();
const auto pacman_size = 1;
@ -86,7 +78,7 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta) {
};
auto canGo = [&](Direction move_direction) {
return pacman::Board::isWalkableForPacMan(moveToPosition(pos, move_direction));
return pacman::isWalkableForPacMan(moveToPosition(pos, move_direction));
};
if (canGo(desired_direction)) {

View File

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

View File

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

View File

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