-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.cpp
More file actions
443 lines (368 loc) · 10.7 KB
/
Copy pathbot.cpp
File metadata and controls
443 lines (368 loc) · 10.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <cstdlib>
#include <cmath>
#include <iostream>
#include "bot.h"
using namespace std;
// hi
const int MAX_ROWS = 40; //constant variables
const int MAX_COLS = 40;
const int MAX_NUM = 10;
const int midr = MAX_ROWS/4;
const int midc = MAX_COLS/4;
bool isfenceUp = false;
bool ifmiddle = false;
bool enoughLimber = false;
bool startingFen = false;
int numFences = 0;
//double distances[3] = {0,0,0};
int ROWS; // global variables
int COLS;
int NUM;
bool isNextToPineTree(Dwarf & dwarf, int r, int c); //check if next to pine tree
bool isNextToAppleTree(Dwarf & dwarf, int r, int c); //check if next to apple tree
bool ifDay(int hours, int minutes); //checks if day or night
Dir directionChop(Dwarf & dwarf, int r, int c); //returns chop direction
void find(Dwarf & dwarf, int & rr, int & cc, int r, int c, char tree); //find object
bool ifEmpty(Dwarf & dwarf, int r, int c); //check if space is empty (valid)
Dir directionPickApple(Dwarf & dwarf, int r, int c); //returns direction of tree
bool isNextToPumkpin(Dwarf & dwarf, int r, int c);
Dir directionPickPumpkin(Dwarf & dwarf, int r, int c); //returns direction of tree
//double distanceFromCenter(int r, int c);
double lowestThree(double num1, double num2, double num3);
Dir directionEmpty(Dwarf & dwarf, int r, int c);
bool isNextToFence(Dwarf & dwarf, int r, int c); //check if there is fence next
int pumpkinOnBoard(Dwarf & dwarf);
//need something to check if there is already someone next to the tree
void onStart(int rows, int cols, int num, std::ostream &log)
{
log << "Start!" << endl; // Print a greeting message
ROWS = rows; // Save values in global variables
COLS = cols;
NUM = num;
}
void onAction(Dwarf &dwarf, int day, int hours, int minutes, ostream &log)
{
int r = dwarf.row();
int c = dwarf.col();
if(dwarf.lumber() >= 300) //how much wood can be collected
{
enoughLimber = true;
}
if(day < 7)
{
if(ifDay(hours, minutes) == true && enoughLimber == false) //daytime and need wood
{
// Get current position of the dwarf
if(isNextToPineTree(dwarf, r, c) == true) //choppoing trees when need wood
{
dwarf.start_chop(directionChop(dwarf, r, c));
}
else //tree finder
{
int rr = 0;
int cc = 0;
find(dwarf, rr, cc, r, c, 'P'); //find PINE tree
log << "Walk to " << rr << " " << cc << endl;
dwarf.start_walk(rr, cc);
}
}
if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == false) //not next to apple tree
{
int rr = 0;
int cc = 0;
numFences = dwarf.lumber()/10;
find(dwarf, rr, cc, r, c, 'A'); //find APPLE tree
log << "Walk to " << rr << " " << cc << endl;
//log << "#FENCES " << numFences << endl; //reports # of fences
dwarf.start_walk(rr, cc);
}
if(ifDay(hours, minutes) == false) //night time build fences
{
if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && ifEmpty(dwarf, r+1, c) == true) //ready to build
{
dwarf.start_build(SOUTH);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && ifEmpty(dwarf, r-1, c) == true) //ready to build
{
dwarf.start_build(NORTH);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && ifEmpty(dwarf, r, c+1) == true) //ready to build
{
dwarf.start_build(EAST);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && ifEmpty(dwarf, r, c-1) == true) //ready to build
{
dwarf.start_build(WEST);
}
else if(ifEmpty(dwarf, r+1, c) == false && ifEmpty(dwarf, r-1, c) == false && ifEmpty(dwarf, r, c-1) == false && ifEmpty(dwarf, r, c+1) == false)
{
isfenceUp = true; //fences are up
}
}
if(isfenceUp == true) //as long as fence is up collect apples
{
dwarf.start_pick(directionPickApple(dwarf, r, c));
}
}
else
{
if(ifDay(hours, minutes) == true) //daytime on day 7 cut fence NEEDS TO KNOW WHEN TO STOP CHOPPING
{
if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && dwarf.look(r+1, c) == FENCE) //ready to chop
{
dwarf.start_chop(SOUTH);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && dwarf.look(r-1, c) == FENCE) //ready to chop
{
dwarf.start_chop(NORTH);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && dwarf.look(r, c+1) == FENCE) //ready to chop
{
dwarf.start_chop(EAST);
}
else if(enoughLimber == true && isNextToAppleTree(dwarf, r, c) == true && dwarf.look(r, c-1) == FENCE) //ready to chop
{
dwarf.start_chop(WEST);
}
else if(dwarf.look(r+1, c) != FENCE && dwarf.look(r-1, c) != FENCE && dwarf.look(r, c-1) != FENCE && dwarf.look(r, c+1) != FENCE)
{
isfenceUp = false;
}
}
if(pumpkinOnBoard(dwarf) > 1)
{
if(ifDay(hours, minutes) == true && isfenceUp == false) //day time collect pumkpin of day 7 dwarves 0-3
{
int r = dwarf.row();
int c = dwarf.col();
if(isNextToPumkpin(dwarf, r, c) == true) //picking up pumkins
{
dwarf.start_pick(directionPickPumpkin(dwarf, r, c));
}
else
{
int rr = 0;
int cc = 0;
find(dwarf, rr, cc, r, c, 'K');
log << "Walk to " << rr << " " << cc << endl;
dwarf.start_walk(rr, cc); //walk to pumpkins
}
}
}
else
{
if(dwarf.name() == 1 && ifmiddle == false && startingFen == false) //day time on day 7 dwarf 4 is not middle and didnt build first fence
{
//log << ifmiddle << endl;
ifmiddle = true;
//log << ifmiddle << endl;
dwarf.start_walk(midr, midc); //dwarf 4 walks to middle
}
if(dwarf.name() == 1 && ifmiddle == true && startingFen == false)
{
dwarf.start_build(NORTH);
}
if(dwarf.name() == 1 && ifmiddle == true && dwarf.look(r-1,c) == FENCE)
{
startingFen = true;
}
if(ifmiddle == true && startingFen == true)
{
int r = dwarf.row();
int c = dwarf.col();
if(isNextToFence(dwarf, r, c) == true) //picking up pumkins
{
dwarf.start_build(EAST);
}
else
{
int rr = 0;
int cc = 0;
find(dwarf, rr, cc, r, c, 'F');
log << "Walk to " << rr << " " << cc << endl;
dwarf.start_walk(rr, cc); //walk to pumpkins
}
}
}
}
}
bool isNextToPineTree(Dwarf & dwarf, int r, int c) //check if there is pine tree next
{
if(dwarf.look(r+1, c) == PINE_TREE ||
dwarf.look(r-1, c) == PINE_TREE ||
dwarf.look(r, c+1) == PINE_TREE ||
dwarf.look(r, c-1) == PINE_TREE)
return true;
else
return false;
}
bool isNextToPumkpin(Dwarf & dwarf, int r, int c) //check if there is pine tree next
{
if(dwarf.look(r+1, c) == PUMPKIN ||
dwarf.look(r-1, c) == PUMPKIN ||
dwarf.look(r, c+1) == PUMPKIN ||
dwarf.look(r, c-1) == PUMPKIN)
return true;
else
return false;
}
bool isNextToAppleTree(Dwarf & dwarf, int r, int c) //check if there is apple tree next
{
if(dwarf.look(r+1, c) == APPLE_TREE ||
dwarf.look(r-1, c) == APPLE_TREE ||
dwarf.look(r, c+1) == APPLE_TREE ||
dwarf.look(r, c-1) == APPLE_TREE )
return true;
else
return false;
}
bool isNextToFence(Dwarf & dwarf, int r, int c) //check if there is fence next
{
if(dwarf.look(r+1, c) == FENCE ||
dwarf.look(r-1, c) == FENCE ||
dwarf.look(r, c+1) == FENCE ||
dwarf.look(r, c-1) == FENCE )
return true;
else
return false;
}
bool ifDay(int hours, int minutes) //checks if day or night
{
if(hours >= 7 && hours <= 20 && minutes >= 0 && minutes <= 59) //give 1 hour to build and wait an hour to drop fences
return true;
else
return false;
}
Dir directionChop(Dwarf & dwarf, int r, int c) //returns direction of tree
{
if (dwarf.look(r+1, c) == PINE_TREE)
return SOUTH;
else if (dwarf.look(r-1, c) == PINE_TREE)
return NORTH;
else if (dwarf.look(r, c+1) == PINE_TREE)
return EAST;
else if (dwarf.look(r, c-1) == PINE_TREE)
return WEST;
}
Dir directionEmpty(Dwarf & dwarf, int r, int c) //returns empty direction
{
if (dwarf.look(r+1, c) == EMPTY)
return SOUTH;
else if (dwarf.look(r-1, c) == EMPTY)
return NORTH;
else if (dwarf.look(r, c+1) == EMPTY)
return EAST;
else if (dwarf.look(r, c-1) == EMPTY)
return WEST;
}
void find(Dwarf & dwarf, int & rr, int & cc, int r, int c, char tree) //finds object
{
int range = 0;
bool isFound = false;
while(isFound == false)
{
for(int row = r-range; row < r+range; row++)
{
for(int col = c-range; col < c+range; col++)
{
switch(tree)
{
case 'P': //if tree is pine
if(isNextToPineTree(dwarf, row, col) == true && ifEmpty(dwarf, row, col) == true) //check if there is space around pine tree
{
rr = row;
cc = col;
isFound = true; //exit the loop
}
break;
case 'A':
if(isNextToAppleTree(dwarf, row, col) == true && (ifEmpty(dwarf, row, col) == true)) //check if there is space around apple tree
{
rr = row;
cc = col;
isFound = true; //exit the loop
}
break;
case 'K': //find pumpkin
if(isNextToPumkpin(dwarf, row, col) == true && ifEmpty(dwarf, row, col) == true) //check if there is space around pine tree
{
rr = row;
cc = col;
isFound = true; //exit the loop
}
break;
case 'F': //find fence
if(isNextToFence(dwarf, row, col) == true && (ifEmpty(dwarf, row-1, col) == true))
{
rr = row;
cc = col;
isFound = true; //exit the loop
}
break;
default:
break;
}
}
}
range++; //if no tree in range, increase range
}
}
bool ifEmpty(Dwarf & dwarf, int r, int c)
{
if(dwarf.look(r, c) == EMPTY)
return true;
else
return false;
}
Dir directionPickApple(Dwarf & dwarf, int r, int c) //returns direction of tree
{
if (dwarf.look(r+1, c) == APPLE_TREE)
return SOUTH;
else if (dwarf.look(r-1, c) == APPLE_TREE)
return NORTH;
else if (dwarf.look(r, c+1) == APPLE_TREE)
return EAST;
else if (dwarf.look(r, c-1) == APPLE_TREE)
return WEST;
}
Dir directionPickPumpkin(Dwarf & dwarf, int r, int c) //returns direction of tree
{
if (dwarf.look(r+1, c) == PUMPKIN)
return SOUTH;
else if (dwarf.look(r-1, c) == PUMPKIN)
return NORTH;
else if (dwarf.look(r, c+1) == PUMPKIN)
return EAST;
else if (dwarf.look(r, c-1) == PUMPKIN)
return WEST;
}
/*double distanceFromCenter(int r, int c)
{
return sqrt(pow((r-middleR),2) + pow((c-middleC),2));
}*/
double lowestThree(double num1, double num2, double num3)
{
double smallest = 999999.99;
if (num1 < smallest)
smallest = num1;
if (num2 < smallest)
smallest = num2;
if(num3 < smallest)
smallest = num3;
return smallest;
}
int pumpkinOnBoard(Dwarf & dwarf)
{
int count = 0;
for(int i = 0; i < ROWS; i ++)
{
for(int j = 0; j < COLS; j++)
{
if(dwarf.look(i,j) == PUMPKIN)
{
count++;
}
}
}
return count;
}