-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment-1Performance.cpp
More file actions
105 lines (91 loc) · 3.09 KB
/
Assignment-1Performance.cpp
File metadata and controls
105 lines (91 loc) · 3.09 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
#include <iostream>
#include <vector>
class Environment {
private:
std::vector<std::vector<bool>> grid;
std::pair<int, int> vacuum_position;
int score;
public:
Environment(int rows, int cols, std::vector<std::pair<int, int>> dirt_positions)
: grid(rows, std::vector<bool>(cols, false)), vacuum_position({0, 0}), score(0) {
for (const auto& pos : dirt_positions) {
grid[pos.first][pos.second] = true;
}
}
bool is_dirty(std::pair<int, int> position) {
return grid[position.first][position.second];
}
void clean(std::pair<int, int> position) {
if (grid[position.first][position.second]) {
grid[position.first][position.second] = false;
score++;
}
}
void move_vacuum(const std::string& direction) {
int x = vacuum_position.first;
int y = vacuum_position.second;
if (direction == "UP" && x > 0) {
vacuum_position.first--;
} else if (direction == "DOWN" && x < grid.size() - 1) {
vacuum_position.first++;
} else if (direction == "LEFT" && y > 0) {
vacuum_position.second--;
} else if (direction == "RIGHT" && y < grid[0].size() - 1) {
vacuum_position.second++;
}
}
std::pair<int, int> get_vacuum_position() {
return vacuum_position;
}
int get_score() {
return score;
}
void display_environment() {
for (const auto& row : grid) {
for (bool cell : row) {
std::cout << (cell ? "D" : ".") << " ";
}
std::cout << std::endl;
}
std::cout << "Vacuum Position: (" << vacuum_position.first << ", " << vacuum_position.second << ")" << std::endl;
std::cout << "Score: " << score << std::endl;
}
};
class VacuumCleanerAgent {
private:
Environment& environment;
public:
VacuumCleanerAgent(Environment& env) : environment(env) {}
void perceive_and_act() {
auto position = environment.get_vacuum_position();
if (environment.is_dirty(position)) {
environment.clean(position);
} else {
// Simple reflex: move to the next position
if (position == std::make_pair(0, 0)) {
environment.move_vacuum("RIGHT");
} else if (position == std::make_pair(0, 1)) {
environment.move_vacuum("DOWN");
} else if (position == std::make_pair(1, 1)) {
environment.move_vacuum("LEFT");
} else if (position == std::make_pair(1, 0)) {
environment.move_vacuum("UP");
}
}
}
};
void run_simulation() {
std::vector<std::pair<int, int>> dirt_positions = {{0, 0}, {1, 1}};
Environment environment(2, 2, dirt_positions);
VacuumCleanerAgent agent(environment);
for (int i = 0; i < 10; ++i) { // Run for 10 steps
agent.perceive_and_act();
std::cout << "Step " << i + 1 << ":" << std::endl;
environment.display_environment();
std::cout << std::endl;
}
}
int main() {
run_simulation();
return 0;
}