-
Notifications
You must be signed in to change notification settings - Fork 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
Create MazeRunner.cpp #80
Conversation
Reviewer's Guide by SourceryThis PR introduces a new maze runner game implementation in C++. The game creates a 10x10 maze where players must collect keys while avoiding traps to reach the exit. The implementation uses a 2D vector for the maze representation and includes player movement, key collection mechanics, and win/loss conditions. No diagrams generated as the changes look simple and do not need a visual representation. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @o10a19 - I've reviewed your changes - here's some feedback:
Overall Comments:
- Consider refactoring this into a class-based design to better encapsulate the game state and behavior. This would make the code more maintainable and reusable.
- Replace rand() with C++'s library for better random number generation. This provides better randomization and is the modern C++ approach.
Here's what I looked at during the review
- 🟡 General issues: 3 issues found
- 🟢 Security: all looks good
- 🟢 Testing: all looks good
- 🟡 Complexity: 1 issue found
- 🟢 Documentation: all looks good
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
maze[exitPos.y][exitPos.x] = EXIT; | ||
|
||
// Randomly place keys, ensuring they are not on the player's position or the exit | ||
for (int i = 0; i < 3; ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue: Keys should only be placed in EMPTY spaces to prevent overwriting existing keys
Add a check for maze[y][x] == EMPTY in the key placement loop, similar to the trap placement logic
int newX = playerPos.x + dx; | ||
int newY = playerPos.y + dy; | ||
|
||
if (maze[newY][newX] != WALL) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Add bounds checking before accessing maze array
Add checks for newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT before accessing maze[newY][newX]
if (newX >= 0 && newX < WIDTH && newY >= 0 && newY < HEIGHT && maze[newY][newX] != WALL) {
bool checkWinCondition(const Point& playerPos, const Point& exitPos, int keysCollected); | ||
|
||
int main() { | ||
srand(static_cast<unsigned int>(time(0))); // Seed for random number generation |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: Consider using modern C++ random facilities instead of rand()
std::random_device and std::mt19937 provide better random number generation. Consider using std::uniform_int_distribution for range generation.
std::random_device rd;
std::mt19937 gen(rd());
return 0; | ||
} | ||
|
||
void initializeMaze(std::vector<std::vector<char>>& maze, Point& playerPos, Point& exitPos) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (complexity): Consider extracting random element placement logic into a reusable helper function
The initializeMaze
function contains duplicated random placement logic. This can be simplified with a helper function:
Point placeGameElement(std::vector<std::vector<char>>& maze, char element) {
int x, y;
do {
x = rand() % (WIDTH - 2) + 1;
y = rand() % (HEIGHT - 2) + 1;
} while (maze[y][x] != EMPTY);
maze[y][x] = element;
return {x, y};
}
void initializeMaze(std::vector<std::vector<char>>& maze, Point& playerPos, Point& exitPos) {
// Place walls (unchanged)
...
// Place player and exit
playerPos = {1, 1};
maze[playerPos.y][playerPos.x] = PLAYER;
exitPos = {WIDTH - 2, HEIGHT - 2};
maze[exitPos.y][exitPos.x] = EXIT;
// Place game elements
for (int i = 0; i < 3; ++i) {
placeGameElement(maze, KEY);
}
for (int i = 0; i < 3; ++i) {
placeGameElement(maze, TRAP);
}
}
This eliminates code duplication while making the placement logic more robust by consistently checking for empty spaces.
Summary by Sourcery
New Features: