Simulation of Random Walks Gaming Project in C++

Explanation

  1. Constants:
    • GRID_SIZE: Size of the grid (10×10).
    • NUM_STEPS: Number of steps to simulate in the random walk.
  2. Enums and Structs:
    • Direction: Enum representing possible movement directions (right, left, up, down).
    • Position: Struct to represent the agent’s position in the grid.
  3. Function getRandomDirection():
    • Purpose: Returns a random direction for movement.
    • Implementation: Uses std::rand() to select a random direction from the available options.
  4. Function move(Position& pos, Direction dir):
    • Purpose: Updates the agent’s position based on the selected direction.
    • Parameters:
      • pos: Reference to the current position.
      • dir: Direction in which to move.
    • Implementation: Adjusts the position coordinates based on the direction.
  5. Main Function:
    • Setup: Initializes the random number generator, the agent’s starting position at the grid’s center, and the grid itself.
    • Simulation Loop:
      • Updates the grid with the agent’s current position.
      • Chooses a random direction and moves the agent.
      • Ensures the agent remains within grid boundaries.
    • Display: Prints the grid showing the path taken by the agent during the random walk.

Usage

  • Random Walk Simulation: Demonstrates a basic random walk in a 2D grid.
  • Grid Visualization: Shows how the agent’s path evolves over time, represented by a grid with * marking the visited positions.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top