Skip to content

Latest commit

 

History

History
138 lines (98 loc) · 3.71 KB

21_exercises.md

File metadata and controls

138 lines (98 loc) · 3.71 KB

< Back

21. std::array and ranged for

Exercise 210

Implement PacManAI::isValidMove

Background: PacMan Moves

At each intersection, check if there is a ghost directly inline with that path. If the path is free of ghosts, you are allowed to turn there. And if PacMan is moving in a direction, and a ghost enters his path, then PacMan will reverse.

For example if PacMan is at an intersection and can go either right or up, and there is a ghost in the path going right, then PacMan will go up. Then while PacMan is going up, a ghost enters that path, PacMan will go back.

You only need to worry about the grid itself and any ghosts on the North/South/East/West axis of PacMan.

Exercise

As a warmup exercise, implement PacManAI::isValidMove and test your implementation with the test in testPacmanAI.cpp called "Is valid move"

Note: isValidMove will be called 4 times in a loop. If the target cell is not walkable the distance is set to infinity.

Note: Going backwards is not allowed, otherwise you'll go back and forwards between 2 pellets while playing the game.

Note: The test is disabled, to enable the test remove the [!shouldfail] from the tags on the test

Note: In the testPacmanAI.cpp test called "Is valid move" a Catch2 feature is used called a Data Generator. This lets you reuse the test across different input values, see the Catch2 documentation for Data Generators.

bool PacManAI::isValidMove(const Move & move) {
  return false;
}
Hint 1

Use isWalkableForPacMan to make sure PacMan is not walking in ways that are not legal

Hint 2

Use oppositeDirection to make sure PacMan doesn't get stuck toggeling back and forth

Solution
bool PacManAI::isValidMove(const Move & move) {
  const bool isOpposite = (move.direction == oppositeDirection(suggested_direction));
  if (isOpposite) {
    return false;
  }

  const bool canWalk = isWalkableForPacMan(move.position);
  if (!canWalk) {
    return false;
  }
  return true;
}

Exercise 211

Implement PacManAI::optimalDirection

Background: PacMan Moves

At each intersection, check if there is a ghost directly inline with that path. If the path is free of ghosts, you are allowed to turn there. And if PacMan is moving in a direction, and a ghost enters his path, then PacMan will reverse.

For example if PacMan is at an intersection and can go either right or up, and there is a ghost in the path going right, then PacMan will go up. Then while PacMan is going up, a ghost enters that path, PacMan will go back.

You only need to worry about the grid itself and any ghosts on the North/South/East/West axis of PacMan.

Exercise

Implement PacManAI::optimalDirection and test your implementation with the test in testPacmanAI.cpp called "Is optimal direction"

Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves) {
  return Direction::NONE;
}
Solution
Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves) {
  double closestDistance = std::numeric_limits<double>::infinity();
  Direction dir = Direction::LEFT;

  for (const auto & move : moves) {
    if (move.distanceToTarget < closestDistance) {
      closestDistance = move.distanceToTarget;
      dir = move.direction;
    }
  }
  return dir;
}