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 |
#include <iostream> #include <vector> #include <cstdlib> #include <ctime> // Directions for movement: right, left, up, down enum Direction { RIGHT, LEFT, UP, DOWN, NUM_DIRECTIONS }; struct Position { int x, y; }; // Function to get a random direction Direction getRandomDirection() { return static_cast<Direction>(std::rand() % NUM_DIRECTIONS); } // Function to update the position based on direction void move(Position& pos, Direction dir) { switch (dir) { case RIGHT: pos.x += 1; break; case LEFT: pos.x -= 1; break; case UP: pos.y -= 1; break; case DOWN: pos.y += 1; break; } } int main() { const int GRID_SIZE = 10; // Size of the grid const int NUM_STEPS = 50; // Number of steps to simulate // Initialize the random number generator std::srand(static_cast<unsigned>(std::time(nullptr))); Position agentPos = {GRID_SIZE / 2, GRID_SIZE / 2}; // Start position at the center std::vector<std::vector<char>> grid(GRID_SIZE, std::vector<char>(GRID_SIZE, '.')); for (int step = 0; step < NUM_STEPS; ++step) { // Mark the current position if (agentPos.x >= 0 && agentPos.x < GRID_SIZE && agentPos.y >= 0 && agentPos.y < GRID_SIZE) { grid[agentPos.y][agentPos.x] = '*'; } // Get a random direction and move the agent Direction dir = getRandomDirection(); move(agentPos, dir); // Keep the agent within the grid boundaries if (agentPos.x < 0) agentPos.x = 0; if (agentPos.x >= GRID_SIZE) agentPos.x = GRID_SIZE - 1; if (agentPos.y < 0) agentPos.y = 0; if (agentPos.y >= GRID_SIZE) agentPos.y = GRID_SIZE - 1; } // Display the grid std::cout << "Random Walk Simulation:\n"; for (int y = 0; y < GRID_SIZE; ++y) { for (int x = 0; x < GRID_SIZE; ++x) { std::cout << grid[y][x] << ' '; } std::cout << '\n'; } return 0; } |
Explanation
- Constants:
- GRID_SIZE: Size of the grid (10×10).
- NUM_STEPS: Number of steps to simulate in the random walk.
Advertisement - Enums and Structs:
- Direction: Enum representing possible movement directions (right, left, up, down).
- Position: Struct to represent the agent’s position in the grid.
- Function
getRandomDirection()
:- Purpose: Returns a random direction for movement.
- Implementation: Uses
std::rand()
to select a random direction from the available options.
Advertisement - 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.
- 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.
Advertisement - 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.