-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tron.cpp
226 lines (174 loc) · 4.53 KB
/
test_tron.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
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
#include <iostream>
#include <vector>
#include <list>
using namespace std;
/* Head ends here */
typedef list< pair<int, int> > coordList;
void printMove(char player, int plx , int ply, int dirx, int diry, vector <string> board);
short int canFollowWall(int x, int y, vector <string> board);
coordList getAvailNeighbors(int x, int y, vector <string> board);
void basicWalking(char player,int x, int y, int o_x, int o_y, vector <string> board);
/*
* Takes player coordinates as well as "future" tile coordinates(also named direction)
* and prints the output accordingly
* Utility function --
*/
void printMove(char player, int plx , int ply, int dirx, int diry, vector <string> board) {
//board.at(dirx).at(diry) = player;
if (plx == dirx) {
if(ply < diry) {
cout << "RIGHT" << endl;
} else {
cout << "LEFT" << endl;
}
} else
if (ply == diry) {
if(plx < dirx) {
cout << "DOWN" << endl;
} else {
cout << "UP" << endl;
}
}
}
/*
* Function takes as parameters player position and the map checks if we can follow wall
* return value :
* 0 - no wall to follow
* 1 - can follow vertical
* 2 - can follow horizontal
*/
short int canFollowWall(int x, int y, vector <string> board) {
/*
* vertical follow
*/
if(((board.at(x).at( y - 1) == '#' ) || (board.at(x).at( y + 1) == '#' )) &&
((board.at(x + 1).at(y) == '-' ) || (board.at(x - 1).at(y) == '-' )))
return 1;
/*
* horizontal follow
*/
if(((board.at(x - 1).at(y) == '#' ) || (board.at(x + 1).at(y) == '#' )) &&
((board.at(x).at( y - 1) == '-' ) || (board.at(x).at( y + 1) == '-' )))
return 2;
return 0;
}
/*
* basic walking function when no wall is close, we just take first direction available
* might be improved to some more complex logic
*/
void basicWalking(char player,int x, int y, int o_x, int o_y, vector <string> board) {
coordList nbs = getAvailNeighbors(x,y, board);
if(nbs.size() == 0) {
/*
* We have a dead end : suicide by going up
*/
printMove(player, x, y, x + 1, y, board);
} else {
/*
* we take first direction available
*/
//left, down, right, up
/*
* count == 1 <- left
* count == 2 <- down
* count == 3 <- right
* count == 4 <- up
*/
coordList::iterator iter = nbs.begin();
int count = 0;
for(;iter != nbs.end();iter++) {
int n_x = (*iter).first;
int n_y = (*iter).second;
if ( n_x == x && n_y == y - 1) {
count = 1;
break;
}
if (n_x == x + 1 && n_y == y) {
count = 2;
break;
}
if(n_x == x && n_y == y + 1) {
count = 3;
break;
}
if(n_x = x - 1 && n_y == y) {
count = 4;
break;
}
}
for(int i = 1; i < count; i++) {
nbs.pop_front(); // get to the direction that we are having
}
printMove(player, x,y, nbs.front().first, nbs.front().second, board);
}
}
coordList getAvailNeighbors(int x, int y, vector <string> board) {
coordList result;
if ((x + 1) < board.size() - 1)
if(board.at(x+1).at(y) == '-')
result.push_back(make_pair(x + 1, y));
if(( x - 1) > 0)
if(board.at( x - 1).at(y) == '-')
result.push_back(make_pair(x - 1, y));
if((y + 1) < board.at(x).length() - 1)
if(board.at(x).at(y + 1) == '-')
result.push_back(make_pair(x, y + 1));
if((y - 1) > 0)
if(board.at(x).at(y - 1))
result.push_back(make_pair(x, y - 1));
return result;
}
void swap (int *a, int*b) {
int aux;
aux = *b;
*b = *a;
*a = aux;
}
void nextMove(char player,int x, int y, int o_x, int o_y, vector <string> board){
if(player == 'g') {
swap(&x, &o_x);
swap(&y, &o_y);
}
coordList nbs = getAvailNeighbors(x,y, board);
if (canFollowWall(x,y, board) == 0) {
basicWalking(player, x, y, o_x, o_y, board);
} else
if (canFollowWall(x,y, board) == 1) {
coordList::iterator iter = nbs.begin();
for(;iter != nbs.end();iter++) {
int n_x = (*iter).first;
int n_y = (*iter).second;
if(n_y == y) {
printMove(player, x, y, n_x, n_y, board);
break;
}
}
} else
if (canFollowWall(x,y, board) == 2) {
coordList::iterator iter = nbs.begin();
for(;iter != nbs.end();iter++) {
int n_x = (*iter).first;
int n_y = (*iter).second;
if(n_x == x) {
printMove(player, x, y, n_x, n_y, board);
break;
}
}
}
}
/* Tail starts here */
int main() {
char player;
int x, y;
int o_x, o_y;
int size_x, size_y;
vector <string> board;
cin >> player;
cin >> x >> y >> o_x >> o_y >> size_x >> size_y;
for(int i=0; i<size_x; i++) {
string s; cin >> s;
board.push_back(s);
}
nextMove(player,x,y, o_x, o_y, board);
return 0;
}