-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.cpp
368 lines (335 loc) · 10.9 KB
/
map.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
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
#include <iostream>
#include <string>
#include <cmath>
#include "datadictionary.h"
using namespace std;
//HEADER
class Map {
public :
Map(int w, int l); //Constructor
~Map(); //Destructor
int getWidth();
int getLength();
int getbombPlanted();
int getPowerUp(Position P);
Position findClosest(Position Player, int code, char playerKey); //Mencari objek terdekat dengan player, objek dispesifikasikan dengan variable code
void readMapAndBomb(string, char); //Membaca map dan bom dari file eksternal
Position findPlayer(char playerKey); //Mencari posisi player dalam map
bool checkSafety(int PositionX, int PositionY); //Mengecek keamanan suatu koordinat dari bom
Position dijkstra(Position src, Position des, int& Distance); //Mencari jalur terdekat antara dua titik koordinat
bool near(Position P, char C); //Mengecek apakah suatu titik P berada di sebelah objek C
private :
char** M; //Map dalam matrix 2d
const int width;
const int length;
BombData B[100];
int nBomb;
int bombPlanted;
};
//IMPLEMENTATION
Map::Map(int w, int l) : width(w) , length(l) {
nBomb = 0;
bombPlanted = 0;
M = new char* [length];
for(int i = 0; i < length; i++)
M[i] = new char [width];
}
Map::~Map(){
for(int i = 0; i < length; i++)
delete [] M[i];
delete [] M;
}
int Map::getWidth(){
return width;
}
int Map::getLength(){
return length;
}
int Map::getbombPlanted(){
return bombPlanted;
}
void Map::readMapAndBomb(string filePath, char playerKey){
cout << "Reading map from " << filePath + "/" + "map.txt" << std::endl;
ifstream myfile(filePath + "/" + "map.txt");
string line;
getline(myfile,line);
//Proses membaca map
for(int i = 0; i < length; i++){
getline(myfile,line); //baca sebaris map
for (int j = 0; j < width; j++)
M[i][j] = line[j]; //assign map ke matrix
}
//Proses membaca bomb
bool myData = false;
while(getline(myfile,line)){
if (line[0] == 'K') { //Parsing Key Player
if (line[5] == playerKey)
myData = true;
else
myData = false;
}
else
if (line[0] == 'B' && line[1] == 'o' && line[2] == 'm' && line[3] == 'b' && line[4] == 's'){ //Parsing kalimat
cout << "Reading bomb " << endl;
unsigned int k = 6;
while(k < line.length()){
if (line[k] == '{'){
k += 3;
B[nBomb].x = line[k] - '0';
k++;
if(line[k] != ','){
B[nBomb].x *= 10;
B[nBomb].x += line[k] - '0';
k++;
}
B[nBomb].x -= 1;
k += 3;
B[nBomb].y = line[k] - '0';
k++;
if (line[k] != ','){
B[nBomb].y *= 10;
B[nBomb].y += line[k] - '0';
k++;
}
B[nBomb].y -= 1;
k += 6;
B[nBomb].fuse = line[k] - '0';
k++;
if (line[k] != ','){
B[nBomb].fuse *= 10;
B[nBomb].fuse += line[k] - '0';
k++;
}
k += 8;
B[nBomb].radius = line[k] - '0';
k++;
if (line[k] != '}'){
B[nBomb].radius *= 10;
B[nBomb].radius += line[k] - '0';
k++;
}
//Positionisi sekarang menunjuk ke tanda tutup bracket '}'
if (myData){ //Jika ini merupakan data dari player bot , save nilai banyaknya bom yang sudah kita pasang untuk nantinya ditrigger
bombPlanted++;
}
nBomb++;
}
k++;
}
}
}
}
bool Map::checkSafety(int PositionX, int PositionY){
bool safe = true;
int i =0;
while (i < nBomb && safe) {
int kiri = B[i].x - B[i].radius;
int kanan = B[i].x + B[i].radius;
int atas = B[i].y - B[i].radius;
int bawah = B[i].y + B[i].radius;
if ((PositionX <= kanan && PositionX >= kiri && B[i].y == PositionY) || (PositionX == B[i].x && PositionY >= atas && PositionY <= bawah)) //Positionisi ada diantara bom
safe = false;
else
i++;
}
return safe;
}
Position Map::findPlayer(char playerKey){
Position P;
char temp;
for(int i = 0; i < length; i++){
for (int j = 0; j < width; j++){
temp = toupper(M[i][j]);
if (temp == playerKey) {
P.x = j;
P.y = i;
}
}
}
return P;
}
Position Map::findClosest(Position Player, int code, char playerKey) {
int TempDistance;
int MinDistance = 99;
Position tempPath;
Position Path;
Path.x = 99;
Path.y = 99; //default value
for(int i = Player.y - 42; i < Player.y + 42; i++){ //Mencari objek tertentu yang posisinya terdekat ke posisi player
for(int j = Player.x - 42; j < Player.x + 42; j++){ // alam area 14 x 14 dari posisi player
if (i > 0 && i < length && j > 0 && j < width){ //Titik tidak boleh di luar batas
Position Ptemp;
Ptemp.x = j;
Ptemp.y = i;
if ((M[i][j] == ' ') && checkSafety(j,i)) {
if (code == 1) { // wall
if (near(Ptemp,'+')){ //Target harus berada di dekat tembok
tempPath = dijkstra(Player, Ptemp, TempDistance); //Cari jalur terdekat dan jaraknya dari Player ke tembok
if (TempDistance < MinDistance) { //Cari objek dengan jalur minimum ke posisi player
cout << " Found candidate at " << tempPath.x << " " << tempPath.y << " " << "Distance : " << TempDistance << endl;
MinDistance = TempDistance;
Path = tempPath;
}
}
}
else
if (code == 3) { //power up
if (near(Ptemp,'!') || near(Ptemp,'&') || near(Ptemp,'$')){ //Target harus berada di dekat power up
tempPath = dijkstra(Player, Ptemp, TempDistance); //Cari jalur terdekat dan jaraknya dari Player ke power up
if (TempDistance < MinDistance) { //Cari objek dengan jalur minimum ke posisi player
cout << " Found candidate at " << tempPath.x << " " << tempPath.y << " " << "Distance : " << TempDistance << endl;
MinDistance = TempDistance;
Path = tempPath;
}
}
}
else
if (code == 4 ) { // enemy
for (char ch = 'A'; ch <= 'z'; ch == 'Z' ? ch = 'a': ch++){ //loop through alphabet
if (ch != toupper(playerKey)) {// bukan player kita{
if (near(Ptemp,ch)){
tempPath = dijkstra(Player, Ptemp, TempDistance); //Cari jalur terdekat dan jaraknya dari Player ke power up
if (TempDistance < MinDistance) { //Cari objek dengan jalur minimum ke posisi player
cout << " Found candidate at " << tempPath.x << " " << tempPath.y << " " << "Distance : " << TempDistance << endl;
MinDistance = TempDistance;
Path = tempPath;
}
}
}
}
}
else
{
tempPath = dijkstra(Player, Ptemp, TempDistance); //Cari jalur terdekat dan jaraknya dari Player ke tempat aman
if (TempDistance < MinDistance) { //Cari objek dengan jalur minimum ke posisi player
cout << " Found candidate at " << tempPath.x << " " << tempPath.y << " " << "Distance : " << TempDistance << endl;
MinDistance = TempDistance;
Path = tempPath;
}
}
}
}
}
}
return Path;
}
Position Map::dijkstra(Position src, Position des, int& distance){
int** dist;
bool** visited;
Position** parent;
dist = new int* [length];
visited = new bool* [length];
parent = new Position* [length];
for(int i = 0; i < length; i++){
dist[i] = new int [width];
parent[i] = new Position [width];
visited[i] = new bool [width];
}
//INISIALISASI
parent[src.y][src.x].x = -1;
parent[src.y][src.x].y = -1;
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
dist[i][j] = 99;
visited[i][j] = false;
}
}
dist[src.y][src.x] = 0;
//PROSES PENCARIAN SHORTEST PATH
for(int count = 1; count < (length*width); count++){ //Telusuri semua vertex kecuali target
//Cari vertex minimum yang belum diproses
int min = 99;
Position choosenV;
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
if (!visited[i][j] && (dist[i][j] < min)){
choosenV.x = j;
choosenV.y = i;
min = dist[i][j];
}
}
}
if (min != 99){
visited[choosenV.y][choosenV.x] = true; //vertex dipilih
if ((choosenV.y == des.y) && (choosenV.x == des.x)) //cek apakah target merupakan destinasi
{
int m = choosenV.y;
int n = choosenV.x;
Position path;
while ((n != src.x) || (m != src.y)){ //Proses Backtracking jalur
path.x = n;
path.y = m;
m = parent[path.y][path.x].y;
n = parent[path.y][path.x].x;
}
distance = dist[choosenV.y][choosenV.x];
return path;
}
//update nilai dari distance tetangga vertex terpilih
int kiri = choosenV.x - 1;
int kanan = choosenV.x + 1;
int atas = choosenV.y - 1;
int bawah = choosenV.y + 1;
if ((kiri >= 0) && (M[choosenV.y][kiri] != '#') && ((M[choosenV.y][kiri] != '+') && (!visited[choosenV.y][kiri]))){
//jika tidak di luar batas dan bukan wall dan belum dikunjungi
if (dist[choosenV.y][kiri] > (dist[choosenV.y][choosenV.x] + 1)){
dist[choosenV.y][kiri] = dist[choosenV.y][choosenV.x] + 1;
parent[choosenV.y][kiri].x = choosenV.x;
parent[choosenV.y][kiri].y = choosenV.y;
}
}
if ((kanan< width) && (M[choosenV.y][kanan] != '#') && ((M[choosenV.y][kanan] != '+') && (!visited[choosenV.y][kanan]))){
//jika tidak di luar batas dan bukan wall dan belum dikunjungi
if (dist[choosenV.y][kanan] > (dist[choosenV.y][choosenV.x] + 1)){
dist[choosenV.y][kanan] = dist[choosenV.y][choosenV.x] + 1;
parent[choosenV.y][kanan].x = choosenV.x;
parent[choosenV.y][kanan].y = choosenV.y;
}
}
if ((atas >= 0) && (M[atas][choosenV.x] != '#') && (M[atas][choosenV.x] != '+') && (!visited[atas][choosenV.x])){
//jika tidak di luar batas dan bukan wall dan belum dikunjungi
if (dist[atas][choosenV.x] > (dist[choosenV.y][choosenV.x] + 1)) {
dist[atas][choosenV.x] = dist[choosenV.y][choosenV.x] + 1;
parent[atas][choosenV.x].x = choosenV.x;
parent[atas][choosenV.x].y = choosenV.y;
}
}
if ((bawah < length) && (M[bawah][choosenV.x] != '#') && (M[bawah][choosenV.x] != '+') && (!visited[bawah][choosenV.x])){
//jika tidak di luar batas dan bukan wall dan belum dikunjungi
if (dist[bawah][choosenV.x] > (dist[choosenV.y][choosenV.x] + 1)){
dist[bawah][choosenV.x] = dist[choosenV.y][choosenV.x] + 1;
parent[bawah][choosenV.x].x = choosenV.x;
parent[bawah][choosenV.x].y = choosenV.y;
}
}
}
else //sudah tidak ada kandidat
{
break;
}
}
//path tidak ditemukan
Position path;
path.x = 99;
path.y = 99;
distance = 99;
return path;
}
bool Map::near(Position P, char C){
return ((M[P.y-1][P.x] == C) || (M[P.y+1][P.x] == C) || (M[P.y][P.x-1] == C) || (M[P.y][P.x+1] == C));
}
int Map::getPowerUp(Position P){
if (((M[P.y-1][P.x] == '!') || (M[P.y-1][P.x] == '&') || (M[P.y-1][P.x] == '$')) && checkSafety(P.x,P.y-1))
return 1;
else
if (((M[P.y+1][P.x] == '!') || (M[P.y+1][P.x] == '&') || (M[P.y+1][P.x] == '$')) &&checkSafety(P.x,P.y+1))
return 4;
else
if (((M[P.y][P.x-1] == '!') || (M[P.y][P.x-1] == '&') || (M[P.y][P.x-1] == '$')) && checkSafety(P.x-1,P.y))
return 2;
else
if (((M[P.y][P.x+1] == '!') || (M[P.y][P.x+1] == '&') || (M[P.y][P.x+1] == '$')) && checkSafety(P.x+1,P.y))
return 3;
else
return 0;
}