2021-10-05 11:48:15 +00:00
|
|
|
[< Back](../README.md)
|
2021-10-05 11:44:51 +00:00
|
|
|
|
2021-09-13 13:07:04 +00:00
|
|
|
# Exercise: PacMan AI
|
|
|
|
|
2021-10-05 13:30:50 +00:00
|
|
|
Let's implement a naive AI for PacMan.
|
2021-09-13 13:07:04 +00:00
|
|
|
|
2021-10-05 13:30:50 +00:00
|
|
|
## Background: PacMan Moves
|
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
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.
|
2021-09-13 13:07:04 +00:00
|
|
|
|
2021-10-05 08:37:48 +00:00
|
|
|
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.
|
2021-09-13 13:07:04 +00:00
|
|
|
|
|
|
|
You only need to worry about the grid itself and any ghosts on the North/South/East/West axis of PacMan.
|
2021-10-05 13:30:50 +00:00
|
|
|
|
|
|
|
## Exercise
|
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
1. Implement [PacManAI::pelletClosestToPacman](../../../lib/PacManAI.cpp) and test your implementation with the test
|
2021-10-05 13:30:50 +00:00
|
|
|
in [testPacmanAI.cpp](../../../test/testPacmanAI.cpp) called _"Find pellet closest to pacman"_
|
2021-10-05 13:46:44 +00:00
|
|
|
|
2021-10-05 14:00:40 +00:00
|
|
|
The function should return the position of the pellet that is "closest" to PacMan. One implementation could be
|
|
|
|
to sort the vector of pellets by the distance they have to PacMan, and then return the first one.
|
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
```cpp
|
|
|
|
GridPosition PacManAI::pelletClosestToPacman(GridPosition pacmanGridPosition,
|
|
|
|
std::vector<GridPosition> & pellets) {
|
|
|
|
return {0, 0};
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-05 14:10:24 +00:00
|
|
|
<details>
|
|
|
|
<summary>Hint 1</summary>
|
|
|
|
|
|
|
|
Use the `positionDistance` function to find the distance to PacMan.
|
2021-10-05 13:46:44 +00:00
|
|
|
|
2021-10-05 14:10:24 +00:00
|
|
|
</details>
|
|
|
|
<details>
|
|
|
|
<summary>Hint 2</summary>
|
|
|
|
|
|
|
|
Use the [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) function to sort the vector.
|
|
|
|
|
|
|
|
</details>
|
2021-10-05 13:46:44 +00:00
|
|
|
|
2021-10-05 14:10:24 +00:00
|
|
|
2. Implement [PacManAI::isValidMove](../../../lib/PacManAI.cpp) and test your implementation with the test
|
|
|
|
in [testPacmanAI.cpp](../../../test/testPacmanAI.cpp) called _"Is valid move"_
|
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
```cpp
|
|
|
|
bool PacManAI::isValidMove(const Move & move) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-10-05 14:10:24 +00:00
|
|
|
<details>
|
|
|
|
<summary>Hint</summary>
|
|
|
|
|
|
|
|
</details>
|
|
|
|
|
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
3. Implement [PacManAI::optimalDirection](../../../lib/PacManAI.cpp) and test your implementation with the test
|
2021-10-05 13:30:50 +00:00
|
|
|
in [testPacmanAI.cpp](../../../test/testPacmanAI.cpp) called _"Is optimal direction"_
|
2021-10-05 14:10:24 +00:00
|
|
|
|
2021-10-05 13:46:44 +00:00
|
|
|
```cpp
|
|
|
|
Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves) {
|
|
|
|
return Direction::NONE;
|
2021-10-05 14:10:24 +00:00
|
|
|
}
|
2021-10-05 13:46:44 +00:00
|
|
|
```
|
2021-10-05 14:10:24 +00:00
|
|
|
|
|
|
|
<details>
|
|
|
|
<summary>Hint</summary>
|
|
|
|
</details>
|
|
|
|
|
|
|
|
|