pacman/lib/Board.cpp

110 lines
5.3 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
2021-07-01 12:33:59 +00:00
std::array<std::array<int, COLUMNS>, ROWS> Board::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, 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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, 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
}};
2020-11-27 13:10:09 +00:00
2021-07-05 09:40:10 +00:00
bool Board::isWalkableForPacMan(Position point, double d, Direction direction) {
2021-06-24 11:32:52 +00:00
return isWalkable(point, d, direction, true);
2021-06-24 09:40:49 +00:00
}
2021-07-05 09:40:10 +00:00
bool Board::isWalkableForGhost(Position point, double d, Direction direction) {
2021-06-24 11:32:52 +00:00
return isWalkable(point, d, direction, false);
2021-06-24 09:40:49 +00:00
}
2021-07-01 12:33:59 +00:00
bool Board::isWalkable(Position point) {
return board[int(point.y)][int(point.x)] != int(Cell::wall);
}
2021-07-01 12:33:59 +00:00
bool Board::isWalkableForGost(Position point, Position origin, bool isEyes) {
2021-06-28 10:42:21 +00:00
return isWalkable(point) && (isEyes || (isInPen(origin) || !isInPen(point)));
}
2021-07-01 12:33:59 +00:00
bool Board::isInPen(Position point) {
return board[int(point.y)][int(point.x)] == int(Cell::pen);
}
2021-07-05 09:40:10 +00:00
bool Board::isWalkable(Position point, double position_delta, Direction direction, bool pacman) {
if (point.x <= 0 || point.x >= COLUMNS - 1)
return true;
2021-07-05 09:40:10 +00:00
auto cellAtPosition = [&](Position point, double position_delta, Direction direction) {
2021-06-24 11:32:52 +00:00
switch (direction) {
case Direction::LEFT:
2021-07-01 12:33:59 +00:00
return board[int(point.y)][int(point.x - position_delta)];
2021-06-24 11:32:52 +00:00
case Direction::RIGHT:
2021-07-01 12:33:59 +00:00
return board[int(point.y)][int(point.x) + 1];
2021-06-24 11:32:52 +00:00
case Direction::UP:
2021-07-01 12:33:59 +00:00
return board[int(point.y - position_delta)][int(point.x)];
2021-06-24 11:32:52 +00:00
case Direction::DOWN:
2021-07-01 12:33:59 +00:00
return board[int(point.y) + 1][int(point.x)];
2021-06-24 11:32:52 +00:00
case Direction::NONE:
default:
2021-07-01 12:33:59 +00:00
return int(Cell::wall);
2021-06-24 11:32:52 +00:00
}
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
}
2021-07-01 12:33:59 +00:00
std::vector<PositionInt> Board::initialPelletPositions() {
std::vector<PositionInt> positions;
2021-07-01 12:33:59 +00:00
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
if (board[row][column] == int(Cell::pellet))
positions.push_back({ column, row });
2020-11-27 13:10:09 +00:00
}
}
return positions;
}
2021-07-01 12:33:59 +00:00
std::vector<PositionInt> Board::initialSuperPelletPositions() {
std::vector<PositionInt> positions;
2021-07-01 12:33:59 +00:00
for (int row = 0; row < ROWS; row++) {
for (int column = 0; column < COLUMNS; column++) {
if (board[row][column] == int(Cell::power_pellet))
positions.push_back({ column, row });
2020-11-27 13:10:09 +00:00
}
}
return positions;
}