-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.cpp
More file actions
273 lines (227 loc) · 6.87 KB
/
bot.cpp
File metadata and controls
273 lines (227 loc) · 6.87 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @author Talha Rahman
* Course: CSCI-135
* Instructor: Maryash
* Project 3
*/
// This program is used to control the movement of
// the dwarves, such as getting them to walk around
// in addition it can also control which trees they cut
// and a few other stuff.
#include <cstdlib>
#include <cmath>
#include "bot.h"
using std::to_string;
const int MAX_ROWS = 40;
const int MAX_COLS = 40;
const int MAX_NUM = 10;
int ROWS; // global variables
int COLS;
int NUM;
// For debugging
// std::ofstream file("report.txt");
// std::string output = "";
/* onStart:
An Initialization procedure called at the start of the game.
You can use it to initialize certain global variables, or do
something else before the actual simulation starts.
Parameters:
rows: number of rows
cols: number of columns
num: number of dwarfs
log: a cout-like log */
void onStart(int rows, int cols, int num, std::ostream &log) {
log << "Start!" << std::endl; // Print a greeting message
ROWS = rows; // Save values in global variables
COLS = cols;
NUM = num;
}
/* onAction:
A procedure called each time an idle dwarf is choosing
their next action.
Parameters:
dwarf: dwarf choosing an action
day: day (1+)
hours: number of hours in 24-hour format (0-23)
minutes: number of minutes (0-59)
log: a cout-like log */
void onAction(Dwarf &dwarf, int day, int hours, int minutes, std::ostream &log) {
// Get current position of the dwarf
int r = dwarf.row();
int c = dwarf.col();
Dir dir;
// Checks for any adjacent trees
if (isNextToTree(dwarf, dir) ){
log << "Dwarf " << dwarf.name() << " - Chopping Tree at (" << r << "," << c << ")\n";
dwarf.start_chop(dir);
return;
}
// pair.first == true if a tree was found nearby
auto pair = lookForNearestTree(dwarf);
if (pair.first) {
// pair.second is the Point of the nearest Tree
r = pair.second.x;
c = pair.second.y;
log << "Dwarf " << dwarf.name() << " - Tree found. Walking to (" << r << "," << c << ")\n";
dwarf.start_walk(r, c);
return;
} else {
// if no tree was found just go to a random spot
goToRandomPosition(dwarf, log);
return;
}
}
void goToRandomPosition(Dwarf& dwarf, std::ostream& log) {
int rr = rand() % ROWS;
int cc = rand() % COLS;
log << "Dwarf " << dwarf.name() << " - Randomly walking to (" << rr << "," << cc << ")\n";
dwarf.start_walk(rr, cc);
}
bool isNextToTree(Dwarf& dwarf, Dir& dir) {
// Returns true if place is a tree. Else false.
auto isTree( [] (Place p) -> bool
{ return p == PINE_TREE || p == APPLE_TREE; }
);
const int R = dwarf.row();
const int C = dwarf.col();
if (isTree(dwarf.look(R, C + 1))) { // Checks East
dir = EAST;
return true;
}
if (isTree(dwarf.look(R, C - 1))) { // Checks West
dir = WEST;
return true;
}
if (isTree(dwarf.look(R - 1, C))) { // Checks North
dir = NORTH;
return true;
}
if (isTree(dwarf.look(R + 1, C))) { // Checks South
dir = SOUTH;
return true;
}
return false;
}
bool isValidSpace(const int& r, const int& c) {
if ((r < 0 || c < 0) or (r >= ROWS || c >= COLS))
return false;
return true;
}
/**
* 1 2 3 4 5 D = Dwarf. (row, col)
* 1 X X X X X Algo will check nearby tiles for Tree
* 2 X X X X X Order of checking:
* 3 X X D X X (2,2) -> (3, 2) -> (4, 2)
* 4 X X X X X (4, 3) -> (4, 4) -> (3, 4)
* 5 X X X X X (2, 4) -> (2, 3) -> (2, 2)
* Will repeat this type of search at (1,1) and so forth
*/
std::pair<bool, Point> lookForNearestTree(Dwarf& dwarf) {
// Returns true if place is a tree. Else false.
auto isTree( [] (Place p) -> bool
{ return p == PINE_TREE || p == APPLE_TREE; }
);
// will hold Points that point to nearby trees
std::list<Point> list;
bool found = false;
int startR = dwarf.row();
int startC = dwarf.col();
int size = std::max(ROWS, COLS);
// worst case algorithm repeats max(ROS, COLS)
for (int i = 1; i < size; ++i) {
// (r, c) is a point north-west of the dwarf
int r = startR - i;
int c = startC - i;
// checks from top-left to bottom-left
for(; r < startR + i; ++r) {
// skips if (r, c) not a valid position on map
if(!isValidSpace(r, c)) continue;
if (isTree(dwarf.look(r, c))) {
auto pair = checkForAdjacentSpace(dwarf, r, c);
if (pair.first) {
list.push_back(pair.second);
found = true;
}
}
}
// checks from bottom-left to bottom-right
for(; c < startC + i; ++c) {
if(!isValidSpace(r, c)) continue;
if (isTree(dwarf.look(r, c))) {
auto pair = checkForAdjacentSpace(dwarf, r, c);
if (pair.first) {
list.push_back(pair.second);
found = true;
}
}
}
// checks from bottom-right to top-right
for(; r > startR - i; --r) {
if(!isValidSpace(r, c)) continue;
if (isTree(dwarf.look(r, c))) {
auto pair = checkForAdjacentSpace(dwarf, r, c);
if (pair.first) {
list.push_back(pair.second);
found = true;
}
}
}
// checks from top-right to top-left
for(; c > startC - i; --c) {
if(!isValidSpace(r, c)) continue;
if (isTree(dwarf.look(r, c))) {
auto pair = checkForAdjacentSpace(dwarf, r, c);
if (pair.first) {
list.push_back(pair.second);
found = true;
}
}
}
}
if (found) {
return std::make_pair(true, calcNearestPointToDwarf(dwarf, list));
} else {
// file << output << "\n"; // for debugging
return std::make_pair(false, Point{});
}
}
std::pair<bool, Point> checkForAdjacentSpace(Dwarf& dwarf, const int& r, const int& c) {
std::list<Point> list;
bool found = false;
if (dwarf.look(r - 1, c) == EMPTY ){ // NORTH
list.push_back(Point(r - 1, c));
found = true;
}
if (dwarf.look(r, c + 1) == EMPTY) { // EAST
list.push_back(Point(r, c + 1));
found = true;
}
if (dwarf.look(r + 1, c) == EMPTY) { // SOUTH
list.push_back(Point(r + 1, c));
found = true;
}
if (dwarf.look(r, c - 1) == EMPTY) { // WEST
list.push_back(Point(r, c - 1));
found = true;
}
// If multiple empty spots were found, return only the closest
if (found) {
Point nearestPoint = calcNearestPointToDwarf(dwarf, list);
return std::make_pair(true, Point{nearestPoint.x, nearestPoint.y});
}
return std::make_pair(false, Point{});
}
Point calcNearestPointToDwarf(Dwarf& dwarf, const std::list<Point>& list) {
Point nearestPoint;
int distance = INT32_MAX;
for(auto& point : list) {
int currentDist = abs(dwarf.row() - point.x) + abs(dwarf.col() - point.y);
// if the current Point is closer than the previous closest distance
// then we set this Point as the nearestPoint
if (currentDist < distance) {
nearestPoint = point;
distance = currentDist;
}
}
return nearestPoint;
}