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 67 68 69 70 71 72 73 74 75 76 77 78 |
#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; } |
Explanation
- Headers:
<iostream>
: For input and output operations.<vector>
: For using thestd::vector
container.<cstdlib>
: Forrand()
andsrand()
functions.<ctime>
: Fortime()
function to seed the random number generator.
- AutonomousVehicle Class:
- Attributes:
x
andy
: Current position of the vehicle.goalX
andgoalY
: Goal position for the vehicle to reach.grid
: 2D grid representing the environment with obstacles.
- Methods:
void move()
: Moves the vehicle towards the goal. If the vehicle encounters an obstacle, it randomly chooses a different direction.bool hasReachedGoal() const
: Checks if the vehicle has reached the goal position.void printPosition() const
: Prints the current position of the vehicle.
- Attributes:
- Main Function:
- Initialization:
- Seeds the random number generator.
- Defines grid dimensions and initializes the grid with empty spaces and some obstacles.
- Creates an
AutonomousVehicle
object with a starting position, goal position, and the grid.
- Simulation Loop:
- Runs until the vehicle reaches the goal.
- Prints the current state, moves the vehicle, and displays the new position.
- Completion:
- Prints a message when the goal is reached.
- Initialization:
Notes:
- Basic Movement: The vehicle moves towards the goal and avoids obstacles by randomly changing direction if an obstacle is encountered.
- Obstacle Avoidance: This simple example shows basic obstacle avoidance by randomly choosing a new direction. More advanced algorithms would involve pathfinding techniques like A* or Dijkstra’s algorithm.
- Simulation: This program provides a foundational example of autonomous vehicle movement and can be expanded with more sophisticated behaviors and decision-making processes.