#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;
}