#include "Board.hpp" namespace pacman { const std::size_t ROWS = 31; const std::size_t COLUMNS = 28; enum class Cell { wall = 0, pellet = 1, // nothing = 2, // missing 3, power_pellet = 4, pen = 5, left_portal = 6, right_portal = 7 }; // clang-format off constexpr std::array, 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 { 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, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, // 6 { 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0 }, // 7 { 0, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0 }, // 8 { 0, 0, 0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cell::wall; return Cell(board[point.y][point.x]); } bool isWall(GridPosition point) { return cellAtPosition(point) == Cell::wall; } bool isWalkableForPacMan(GridPosition point) { return !isWall(point) && !isInPen(point); } bool isWalkableForGhost(GridPosition target_position, GridPosition current_position, bool isEyes) { if (isWall(target_position)) return false; return isEyes || isInPen(current_position) || !isInPen(target_position); } bool isInPen(GridPosition point) { return cellAtPosition(point) == Cell::pen; } bool isPortal(GridPosition point, Direction direction) { return (cellAtPosition(point) == Cell::left_portal && direction == Direction::LEFT) || (cellAtPosition(point) == Cell::right_portal && direction == Direction::RIGHT); } bool isIntersection(GridPosition point) { if (!isWalkableForPacMan(point) || cellAtPosition(point) == Cell::left_portal || cellAtPosition(point) == Cell::right_portal) { return false; } const GridPosition right{ point.x + 1, point.y }; const bool rightWalkable = isWalkableForPacMan(right); const GridPosition left{ point.x - 1, point.y }; const bool leftWalkable = isWalkableForPacMan(left); const GridPosition top{ point.x, point.y - 1 }; const bool topWalkable = isWalkableForPacMan(top); const GridPosition bottom{ point.x, point.y + 1 }; const bool bottomWalkable = isWalkableForPacMan(bottom); return (topWalkable && rightWalkable) || (rightWalkable && bottomWalkable) || (bottomWalkable && leftWalkable) || (leftWalkable && topWalkable); } 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; } std::vector initialPelletPositions() { std::vector positions; positions.reserve(4); // Reserve space for 4 supper pellets 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.emplace_back(column, row); } } return positions; } std::vector initialSuperPelletPositions() { std::vector positions; // 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); 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.emplace_back(column, row); } } return positions; } } // namespace pacman