Skip to content

suggested improvements #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"limits": "cpp"
}
}
1 change: 1 addition & 0 deletions lib/PacManAI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Direction PacManAI::chooseNewDirectionForPacMan(const PacMan & pacMan [[maybe_un
return optimalDirection(possibleMoves);
}

// todo: why is update taking pacman as argument instead of pacManAI
void PacManAI::update(const PacMan & pacMan, const Pellets & pellets [[maybe_unused]]) {

const GridPosition pacManGridPos = pacMan.positionInGrid();
Expand Down
39 changes: 16 additions & 23 deletions test/exercises/21_exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,21 @@

### 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.
#### Background: PacManAI Moves
PacManAI is an alternative gameplay option to the regular PacMan. PacmanAI moves based on
`suggested_direction` rather than take an input from the keyboard.
The `suggested_direction` always starts from `Direction::Right` and is updated based on `chooseNewDirectionForPacMan`.

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][1] and test your
implementation with the test in [testPacmanAI.cpp][2] 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.
walkable the distance stays as infinity.

Note: Going backwards is not allowed, otherwise you'll go back and forwards between 2
Note: Walking opposite to the suggested direction 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
Expand Down Expand Up @@ -76,6 +69,8 @@ bool PacManAI::isValidMove(const Move & move) {
if (!canWalk) {
return false;
}

move.distanceToTarget = positionDistance(AI.position(), move.position);
return true;
}
```
Expand All @@ -88,18 +83,17 @@ bool PacManAI::isValidMove(const Move & move) {

#### 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.
// suggestd Fix:
// The background is slightly misleading as optimalDirection is basically checking the direction for the shortest distance
rather than explicitly checking if there is a ghost inline.

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.
## Exercise
Find the direction of the pellets closest to PacManAI implement in `PacManAI::optimalDirection`

You only need to worry about the grid itself and any ghosts on the
North/South/East/West axis of PacMan.
PacManAI moves in `suggested_direction`. Optimize which direction is an optimal one by finding the pellet
closest to PacmanAI.

#### Exercise
#### Exercise 211

Implement [PacManAI::optimalDirection][1] and test your implementation with the test
in [testPacmanAI.cpp][2] called _"Is optimal direction"_
Expand Down Expand Up @@ -127,7 +121,6 @@ Direction PacManAI::optimalDirection(const std::array<Move, 4> & moves) {
return dir;
}

```

</details>

Expand Down
1 change: 1 addition & 0 deletions test/exercises/22_exercises.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ std::vector<GridPosition> initialPelletPositions() {

```cpp
std::vector<GridPosition> initialPelletPositions() {
//could be renamed to pellet position
std::vector<GridPosition> positions;

positions.reserve((ROWS * COLUMNS) / 3);
Expand Down