Simplify ghost movement code

This commit is contained in:
Corentin Jabot 2021-06-28 10:24:38 +02:00 committed by Patricia Aas
parent c2cfb8c734
commit 3e84746cee

View file

@ -57,30 +57,33 @@ void Ghost::updateDirection(const Board & board) {
if(cell == lastIntersection) if(cell == lastIntersection)
return; return;
using P = std::tuple<Direction, Position, double>; struct NewDirection {
Direction direction;
Position position;
double distance;
};
auto [x , y] = cell; auto [x , y] = cell;
std::array<P, 4> positions = {{ std::array directions = {
P{Direction::UP, {x, y-1}, 0}, NewDirection{Direction::UP, {x, y-1}, 0},
P{Direction::LEFT, {x-1, y}, 0}, NewDirection{Direction::LEFT, {x-1, y}, 0},
P{Direction::DOWN, {x, y+1}, 0}, NewDirection{Direction::DOWN, {x, y+1}, 0},
P{Direction::RIGHT, {x+1, y}, 0} NewDirection{Direction::RIGHT, {x+1, y}, 0}
}}; };
const Position target = this->target(board); const Position target = this->target(board);
std::for_each(positions.begin(), positions.end(), [&](P & p) { for(auto && d : directions) {
get<2>(p) = (get<0>(p) != oppositeDirection(direction) && board.isWalkableForGost(get<1>(p), cell)) ? d.distance = (d.direction != oppositeDirection(direction) && board.isWalkableForGost(d.position, cell)) ?
std::hypot(get<1>(p).x - target.x, get<1>(p).y - target.y) std::hypot(d.position.x - target.x, d.position.y - target.y)
: std::numeric_limits<double>::infinity(); : std::numeric_limits<double>::infinity();
}); }
auto it = std::min_element(positions.begin(), positions.end(), [](const auto & a, const auto &b) auto it = std::min_element(directions.begin(), directions.end(), [](const auto & a, const auto &b) {
{ return a.distance < b.distance;
return get<2>(a) < get<2>(b);
}); });
lastIntersection = cell; lastIntersection = cell;
direction = std::get<0>(*it); direction = it->direction;
} }
Position Ghost::target(const Board & board) const { Position Ghost::target(const Board & board) const {