-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartiallyKnownGrid.cpp
164 lines (134 loc) · 3.49 KB
/
PartiallyKnownGrid.cpp
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
#include "PartiallyKnownGrid.h"
using namespace std;
PartiallyKnownGrid::PartiallyKnownGrid(std::string instance_name) {
start_ = kInvalidXYLoc;
curr_ = kInvalidXYLoc;
goal_ = kInvalidXYLoc;
width_ = -1;
height_ = -1;
ifstream map;
map.open(instance_name.c_str());
// Read the dimensions of the grid.
string s;
map >> s;
if (s == "width")
map >> width_;
else
cout<<"Error reading width!"<<endl;
map >> s;
if (s == "height")
map >> height_;
else
cout<<"Error reading height!"<<endl;
cout<<"Grid dimensions: "<<width_<<" x "<<height_<<endl;
// Generate the grid.
grid_.resize(width_);
for (int x = 0; x < width_; x++)
grid_[x].resize(height_);
// Read the cells.
string grid;
while (getline(map, s))
grid = grid + s;
if (grid.length() != width_ * height_)
cout<<"Map dimensions do not match the map!"<<endl;
int i = 0;
for (int y = 0; y < height_; y++) {
for (int x = 0; x < width_; x++) {
char cell = grid[i];
switch (cell) {
case '#':
grid_[x][y] = kObstacle;
break;
case '.':
grid_[x][y] = kEmptyCell;
break;
case 'H':
grid_[x][y] = kHiddenObstacle;
break;
case 'O':
grid_[x][y] = kEmptyCell;
start_ = xyLoc(x,y);
curr_ = xyLoc(x,y);
break;
case '$':
grid_[x][y] = kEmptyCell;
goal_ = xyLoc(x,y);
break;
default:
cout<<"Unknown symbol in map: "<<cell<<endl;
break;
}
i++;
}
}
if (start_ == kInvalidXYLoc || goal_ == kInvalidXYLoc)
cout<<"Start and/or goal not specified!"<<endl;
}
void PartiallyKnownGrid::Reset() {
curr_ = start_;
for (int x = 0; x < width_; x++)
for (int y = 0; y < height_; y++)
if (grid_[x][y] == kDiscoveredHiddenObstacle)
grid_[x][y] = kHiddenObstacle;
}
bool PartiallyKnownGrid::MoveTo(xyLoc l) {
int dx = abs(curr_.x - l.x);
int dy = abs(curr_.y - l.y);
if (dx + dy != 1) {
cout<<"MoveTo called with a location "<<l<<" that is not a neighbor of the current cell "<<curr_<<"!"<<endl;
}
return JumpTo(l);
}
bool PartiallyKnownGrid::JumpTo(xyLoc l) {
if (!IsValidLocation(l)) {
cout<<"MoveTo called with a location "<<l<<" that is outside the boundaries of the map!"<<endl;
return false;
}
if (IsBlocked(l)) {
cout<<"MoveTo called with a location "<<l<<" that contains an obstacle!"<<endl;
return false;
}
// Move the agent.
curr_ = l;
// Check the surrounding cells for hidden obstacles.
int x_min = max(l.x-1, 0);
int x_max = min(l.x+1, width_-1);
int y_min = max(l.y-1, 0);
int y_max = min(l.y+1, height_-1);
for (int x = x_min; x <= x_max; x++)
for (int y = y_min; y <= y_max; y++)
if (grid_[x][y] == kHiddenObstacle)
grid_[x][y] = kDiscoveredHiddenObstacle;
return true;
}
bool PartiallyKnownGrid::IsValidLocation(xyLoc l) const {
return l.x >= 0 && l.x < width_ && l.y >= 0 && l.y < height_;
}
bool PartiallyKnownGrid::IsBlocked(xyLoc l) const {
if (!IsValidLocation(l)) {
cout<<"IsBlocked called with a location that is out of bounds!"<<endl;
return false;
}
return grid_[l.x][l.y] == kObstacle || grid_[l.x][l.y] == kDiscoveredHiddenObstacle;
}
void PartiallyKnownGrid::DrawGrid() const {
for (int y = 0; y < height_; y++) {
for (int x = 0; x < width_; x++) {
cellType type = (cellType)grid_[x][y];
if (type == kEmptyCell) {
xyLoc l(x,y);
if (l == curr_)
cout<<"O";
else if (l == goal_)
cout<<"$";
else
cout<<".";
}
else if (type == kObstacle || type == kDiscoveredHiddenObstacle)
cout<<"#";
else if (type == kHiddenObstacle)
cout<<"H";
}
cout<<endl;
}
}