-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartAlgorithm.cpp
More file actions
1289 lines (1254 loc) · 48.8 KB
/
SmartAlgorithm.cpp
File metadata and controls
1289 lines (1254 loc) · 48.8 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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "SmartAlgorithm.h"
// this should be a global line in SmartAlgorithm.cpp
//REGISTER_ALGORITHM (SmartAlgorithm)
//constructor - initialize starting point to (0,0)
SmartAlgorithm::SmartAlgorithm() : sensor(NULL), curr_point(0,0), choice(Direction::Stay),
last_choice(Direction::Stay), mode(EXPLORING), starting_mode(mode), current_battery(0), steps_to_docking(0), current_step(0),
is_returning_to_docking(false), steps_till_finishing(-1), prev_step(Direction::Stay),
steps_from_docking(0), algorithm_step(Direction::Stay), is_back_on_route(true) {
//set docking placement
docking = Point(0,0);
}
// setSensor is called once when the Algorithm is initialized
//initialize all required data for current run
void SmartAlgorithm::setSensor(const AbstractSensor& sensor) {
this->sensor = &sensor;
//set docking placement
docking = Point(0,0);
//reset point on each new house
curr_point = Point(0,0);
//default step is Stay
choice = Direction::Stay;
//last_choise step is Stay
last_choice = Direction::Stay;
//reset list of clean points
clean_points.clear();
//reset map of adjacent points
adjacent.clear();
//reset mode to starting mode
mode = EXPLORING;
//set starting mode to mode
starting_mode = mode;
//get current battery level
current_battery = this->configuration["BatteryCapacity"];
//reset number of steps for current run
current_step = 0;
//set steps till end of simulation
steps_till_finishing = -1;
//initialize previous step
prev_step = Direction::Stay;
//initialize algorithm step
algorithm_step = Direction::Stay;
//initialize was_step_performed to true
is_back_on_route = true;
//empty stacks
while (!unexplored_points.empty()) {
unexplored_points.pop();
}
while (!route.empty()) {
route.pop();
}
while (!route_to_docking.empty()) {
route_to_docking.pop();
}
while (!route_from_docking.empty()) {
route_from_docking.pop();
}
while (!simulation_interference.empty()) {
simulation_interference.pop();
}
//empty maps
if (adjacent.size()) {
adjacent.clear();
}
if (points_with_path_to_docking.size()) {
points_with_path_to_docking.clear();
}
//empty vector
if (clean_points.size()) {
clean_points.clear();
}
}
// setConfiguration is called once when the Algorithm is initialized - see below
void SmartAlgorithm::setConfiguration(map<string, int> config) {
this->configuration = config;
}
//revert the direction
void SmartAlgorithm::revertDirection(Direction current_direction) {
if (current_direction == Direction::East) {
choice = Direction::West;
}
if (current_direction == Direction::West) {
choice = Direction::East;
}
if (current_direction == Direction::North) {
choice = Direction::South;
}
if (current_direction == Direction::South) {
choice = Direction::North;
}
if (current_direction == Direction::Stay) {
choice = Direction::Stay;
}
}
//revert the direction
void SmartAlgorithm::revertDirection() {
switch (choice) {
case Direction::East: {
choice = Direction::West;
break;
}
case Direction::West: {
choice = Direction::East;
break;
}
case Direction::North: {
choice = Direction::South;
break;
}
case Direction::South: {
choice = Direction::North;
break;
}
case Direction::Stay: {
choice = Direction::Stay;
break;
}
default: {
}
}
}
//update current point accordingly to chosen direction
void SmartAlgorithm::updateCurrentPoint(Direction chosen_direction) {
//if East chosen
if (chosen_direction == Direction::East) {
//x increases by 1, y doesn't change
curr_point.increaseX();
}
//if West chosen
if (chosen_direction == Direction::West) {
//x decreases by 1, y doesn't change
curr_point.decreaseX();
}
//if South chosen
if (chosen_direction == Direction::South) {
//y increases by 1, x doesn't change
curr_point.increaseY();
}
//if North chosen
if (chosen_direction == Direction::North) {
//y decreases by 1, x doesn't change
curr_point.decreaseY();
}
}
//decrease current battery level by battery consumption
void SmartAlgorithm::decreaseBatteryLevel() {
//decrease current battery level by battery consumption
current_battery -= configuration["BatteryConsumptionRate"];
}
//if its time to finish - decrease steps_till_finishing
void SmartAlgorithm::decreaseStepsTillFinishing() {
//if its time to finish
if (steps_till_finishing > 0) {
//decrease steps_till_finishing by 1 on every step
steps_till_finishing--;
}
}
//get return_direction_to_docking from stack
Direction SmartAlgorithm::getReturnToDockingDirection(Direction return_direction_to_docking) {
//if size is not zero
if (!route_to_docking.empty()) {
//get direction from stack of returning to docking
return_direction_to_docking = route_to_docking.top();
//remove direction from stack
route_to_docking.pop();
} else {
//if stack is empty - stay
return_direction_to_docking = Direction::Stay;
}
return return_direction_to_docking;
}
//get return_direction_from_docking from stack
Direction SmartAlgorithm::getReturnFromDockingDirection(Direction return_direction_from_docking) {
//if size is not zero
if (!route_from_docking.empty()) {
//get direction from stack of returning from docking
return_direction_from_docking = route_from_docking.top();
//remove direction from stack
route_from_docking.pop();
} else {
//if stack is empty - stay
return_direction_from_docking = Direction::Stay;
}
return return_direction_from_docking;
}
//get return_direction_to_unexplored from stack
Direction SmartAlgorithm::getReturnToUnexploredDirection(Direction return_direction_to_unexplored) {
//if size is not zero
if (!route.empty()) {
//get direction from stack of returning from docking
return_direction_to_unexplored = route.top();
//remove direction from stack
route.pop();
} else {
//if stack is empty - stay
return_direction_to_unexplored = Direction::Stay;
}
return return_direction_to_unexplored;
}
//get return_direction_to_unexplored from stack
void SmartAlgorithm::getReturnToUnexploredDirection() {
//if size is not zero
if (!route.empty()) {
//get direction from stack of returning from docking
choice = route.top();
//remove direction from stack
route.pop();
} else {
//if stack is empty - stay
choice = Direction::Stay;
}
}
//check surrounding walls and adjacent points
void SmartAlgorithm::checkWalls(SensorInformation sensor_information, vector<Direction>& direction_vector, vector<Point>& adjacent_points) {
//size of array sensor_information.isWall is 4
for (int i = 0; i < 4; i++) {
//no walls in given direction
if (!sensor_information.isWall[i]) {
switch (i) {
case 0: {
//add current direction into direction_vector
direction_vector.push_back(Direction::East);
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//add point in this direction to vector of adjacent points
adjacent_points.push_back(east_point);
//add East point to map of points with routes to docking
if (curr_point == docking) {
stack<Direction> temp_stack;
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::East);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(east_point, temp_stack);
}
else {
stack<Direction> temp_stack = points_with_path_to_docking.find(curr_point)->second;
//check length of the current path
int new_length = temp_stack.size() + 1;
//check length of the old path
int old_length;
//iterator for searching map
std::map<Point, stack<Direction>>::iterator place_of_point;
//check if East point is already added into points_with_path_to_docking map
place_of_point = points_with_path_to_docking.find(east_point);
//if point is found in a map
if (place_of_point != points_with_path_to_docking.end()) {
//get route from East point to docking
stack<Direction> old_stack = points_with_path_to_docking.find(east_point)->second;
old_length = old_stack.size();
//only if new length is shorter - update the path
if (old_length > new_length) {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::East);
//remove old entry from map
points_with_path_to_docking.erase(east_point);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(east_point, temp_stack);
}
}
//if point is not found in a map
else {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::East);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(east_point, temp_stack);
}
}
break;
}
case 1: {
//add current direction into direction_vector
direction_vector.push_back(Direction::West);
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//add point in this direction to vector of adjacent points
adjacent_points.push_back(west_point);
//add West point to map of points with routes to docking
if (curr_point == docking) {
stack<Direction> temp_stack;
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::West);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(west_point, temp_stack);
}
else {
stack<Direction> temp_stack = points_with_path_to_docking.find(curr_point)->second;
//check length of the current path
int new_length = temp_stack.size() + 1;
//check length of the old path
int old_length;
//iterator for searching map
std::map<Point, stack<Direction>>::iterator place_of_point;
//check if East point is already added into points_with_path_to_docking map
place_of_point = points_with_path_to_docking.find(west_point);
//if point is found in a map
if (place_of_point != points_with_path_to_docking.end()) {
//get route from West point to docking
stack<Direction> old_stack = points_with_path_to_docking.find(west_point)->second;
old_length = old_stack.size();
//only if new length is shorter - update the path
if (old_length > new_length) {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::West);
//remove old entry from map
points_with_path_to_docking.erase(west_point);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(west_point, temp_stack);
}
}
//if point is not found in a map
else {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::West);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(west_point, temp_stack);
}
}
break;
}
case 2: {
//add current direction into direction_vector
direction_vector.push_back(Direction::South);
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//add point in this direction to vector of adjacent points
adjacent_points.push_back(south_point);
//add South point to map of points with routes to docking
if (curr_point == docking) {
stack<Direction> temp_stack;
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::South);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(south_point, temp_stack);
}
else {
stack<Direction> temp_stack = points_with_path_to_docking.find(curr_point)->second;
//check length of the current path
int new_length = temp_stack.size() + 1;
//check length of the old path
int old_length;
//iterator for searching map
std::map<Point, stack<Direction>>::iterator place_of_point;
//check if South point is already added into points_with_path_to_docking map
place_of_point = points_with_path_to_docking.find(south_point);
//if point is found in a map
if (place_of_point != points_with_path_to_docking.end()) {
//get route from West point to docking
stack<Direction> old_stack = points_with_path_to_docking.find(south_point)->second;
old_length = old_stack.size();
//only if new length is shorter - update the path
if (old_length > new_length) {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::South);
//remove old entry from map
points_with_path_to_docking.erase(south_point);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(south_point, temp_stack);
}
}
//if point is not found in a map
else {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::South);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(south_point, temp_stack);
}
}
break;
}
case 3: {
//add current direction into direction_vector
direction_vector.push_back(Direction::North);
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//add point in this direction to vector of adjacent points
adjacent_points.push_back(north_point);
//add North point to map of points with routes to docking
if (curr_point == docking) {
stack<Direction> temp_stack;
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::North);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(north_point, temp_stack);
}
else {
stack<Direction> temp_stack = points_with_path_to_docking.find(curr_point)->second;
//check length of the current path
int new_length = temp_stack.size() + 1;
//check length of the old path
int old_length;
//iterator for searching map
std::map<Point, stack<Direction>>::iterator place_of_point;
//check if North point is already added into points_with_path_to_docking map
place_of_point = points_with_path_to_docking.find(north_point);
//if point is found in a map
if (place_of_point != points_with_path_to_docking.end()) {
//get route from North point to docking
stack<Direction> old_stack = points_with_path_to_docking.find(north_point)->second;
old_length = old_stack.size();
//only if new length is shorter - update the path
if (old_length > new_length) {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::North);
//remove old entry from map
points_with_path_to_docking.erase(north_point);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(north_point, temp_stack);
}
}
//if point is not found in a map
else {
//add direction from current point to adjacent point into the stack
temp_stack.push(Direction::North);
//add new entry into the points_with_path_to_docking map
points_with_path_to_docking.emplace(north_point, temp_stack);
}
}
break;
}
default: {
}
}
}
}
//Stay can be always a choice - added into direction_vector
direction_vector.push_back(Direction::Stay);
}
//get new direction from current point in order of priorities
void SmartAlgorithm::getNewDirection(SensorInformation sensor_information, vector<Direction>& available_directions) {
//default direction is Stay
choice = Direction::Stay;
std::vector<Point>::iterator it; //iterator for checking clean points
//EAST=>NORTH=>WEST=>SOUTH
//check if appropriate direction is available
//check from lowest priority to highest - chosen direction will be with highest priority
//check there is no wall in desired direction and dirt level at current point is 0
//no wall at South
if (!sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_directions.push_back(Direction::South);
}
}
//no wall at West
if (!sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_directions.push_back(Direction::West);
}
}
//no wall at North
if (!sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_directions.push_back(Direction::North);
}
}
if (!sensor_information.isWall[0]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_directions.push_back(Direction::East);
}
}
//stay till dust level is not 0
if (sensor_information.dirtLevel != 0) {
choice = Direction::Stay;
}
}
//get new direction from current point in order of priorities
void SmartAlgorithm::getNewDirection2(SensorInformation sensor_information, vector<Direction>& available_unvisited_directions) {
//default direction
choice = Direction::Stay;
//iterator for checking clean points
std::vector<Point>::iterator it;
//EAST=>WEST=>NORTH=>SOUTH
//check if appropriate direction is available
//check from lowest priority to highest - chosen direction will be with highest priority
//check there is no wall in desired direction and dirt level at current point is 0
//no wall at South
if (!sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::South);
}
}
//no wall at North
if (!sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::North);
}
}
//no wall at West
if (!sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::West);
}
}
//no wall at East
if (!sensor_information.isWall[0]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::East);
}
}
//stay till dust level is not 0
if (sensor_information.dirtLevel != 0) {
choice = Direction::Stay;
}
}
//get new direction from current point in order of priorities
void SmartAlgorithm::getNewDirection3(SensorInformation sensor_information, vector<Direction>& available_unvisited_directions) {
//default direction
choice = Direction::Stay;
//iterator for checking clean points
std::vector<Point>::iterator it;
//NORTH=>EAST=>WEST=>SOUTH
//check if appropriate direction is available
//check from lowest priority to highest - chosen direction will be with highest priority
//check there is no wall in desired direction and dirt level at current point is 0
//no wall at South
if (!sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::South);
}
}
//no wall at West
if (!sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::West);
}
}
//no wall at East
if (!sensor_information.isWall[0]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::East);
}
}
//no wall at North
if (!sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::North);
}
}
//stay till dust level is not 0
if (sensor_information.dirtLevel != 0) {
choice = Direction::Stay;
}
}
//get new direction from current point in order of priorities
void SmartAlgorithm::getNewDirection4(SensorInformation sensor_information, vector<Direction>& available_unvisited_directions) {
//default direction
choice = Direction::Stay;
//iterator for checking clean points
std::vector<Point>::iterator it;
//NORTH=>SOUTH=>EAST=>WEST
//check if appropriate direction is available
//check from lowest priority to highest - chosen direction will be with highest priority
//check there is no wall in desired direction and dirt level at current point is 0
//no wall at South
if (!sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::South);
}
}
//no wall at West
if (!sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::West);
}
}
//no wall at East
if (!sensor_information.isWall[0]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::East);
}
}
//no wall at South
if (!sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::South);
}
}
//no wall at North
if (!sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_unvisited_directions.push_back(Direction::North);
}
}
//stay till dust level is not 0
if (sensor_information.dirtLevel != 0) {
choice = Direction::Stay;
}
}
//add current point and directions from it to stack of unexplored points and directions
void SmartAlgorithm::addPointToUnexplored(Direction dir, const vector<Direction>& available_directions) {
//if East chosen
if (dir == Direction::East) {
//if there is any additional directions except chosen
if (available_directions.size() > 1) {
//add current point and chosen direction to stack of unexplored points
unexplored_points.emplace(curr_point, Direction::East);
}
}
//if West chosen
if (dir == Direction::West) {
//if there is any additional directions except chosen
if (available_directions.size() > 1) {
//add current point and chosen direction to stack of unexplored points
unexplored_points.emplace(curr_point, Direction::West);
}
}
//if South chosen
if (dir == Direction::South) {
//if there is any additional directions except chosen
if (available_directions.size() > 1) {
//add current point and chosen direction to stack of unexplored points
// unexplored_points.emplace(curr_point, Direction::South);
}
}
//if North chosen
if (dir == Direction::North) {
//if there is any additional directions except chosen
if (available_directions.size() > 1) {
//add current point and chosen direction to stack of unexplored points
unexplored_points.emplace(curr_point, Direction::North);
}
}
}
//update stacks
void SmartAlgorithm::updateStacks(Direction prevStep, Direction dir) {
//rearrange stack accordingly to actual previous step taken by simulation
//if previous step was taken - no rearrangement is needed
if (prev_step == prevStep) {
//add chosen direction to stack route
route.push(dir);
//add chosen direction to stack route_to_docking
route_to_docking.push(dir);
}
//otherwise - remove previous step from stack and insert actually taken step
else {
//remove previous step
route.pop();
//insert previous step taken by simulation
route.push(prevStep);
//insert current step taken by algorithm
route.push(dir);
}
}
//chose next unexplored direction
void SmartAlgorithm::getNewUnexploredDirection(SensorInformation sensor_information, Direction prevStep, vector<Direction>& available_directions) {
std::vector<Point>::iterator it;
//EAST=>WEST=>NORTH=>SOUTH
//chose next unchosen direction
//select next direction
if (last_choice == Direction::East && !sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_directions.push_back(Direction::West);
}
}
if (last_choice == Direction::West && !sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_directions.push_back(Direction::North);
}
}
if (last_choice == Direction::North && !sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_directions.push_back(Direction::South);
}
}
if (choice != Direction::Stay) {
//rearrange stack accordingly to actual previous step taken by simulation
//if previous step was taken - no rearrangement is needed
updateStacks(prevStep, choice);
//update distance to docking
steps_to_docking++;
}
//if last choice chosen - remove from unexplored points stack
if (choice == Direction::South || available_directions.size() == 1) {
//remove it from stack
unexplored_points.pop();
}
}
//chose next unexplored direction
void SmartAlgorithm::getNewUnexploredDirection2(SensorInformation sensor_information, Direction prevStep, vector<Direction>& available_directions) {
std::vector<Point>::iterator it; //iterator for checking clean points
// choice = Direction::Stay;
//EAST=>NORTH=>WEST=>SOUTH
//chose next unchosen direction
//select next direction
if (last_choice == Direction::East && !sensor_information.isWall[3]) {
//check if point to step in is not visited already
//y decreases by 1, x doesn't change
Point north_point(curr_point.getX(), curr_point.getY() - 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), north_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::North;
//add current direction to list of available from current point
available_directions.push_back(Direction::North);
}
}
if (last_choice == Direction::North && !sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_directions.push_back(Direction::West);
}
}
if (last_choice == Direction::West && !sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_directions.push_back(Direction::South);
}
}
if (choice != Direction::Stay) {
//rearrange stack accordingly to actual previous step taken by simulation
//if previous step was taken - no rearrangement is needed
updateStacks(prevStep, choice);
//update distance to docking
steps_to_docking++;
}
//if last choice chosen - remove from unexplored points stack
if (choice == Direction::South || available_directions.size() == 1) {
//remove it from stack
unexplored_points.pop();
}
}
void SmartAlgorithm::getNewUnexploredDirection3(SensorInformation sensor_information, Direction prevStep, vector<Direction>& available_directions) {
std::vector<Point>::iterator it; //iterator for checking clean points
// choice = Direction::Stay;
//NORTH=>EAST=>WEST=>SOUTH
//chose next unchosen direction
//select next direction
if (last_choice == Direction::North && !sensor_information.isWall[0]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_directions.push_back(Direction::East);
}
}
if (last_choice == Direction::East && !sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x decreases by 1, y doesn't change
Point west_point(curr_point.getX() - 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), west_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::West;
//add current direction to list of available from current point
available_directions.push_back(Direction::West);
}
}
if (last_choice == Direction::West && !sensor_information.isWall[2]) {
//check if point to step in is not visited already
//y increases by 1, x doesn't change
Point south_point(curr_point.getX(), curr_point.getY() + 1);
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), south_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::South;
//add current direction to list of available from current point
available_directions.push_back(Direction::South);
}
}
if (choice != Direction::Stay) {
//rearrange stack accordingly to actual previous step taken by simulation
//if previous step was taken - no rearrangement is needed
updateStacks(prevStep, choice);
//update distance to docking
steps_to_docking++;
}
//if last choice chosen - remove from unexplored points stack
if (choice == Direction::South || available_directions.size() == 1) {
//remove it from stack
unexplored_points.pop();
}
}
//chose next unexplored direction
void SmartAlgorithm::getNewUnexploredDirection4(SensorInformation sensor_information, Direction prevStep, vector<Direction>& available_directions) {
std::vector<Point>::iterator it;
//WEST=>EAST=>NORTH=>SOUTH
//chose next unchosen direction
//select next direction
if (last_choice == Direction::West && !sensor_information.isWall[1]) {
//check if point to step in is not visited already
//x increases by 1, y doesn't change
Point east_point(curr_point.getX() + 1, curr_point.getY());
//try to find next point in vector of visited
it = find(clean_points.begin(), clean_points.end(), east_point);
//point NOT found - step into it
if (it == clean_points.end()) {
choice = Direction::East;
//add current direction to list of available from current point
available_directions.push_back(Direction::East);
}
}
if (last_choice == Direction::East && !sensor_information.isWall[3]) {