-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAimer.cpp
More file actions
180 lines (153 loc) · 3.39 KB
/
Aimer.cpp
File metadata and controls
180 lines (153 loc) · 3.39 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
#include <cassert>
#include "Aimer.hpp"
#include "Functions.hpp"
#include "State.hpp"
#include "Snake.hpp"
const int Aimer::NO_AIMS = 5;
Aimer::Aimer(Snake *s):
snake(s)
{}
const char *Aimer::Name() const {
switch(aim) {
case 0:
return "None";
case 1:
return "Closest Fruit";
case 2:
return "Random fruit";
case 3:
return "Furthest fruit";
case 4:
return "Tail";
default:
return NULL;
}
}
const Snake *Aimer::GetSnake() const {
return snake;
}
const std::map <Ball, bool> &Aimer::Sonar() {
if(sonar.empty())
Scan();
return sonar;
}
void Aimer::SonarToggleHead() {
const Ball &head = snake->head();
if(sonar.count(head))
sonar.erase(head);
else
sonar[head] = true;
}
std::map <Ball, int> &Aimer::DistancesHead() {
if(distances_head.empty())
distances_head = bfs(Sonar(), snake->head());
return distances_head;
}
void Aimer::Scan() {
for(const auto &objects : {WALLS->GetObjects(), snake->GetObjects()})
for(const auto &obj : objects)
sonar[obj] = true;
const std::vector <Ball> &snobj = snake->GetObjects();
std::map <Ball, int> &distances = DistancesHead();
for(size_t i = 1; i < snobj.size(); ++i) {
if(distances.count(snobj[i]) == 1
&& distances.at(snobj[i]) > snobj.size() - i)
{
/* distances = bfs(Sonar(), snake->head()); */
sonar.erase(snobj[i]);
}
}
}
void Aimer::Reset() {
sonar.clear();
distances_head.clear();
target = UNDEF_BALL;
}
Ball Aimer::GetTarget() {
assert(0 <= aim && aim < NO_AIMS);
if(aim == 0) {
target = UNDEF_BALL;
return UNDEF_BALL;
}
if(target != UNDEF_BALL) {
assert(target.is_valid_position());
return target;
}
switch(aim) {
case 1:
SetTargetClosestFruit();
break;
case 2:
SetTargetRandomFruit();
break;
case 3:
SetTargetFurthestFruit();
break;
case 4:
SetTargetSnakeTail();
break;
}
assert(target.is_valid_position() || target == UNDEF_BALL);
return target;
}
void Aimer::SetTargetClosestFruit() {
if(FRUITS->GetObjects().empty())
return;
int range = UNDEF_INT;
const std::map <Ball, int> &distances = DistancesHead();
for(const auto &fruit : FRUITS->GetObjects()) {
if(
distances.count(fruit)
&& (
range == UNDEF_INT
|| range > distances.at(fruit)
)
)
{
target = fruit;
range = distances.at(target);
}
}
}
void Aimer::SetTargetRandomFruit() {
const std::map <Ball, int> &distances = DistancesHead();
if(
Ball::InSegment(snake->targetLast, FRUITS->GetObjects())
&& distances.count(snake->targetLast))
{
target = snake->targetLast;
return;
}
target = FRUITS->GetObjects()[rand() % FRUITS->GetObjects().size()];
}
void Aimer::SetTargetFurthestFruit() {
if(FRUITS->GetObjects().empty())
return;
const std::map <Ball, int> &distances = DistancesHead();
int range = UNDEF_INT;
if(
Ball::InSegment(snake->targetLast, FRUITS->GetObjects())
&& distances.count(snake->targetLast))
{
target = snake->targetLast;
return;
}
for(const auto &fruit : FRUITS->GetObjects()) {
if(distances.count(fruit) && range < distances.at(fruit)) {
target = fruit;
range = distances.at(target);
}
}
}
void Aimer::SetTargetSnakeTail() {
const std::map <Ball, int> &distances = DistancesHead();
int range = UNDEF_INT;
target = snake->GetObjects().back();
for(int i = snake->GetObjects().size() - 1; i >= 0; --i) {
if(distances.count(target)) {
target = snake->GetObjects()[i];
range = distances.at(target);
return;
}
}
}