DemoBot.hpp
Go to the documentation of this file.
1 #include "Robot.hpp"
2 
3 #include <cmath>
4 #include <iostream>
5 
6 using namespace robot_api;
7 using namespace std;
8 
12 class DemoBot : public Robot
13 {
14 private:
16 
17 public:
18  Robot_Specs createRobot(WorldAPI* api, int skill_points, vector<uint8_t> message)
19  {
20  Robot_Specs to_return;
21  to_return.attack = to_return.defense = to_return.power = to_return.charge = skill_points/4;
22  //This handles the pathological case where skill_points<4
23  if(skill_points<4)
24  {
25  to_return.charge = 1;
26  skill_points--;
27  if(skill_points > 0)
28  {
29  to_return.power = 1;
30  skill_points--;
31  }
32  if(skill_points > 0)
33  {
34  to_return.defense = 1;
35  skill_points--;
36  }
37  }
38  else
39  {
40  skill_points-=(skill_points/4)*4;
41  to_return.attack += skill_points;
42  }
43 
44  //Keep track of our specs; simulator won't do it for us!
45  my_specs = to_return;
46  return to_return;
47  }
48 
49 private:
50  static bool isAdjacent(const GridCell& c1, const GridCell& c2)
51  {
52  return (abs(c1.x_coord-c2.x_coord)==1 &&
53  c1.y_coord == c2.y_coord ||
54  abs(c1.y_coord-c2.y_coord)==1 &&
55  c1.x_coord == c2.x_coord);
56  }
57 
58  static int searchAndDestroy(GridCell& self, vector<vector<GridCell>> neighbors, WorldAPI& api,int remaining_power)
59  {
60  //cout << "Self: (" << self.x_coord << "," << self.y_coord << ")" << endl;
61  for(int i=0; i<neighbors.size(); i++)
62  for(int j=0; j<neighbors[0].size(); j++)
63  if(neighbors[i][j].contents==ENEMY)
64  {
65  auto path = RobotUtility::findShortestPath(self,neighbors[i][j],neighbors);
66  if(path.size())
67  {
68  auto end_pos = path.end();
69  end_pos--;
70  for(auto k=path.begin(); k!=end_pos && remaining_power > 0; k++)
71  {
72  //cout << "Move: (" << (*k)->x_coord << "," << (*k)->y_coord << ")";
73  Direction way;
74  if((*k)->x_coord < self.x_coord)
75  way = LEFT;
76  else if((*k)->x_coord > self.x_coord)
77  way = RIGHT;
78  else if((*k)->y_coord < self.y_coord)
79  way = UP;
80  else
81  way = DOWN;
82  api.move(1,way);
83  remaining_power--;
84  self = **k;
85  }
86  }
87  if(remaining_power > 0 && isAdjacent(self,neighbors[i][j]))
88  {
89  api.meleeAttack(remaining_power,neighbors[i][j]);
90  remaining_power = 0;
91  }
92  }
93 
94  return remaining_power;
95  }
96 
97  void act(WorldAPI& api, Robot_Status status, vector<vector<uint8_t>> received_radio)
98  {
99  int remaining_power = status.power;
100  int remaining_charge = status.charge;
101 
102  try
103  {
104  auto neighbors = api.getVisibleNeighborhood();
105 
106  //What's our position?
107  GridCell self;
108  for(int i=0; i<neighbors.size(); i++)
109  for(int j=0; j<neighbors[0].size(); j++)
110  if(neighbors[i][j].contents==SELF)
111  {
112  self=neighbors[i][j];
113  break;
114  }
115 
116  //Visible and reachable enemy? Attack!
117  remaining_power = searchAndDestroy(self,neighbors,api,remaining_power);
118 
119  //Do we still have power? Then we haven't attacked anything.
120  //Perhaps we couldn't find an enemy...
121  if(remaining_power > 3)
122  {
123  auto world = api.getWorld(3);
124  remaining_power-=3;
125  searchAndDestroy(self,world,api,remaining_power);
126  }
127  }
129  {
130  cerr << e.msg << endl;
131  }
132  }
133 };
virtual AttackResult meleeAttack(int power, GridCell &adjacent_cell)=0
Definition: Robot.hpp:19
virtual vector< vector< GridCell > > getVisibleNeighborhood()=0
static bool isAdjacent(const GridCell &c1, const GridCell &c2)
Definition: DemoBot.hpp:50
Robot_Specs createRobot(WorldAPI *api, int skill_points, vector< uint8_t > message)
Definition: DemoBot.hpp:18
virtual vector< vector< GridCell > > getWorld(int power)=0
Robot_Specs my_specs
Definition: DemoBot.hpp:15
static int searchAndDestroy(GridCell &self, vector< vector< GridCell >> neighbors, WorldAPI &api, int remaining_power)
Definition: DemoBot.hpp:58
virtual void move(int steps, Direction way)=0
static std::list< GridCell * > findShortestPath(GridCell &origin, const GridCell &target, std::vector< std::vector< GridCell > > &grid)
Definition: robot_api.cpp:29
void act(WorldAPI &api, Robot_Status status, vector< vector< uint8_t >> received_radio)
Definition: DemoBot.hpp:97