pacman/lib/Board.cpp

117 lines
5.5 KiB
C++
Raw Normal View History

#include "Board.hpp"
2020-11-27 13:10:09 +00:00
// Legend
// 0 - wall
// 1 - pellet
// 2 - nothing
// 3 - door
// 4 - superpower
2021-06-24 09:40:49 +00:00
// 5 - pen doors
2020-11-27 13:10:09 +00:00
static const uint8_t board[ROWS][COLUMNS] = {
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, 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, 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 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
2020-11-27 13:10:09 +00:00
};
Board::Board() {
for (uint8_t row = 0; row < ROWS; row++)
for (uint8_t column = 0; column < COLUMNS; column++)
board_state[row][column] = board[row][column];
}
2021-06-24 09:40:49 +00:00
bool Board::isWalkableForPacMan(Position point, float d, Direction direction) const {
2021-06-24 11:32:52 +00:00
return isWalkable(point, d, direction, true);
2021-06-24 09:40:49 +00:00
}
bool Board::isWalkableForGhost(Position point, float d, Direction direction) const {
2021-06-24 11:32:52 +00:00
return isWalkable(point, d, direction, false);
2021-06-24 09:40:49 +00:00
}
bool Board::isWalkable(Position point) const {
return board_state[int(point.y)][int(point.x)] != uint8_t(Cell::wall);
}
bool Board::isWalkableForGost(Position point, Position origin) const
{
return isWalkable(point) && (isInPen(origin) || !isInPen(point));
}
bool Board::isInPen(Position point) const {
return board_state[int(point.y)][int(point.x)] == uint8_t(Cell::pen);
}
2021-06-24 09:40:49 +00:00
bool Board::isWalkable(Position point, float position_delta, Direction direction, bool pacman) const {
if (point.x <= 0 || point.x >= COLUMNS - 1)
return true;
2021-06-24 09:40:49 +00:00
auto cellAtPosition = [&](Position point, float position_delta, Direction direction) {
2021-06-24 11:32:52 +00:00
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(Cell::wall);
}
2021-06-24 09:40:49 +00:00
};
2021-06-24 11:32:52 +00:00
Cell cell = Cell(cellAtPosition(point, position_delta, direction));
return pacman ? cell != Cell::wall : cell != Cell::wall && cell != Cell::pen;
2020-11-27 13:10:09 +00:00
}
std::vector<PositionInt> Board::initialPelletPositions() const {
std::vector<PositionInt> positions;
2020-11-27 13:10:09 +00:00
for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t column = 0; column < COLUMNS; column++) {
2021-06-24 11:32:52 +00:00
if (board_state[row][column] == uint8_t(Cell::pellet))
positions.push_back({ column, row });
2020-11-27 13:10:09 +00:00
}
}
return positions;
}
std::vector<PositionInt> Board::initialSuperPelletPositions() const {
std::vector<PositionInt> positions;
2020-11-27 13:10:09 +00:00
for (uint8_t row = 0; row < ROWS; row++) {
for (uint8_t column = 0; column < COLUMNS; column++) {
2021-06-24 11:32:52 +00:00
if (board_state[row][column] == uint8_t(Cell::power_pellet))
positions.push_back({ column, row });
2020-11-27 13:10:09 +00:00
}
}
return positions;
}