Skip to content
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

Merged
merged 1 commit into from
Nov 1, 2024
Merged

Create MazeRunner.cpp #80

merged 1 commit into from
Nov 1, 2024

Conversation

o10a19
Copy link
Contributor

@o10a19 o10a19 commented Oct 31, 2024

Summary by Sourcery

New Features:

  • Introduce a new MazeRunner game implemented in C++ where players navigate a maze to collect keys and reach an exit.

Copy link
Contributor

sourcery-ai bot commented Oct 31, 2024

Reviewer's Guide by Sourcery

This 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

Change Details Files
Implementation of the core maze game structure and initialization
  • Define game constants including maze dimensions and symbols
  • Create Point structure for position tracking
  • Implement maze initialization with walls, player, exit, keys, and traps
  • Add random placement logic for game elements
MazeRunner.cpp
Game loop and player interaction implementation
  • Create main game loop with command processing
  • Implement player movement logic with boundary checking
  • Add key collection and trap detection mechanics
  • Include win condition checking based on keys collected and exit position
MazeRunner.cpp
Utility functions for game operation
  • Add maze printing functionality
  • Implement movement validation logic
  • Create win condition checking function
  • Add helper functions for game state management
MazeRunner.cpp

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time. You can also use
    this command to specify where the summary should be inserted.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a 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

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
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) {
Copy link
Contributor

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) {
Copy link
Contributor

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
Copy link
Contributor

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) {
Copy link
Contributor

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.

@x0lg0n x0lg0n merged commit 7e60fb9 into x0lg0n:main Nov 1, 2024
6 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants