-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPacManAI.cpp
More file actions
74 lines (59 loc) · 2.3 KB
/
PacManAI.cpp
File metadata and controls
74 lines (59 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "PacManAI.hpp"
#include "Board.hpp"
#include "Random.hpp"
namespace pacman {
void PacManAI::reset() {
pos = {};
suggested_direction = Direction::RIGHT;
}
// This function is not yet implemented.
// You will implement it as part of module 24.
GridPosition PacManAI::pelletClosestToPacman(GridPosition pacmanGridPosition [[maybe_unused]],
std::vector<GridPosition> & pellets [[maybe_unused]]) {
return {0, 0};
}
// This function is not yet implemented.
// You will implement it as part of module 21.
bool PacManAI::isValidMove(const Move & move [[maybe_unused]]) {
return false;
}
// This function is not yet implemented.
// You will implement it as part of module 21. and 24.
Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves [[maybe_unused]]) {
return Direction::NONE;
}
// This function is not yet implemented.
// You will implement it as part of module 25.
Direction PacManAI::chooseNewDirectionForPacMan(const PacMan & pacMan [[maybe_unused]],
const Pellets & pellets [[maybe_unused]]) {
const GridPosition pacManGridPos = pacMan.positionInGrid();
auto pelletPositions = pellets.allPellets();
if (pelletPositions.empty()) {
return Direction::NONE;
}
const GridPosition targetPos = pelletClosestToPacman(pacManGridPos, pelletPositions);
const GridPosition currentPosition = pacMan.positionInGrid();
const auto [x, y] = currentPosition;
std::array<Move, 4> possibleMoves = {
Move{ Direction::UP, { x, y - 1 } },
Move{ Direction::LEFT, { x - 1, y } },
Move{ Direction::DOWN, { x, y + 1 } },
Move{ Direction::RIGHT, { x + 1, y } }
};
for (auto & move : possibleMoves) {
if (!isValidMove(move))
continue;
move.distanceToTarget = positionDistance(move.position, targetPos);
}
return optimalDirection(possibleMoves);
}
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets [[maybe_unused]]) {
const GridPosition pacManGridPos = pacMan.positionInGrid();
const GridPosition currentGridPos = positionToGridPosition(pos);
if (!isIntersection(pacManGridPos) || currentGridPos == pacManGridPos) {
return;
}
suggested_direction = chooseNewDirectionForPacMan(pacMan, pellets);
pos = pacMan.position();
}
} // namespace pacman