#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
// Directions for movement (right, down, left, up)
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
// AutonomousVehicle class
class AutonomousVehicle {
private:
int x, y; // Current position
int goalX, goalY; // Goal position
vector<vector<char>> grid;
public:
AutonomousVehicle(int startX, int startY, int goalX, int goalY, const vector<vector<char>>& grid)
: x(startX), y(startY), goalX(goalX), goalY(goalY), grid(grid) {}
void move() {
// Move towards the goal position
if (x < goalX) x += 1; // Move right
else if (x > goalX) x -= 1; // Move left
if (y < goalY) y += 1; // Move down
else if (y > goalY) y -= 1; // Move up
// Simulate obstacle avoidance (if there's an obstacle at the new position)
if (grid[y][x] == '#') {
// Randomly choose a different direction if there's an obstacle
int dir = rand() % 4;
x += dx[dir];
y += dy[dir];
}
}
bool hasReachedGoal() const {
return x == goalX && y == goalY;
}
void printPosition() const {
cout << "Vehicle position: (" << x << ", " << y << ")" << endl;
}
};
int main() {
srand(static_cast<unsigned>(time(0))); // Seed for random number generation
int width = 10;
int height = 10;
// Initialize the grid with empty spaces
vector<vector<char>> grid(height, vector<char>(width, '.'));
// Add some obstacles to the grid
grid[3][3] = '#';
grid[4][3] = '#';
grid[5][3] = '#';
// Initialize the vehicle
AutonomousVehicle vehicle(0, 0, 9, 9, grid);
// Simulation loop
while (!vehicle.hasReachedGoal()) {
cout << "Current state:" << endl;
vehicle.printPosition();
vehicle.move();
cout << "Moving..." << endl;
cout << endl;
}
cout << "Goal reached!" << endl;
return 0;
}