pacman/lib/Board.cpp

128 lines
5.6 KiB
C++
Raw Normal View History

#include "Board.hpp"
2020-11-27 13:10:09 +00:00
2021-07-05 12:10:01 +00:00
namespace pacman {
2021-07-08 15:42:24 +00:00
const std::size_t ROWS = 31;
const std::size_t COLUMNS = 28;
enum class Cell {
wall = 0,
pellet = 1,
2021-08-02 14:43:18 +00:00
// nothing = 2,
// missing 3,
2021-07-08 15:42:24 +00:00
power_pellet = 4,
pen = 5,
2021-07-08 16:00:39 +00:00
left_portal = 6,
right_portal = 7
2021-07-08 15:42:24 +00:00
};
2021-07-05 11:54:54 +00:00
// clang-format off
2021-07-08 15:42:24 +00:00
constexpr std::array<std::array<int, COLUMNS>, ROWS> board = {{
2021-06-24 11:32:52 +00:00
// 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
2021-06-16 11:14:47 +00:00
{ 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, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, // 2
{ 0, 4, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 4, 0 }, // 3
{ 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, // 4
{ 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 }, // 5
{ 0, 1, 0, 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 0, 0, 1, 0 }, // 27
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, // 28
{ 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
2021-07-01 12:33:59 +00:00
}};
2021-07-05 11:54:54 +00:00
// clang-format on
2020-11-27 13:10:09 +00:00
2021-07-08 15:42:24 +00:00
static Cell cellAtPosition(GridPosition point) {
if (point.x < 0 || point.x >= COLUMNS || point.y < 0 || point.y >= ROWS)
2021-07-08 15:42:24 +00:00
return Cell::wall;
return Cell(board[point.y][point.x]);
}
bool isWalkableForPacMan(GridPosition point) {
2021-07-05 11:54:54 +00:00
return cellAtPosition(point) != Cell::wall;
2021-06-24 09:40:49 +00:00
}
2021-07-08 15:42:24 +00:00
bool isWalkableForGhost(GridPosition point, GridPosition origin, bool isEyes) {
const Cell cell = cellAtPosition(point);
2021-07-05 11:54:54 +00:00
if (cell == Cell::wall)
return false;
return isEyes || isInPen(origin) || !isInPen(point);
}
2021-07-08 15:42:24 +00:00
bool isInPen(GridPosition point) {
2021-07-05 11:54:54 +00:00
return cellAtPosition(point) == Cell::pen;
}
2021-07-08 16:00:39 +00:00
bool isPortal(GridPosition point, Direction direction) {
return (cellAtPosition(point) == Cell::left_portal && direction == Direction::LEFT) ||
(cellAtPosition(point) == Cell::right_portal && direction == Direction::RIGHT);
}
GridPosition teleport(GridPosition point) {
size_t right = COLUMNS - 1;
size_t left = 0;
if (point.x == left)
point.x = right;
else if (point.x == right)
point.x = left;
return point;
2020-11-27 13:10:09 +00:00
}
2021-07-08 15:42:24 +00:00
std::vector<GridPosition> initialPelletPositions() {
2021-07-05 09:46:49 +00:00
std::vector<GridPosition> positions;
2021-07-16 07:19:10 +00:00
positions.reserve(4); // Reserve space for 4 supper pellets
2021-07-05 11:54:54 +00:00
for (std::size_t row = 0; row < ROWS; row++) {
for (std::size_t column = 0; column < COLUMNS; column++) {
2021-07-01 12:33:59 +00:00
if (board[row][column] == int(Cell::pellet))
2021-07-06 08:07:35 +00:00
positions.emplace_back(column, row);
2020-11-27 13:10:09 +00:00
}
}
return positions;
}
2021-07-08 15:42:24 +00:00
std::vector<GridPosition> initialSuperPelletPositions() {
2021-07-05 09:46:49 +00:00
std::vector<GridPosition> positions;
2021-07-16 07:19:10 +00:00
// To avoid having to resize the vector too many times, we can
// presemptively reserve space for elements.
// At a glance, we can estimate that there are pellets on a third of the grid.
// This is not accurate so we might either reserve too much or too little memory,
// but this is will still be faster than not calling reserve at all!
// This will be very noticeable on both very large vectors (10 thousands of elements),
// Or small vectors in functions called very frequently.
positions.reserve((ROWS * COLUMNS) / 3);
2021-07-05 11:54:54 +00:00
for (std::size_t row = 0; row < ROWS; row++) {
for (std::size_t column = 0; column < COLUMNS; column++) {
2021-07-01 12:33:59 +00:00
if (board[row][column] == int(Cell::power_pellet))
2021-07-06 08:07:35 +00:00
positions.emplace_back(column, row);
2020-11-27 13:10:09 +00:00
}
}
return positions;
}
2021-07-05 12:10:01 +00:00
} // namespace pacman