-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMarsRover.cpp
More file actions
194 lines (170 loc) · 5.15 KB
/
MarsRover.cpp
File metadata and controls
194 lines (170 loc) · 5.15 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <bits/stdc++.h>
using namespace std;
// Defining the Command Pattern
class Commands {
public:
virtual void execute(class MarsRover& rover) = 0;
};
class MoveCommand : public Commands {
public:
void execute(class MarsRover& rover) override;
};
class TurnLeftCommand : public Commands {
public:
void execute(class MarsRover& rover) override;
};
class TurnRightCommand : public Commands {
public:
void execute(class MarsRover& rover) override;
};
// Define the MarsRover class
class MarsRover {
public:
// Constructor
MarsRover(int x, int y, char direction, int width, int height, const std::vector<std::pair<int, int>>& obstacles);
// Commands
void move();
void turnLeft();
void turnRight();
// Validation
bool isObstacle(int x, int y);
bool isValidPosition(int x, int y);
// Printing Status
string sendStatusReport();
private:
int x;
int y;
char direction;
int width;
int height;
std::vector<std::pair<int, int>> obstacles;
};
// Constructor - Print initial rover status
MarsRover::MarsRover(int x, int y, char direction, int width, int height, const std::vector<std::pair<int, int>>& obstacles)
: x(x), y(y), direction(direction), width(width), height(height), obstacles(obstacles) {
cout << endl << "Initial Rover status --> " << sendStatusReport()<<endl;
}
// Implementing Move Command
void MarsRover::move() {
int nextX = x, nextY = y;
// Calculate the next position based on the current direction
if (direction == 'N') {
nextY += 1;
} else if (direction == 'S') {
nextY -= 1;
} else if (direction == 'E') {
nextX += 1;
} else if (direction == 'W') {
nextX -= 1;
}
// Check if the next position is within the grid
if (x >= 0 && x < width && y >= 0 && y < height) {
// Check if the next position is not an obstacle
if (!isObstacle(nextX, nextY)) {
// Update position
x = nextX;
y = nextY;
}
}
}
bool MarsRover::isObstacle(int x, int y) {
// Check if the position contains an obstacle
for (const auto& obstacle : obstacles) {
if (obstacle.first == x && obstacle.second == y) {
return true;
}
}
return false;
}
void MarsRover::turnLeft() {
// Left turn logic
if (direction == 'N') {
direction = 'W';
} else if (direction == 'W') {
direction = 'S';
} else if (direction == 'S') {
direction = 'E';
} else if (direction == 'E') {
direction = 'N';
}
}
void MarsRover::turnRight() {
// Right turn logic
if (direction == 'N') {
direction = 'E';
} else if (direction == 'E') {
direction = 'S';
} else if (direction == 'S') {
direction = 'W';
} else if (direction == 'W') {
direction = 'N';
}
}
string MarsRover::sendStatusReport() {
// Generate and return a status report
string obstacleDetected = isObstacle(x, y) ? "Obstacle detected." : "No obstacles detected.";
return "Rover Status --> At (" + to_string(x) + ", " + to_string(y) + ") facing " + direction + ". " + obstacleDetected;
}
// Function to control the rover interactively
void controlRover(MarsRover& rover, vector<char> moves) {
for (char move : moves) {
if (move == 'M') {
rover.move();
cout << "For Move M: " << endl;
cout << rover.sendStatusReport() << endl;
} else if (move == 'L') {
rover.turnLeft();
cout << "For Move L: " << endl;
cout << rover.sendStatusReport() << endl;
} else {
rover.turnRight();
cout << "For Move R: " << endl;
cout << rover.sendStatusReport() << endl;
}
}
}
int main() {
// Initializing the Rover
// Set grid width and height - Eg: 10 X 10
int gridWidth, gridHeight;
cout << "Enter the grid width and height: ";
cin >> gridWidth >> gridHeight;
// Set initial Position
int x, y;
cout << "Enter initial position (x y): ";
cin >> x >> y;
// Set direction
char direction;
cout << "Enter the initial direction (N, S, E, or W): ";
cin >> direction;
// Set obstacles - Eg: [(2, 2), (3, 5)]
std::vector<std::pair<int, int>> obstacles;
int numObstacles;
cout << "Enter the number of obstacles: ";
cin >> numObstacles;
for (int i = 0; i < numObstacles; ++i) {
int obstacleX, obstacleY;
cout << "Enter obstacle position (x y) for obstacle" << i + 1 << ": ";
cin >> obstacleX >> obstacleY;
obstacles.emplace_back(obstacleX, obstacleY);
}
// Create Rover Object
MarsRover rover(x, y, direction, gridWidth, gridHeight, obstacles);
// Moving the Rover
cout << endl<< "Enter a list of moves (Enter Q as last move):" << endl;
vector<char> moves;
char move;
while (cin >> move) {
if (move == 'Q') {
break;
}
if (move == 'M' || move == 'L' || move == 'R') {
moves.push_back(move);
} else {
cout << "Invalid move";
break;
}
}
controlRover(rover, moves);
return 0;
}