-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.hpp
156 lines (137 loc) · 4.16 KB
/
bot.hpp
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <stdio.h>
#include <string>
#include <bits/stdc++.h>
#include <stdlib.h>
#include <time.h>
class bot{
public:
bot();
void update(int prow, int pcol, bool runAway);
void getNextCell(int prow, int pcol);
int favourUp(int prow, int pcol);
int favourDown(int prow, int pcol);
int favourLeft(int prow, int pcol);
int favourRight(int prow, int pcol);
int updateAngle();
string getDirection(int dx, int dy);
vector<pair<int,int>> getValidCells();
pair <int, int> changep (int prow, int pcol, string pdir, int offset);
pair <int, int> target (int prow, int pcol, string pdir, int crow, int ccol);
string currDirection = "still";
string nextDirection = "still";
int mode = 0; // 0-chase and 1-run away
int x;
int y;
int nxtRow;
int nxtCol;
int row;
int col;
int cellWidth = 24;
int cellHeight = 24;
int angle = 0;
};
bot::bot(){
row = col = 27;
x = y = row*cellWidth;
}
pair <int, int> bot::changep (int prow, int pcol, string pdir, int offset){
if (pdir == "down") return {prow+offset, pcol };
else if (pdir =="up") return {prow-offset, pcol};
else if (pdir =="left") return {prow, pcol-offset};
else return {prow, pcol+offset};
}
pair <int, int> bot::target (int prow, int pcol, string pdir, int crow, int ccol){
pair <int, int> newpos = changep(prow, pcol, pdir, 2);
prow = newpos.first;
pcol = newpos.second;
return {2*prow - crow, 2*pcol - ccol};
}
vector<pair<int,int>> bot::getValidCells(){
vector<pair<int,int>> nextValidCells;
if(maze[row+1][col]!=1 && currDirection!="up"){
nextValidCells.push_back({row+1,col});
}
if(maze[row-1][col]!=1 && currDirection!="down"){
nextValidCells.push_back({row-1,col});
}
if(maze[row][col+1]!=1 && currDirection!="left"){
nextValidCells.push_back({row,col+1});
}
if(maze[row][col-1]!=1 && currDirection!="right"){
nextValidCells.push_back({row,col-1});
}
return nextValidCells;
}
string bot::getDirection(int dx, int dy){
if(dx == 1 && dy == 0)return "right";
if(dx == -1 && dy == 0)return "left";
if(dx == 0 && dy == 1)return "down";
if(dx == 0 && dy == -1)return "up";
return "still";
}
void bot::getNextCell(int prow, int pcol){
vector<pair<int,int>> nextValidCells = getValidCells();
int distL = 1e7;
int distG = 0;
string turnDirection = "still";
for(auto j:nextValidCells){
int tempDist = (j.first-prow)*(j.first-prow) + (j.second-pcol)*(j.second-pcol);
if(tempDist < distL && mode == 0){
turnDirection = getDirection( j.second - col, j.first - row);
distL = tempDist;
}
if(tempDist > distG && mode == 1){
turnDirection = getDirection( j.second - col, j.first - row);
distG = tempDist;
}
}
currDirection = turnDirection;
}
void bot::update(int prow, int pcol, bool runAway){
if(runAway)mode = 1;
else mode = 0;
if(x%cellWidth == 0 && y%cellWidth == 0){
row = y/cellWidth;
col = x/cellWidth;
getNextCell(prow, pcol);
if(currDirection == "left"){
nxtRow = row;
nxtCol = col-1;
}
else if(currDirection == "right"){
nxtRow = row;
nxtCol = col+1;
}
else if(currDirection == "down"){
nxtRow = row+1;
nxtCol = col;
}
else if(currDirection == "up"){
nxtRow = row-1;
nxtCol = col;
}
}
if(currDirection == "still")return;
else if(currDirection == "left"){
x-=3;
}
else if(currDirection == "right"){
x+=3;
}
else if(currDirection == "down"){
y+=3;
}
else if(currDirection == "up"){
y-=3;
}
}
int bot::updateAngle(){
if (currDirection =="still") return angle; // this will retain the angle
if (currDirection =="down") angle = 90;
else if (currDirection == "left") angle = 180;
else if (currDirection =="up") angle = 270;
else if (currDirection == "right") angle = 0;
return angle; // else we will update the angle
}