Make a ghost pen

This commit is contained in:
Corentin Jabot 2021-06-24 11:40:49 +02:00 committed by Patricia Aas
parent a0780c7a8c
commit 79506d218e
3 changed files with 36 additions and 21 deletions

View file

@ -6,6 +6,7 @@
// 2 - nothing
// 3 - door
// 4 - superpower
// 5 - pen doors
static const uint8_t board[ROWS][COLUMNS] = {
// 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
@ -21,10 +22,10 @@ static const uint8_t board[ROWS][COLUMNS] = {
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 9
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 10
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 11
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 3, 3, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 12
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 13
{ 3, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 3 }, // 14
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 15
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 5, 5, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 12
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 13
{ 3, 2, 2, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2, 2, 2, 3 }, // 14
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 2, 2, 2, 2, 2, 2, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 15
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 16
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 17
{ 0, 0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, // 18
@ -48,23 +49,35 @@ Board::Board() {
board_state[row][column] = board[row][column];
}
bool Board::isWalkable(Position point, float position_delta, Direction direction) const {
bool Board::isWalkableForPacMan(Position point, float d, Direction direction) const {
return isWalkable(point, d, direction, true);
}
bool Board::isWalkableForGhost(Position point, float d, Direction direction) const {
return isWalkable(point, d, direction, false);
}
bool Board::isWalkable(Position point, float position_delta, Direction direction, bool pacman) const {
if (point.x <= 0 || point.x >= COLUMNS - 1)
return true;
auto cellAtPosition = [&](Position point, float position_delta, Direction direction) {
switch (direction) {
case Direction::LEFT:
return board_state[int(point.y)][int(point.x - position_delta)] != 0;
return board_state[int(point.y)][int(point.x - position_delta)];
case Direction::RIGHT:
return board_state[int(point.y)][int(point.x) + 1] != 0;
return board_state[int(point.y)][int(point.x) + 1];
case Direction::UP:
return board_state[int(point.y - position_delta)][int(point.x)] != 0;
return board_state[int(point.y - position_delta)][int(point.x)];
case Direction::DOWN:
return board_state[int(point.y) + 1][int(point.x)] != 0;
return board_state[int(point.y) + 1][int(point.x)];
case Direction::NONE:
default:
return true;
return uint8_t(0);
}
};
auto cell = cellAtPosition(point, position_delta, direction);
return pacman ? cell != 0 : cell != 0 && cell != 5;
}
std::vector<PositionInt> Board::initialPelletPositions() const {

View file

@ -14,7 +14,8 @@ class Board {
public:
Board();
[[nodiscard]] bool isWalkable(Position point, float d, Direction direction) const;
[[nodiscard]] bool isWalkableForPacMan(Position point, float d, Direction direction) const;
[[nodiscard]] bool isWalkableForGhost(Position point, float d, Direction direction) const;
[[nodiscard]] std::vector<PositionInt> initialPelletPositions() const;
@ -23,5 +24,6 @@ public:
static Position initialPacManPosition() { return { 14, 23 }; }
private:
[[nodiscard]] bool isWalkable(Position point, float d, Direction direction, bool pacman) const;
uint8_t board_state[ROWS][COLUMNS]{};
};

View file

@ -55,11 +55,11 @@ void PacMan::updateMazePosition(std::chrono::milliseconds time_delta, const Boar
pos.x = COLUMNS;
}
else if (board.isWalkable(pos, position_delta, desired_direction)) {
else if (board.isWalkableForPacMan(pos, position_delta, desired_direction)) {
direction = desired_direction;
}
if (board.isWalkable(pos, position_delta, direction)) {
if (board.isWalkableForPacMan(pos, position_delta, direction)) {
switch (direction) {
case Direction::NONE:
break;