DemoBot.java
Go to the documentation of this file.
1 
5 public class DemoBot implements Robot
6 {
8 
9  public Robot_Specs createRobot(WorldAPI api, int skill_points, byte[] message)
10  {
11  Robot_Specs to_return = new Robot_Specs();
12  to_return.attack = to_return.defense = to_return.power = to_return.charge = skill_points/4;
13  //This handles the pathological case where skill_points<4
14  if(skill_points<4)
15  {
16  to_return.charge = 1;
17  skill_points--;
18  if(skill_points > 0)
19  {
20  to_return.power = 1;
21  skill_points--;
22  }
23  if(skill_points > 0)
24  {
25  to_return.defense = 1;
26  skill_points--;
27  }
28  }
29  else
30  {
31  skill_points-=(skill_points/4)*4;
32  to_return.attack += skill_points;
33  }
34 
35  //Keep track of our specs; simulator won't do it for us!
36  my_specs = to_return;
37  return to_return;
38  }
39 
40  private static boolean isAdjacent(GridCell c1, GridCell c2)
41  {
42  return (Math.abs(c1.x_coord-c2.x_coord)==1 &&
43  c1.y_coord == c2.y_coord ||
44  Math.abs(c1.y_coord-c2.y_coord)==1 &&
45  c1.x_coord == c2.x_coord);
46  }
47 
48  private static int searchAndDestroy(GridCell self, GridCell[][] neighbors,WorldAPI api,int remaining_power) throws RoboSim.RoboSimExecutionException
49  {
50  for(int i=0; i<neighbors.length; i++)
51  for(int j=0; j<neighbors[0].length; j++)
52  if(neighbors[i][j].contents==GridObject.ENEMY)
53  {
54  GridCell[] path = RobotUtility.findShortestPath(self,neighbors[i][j],neighbors);
55  if(path!=null)
56  for(int k=0; k<path.length-1 && remaining_power > 0; k++)
57  {
58  Direction way = null;
59  if(path[k].x_coord < self.x_coord)
60  way = Direction.LEFT;
61  else if(path[k].x_coord > self.x_coord)
62  way = Direction.RIGHT;
63  else if(path[k].y_coord < self.y_coord)
64  way = Direction.UP;
65  else
66  way = Direction.DOWN;
67  api.move(1,way);
68  remaining_power--;
69  self = path[k];
70  }
71 
72  if(remaining_power > 0 && isAdjacent(self,neighbors[i][j]))
73  {
74  api.meleeAttack(remaining_power,neighbors[i][j]);
75  remaining_power = 0;
76  }
77  }
78 
79  return remaining_power;
80  }
81 
82  public void act(WorldAPI api, Robot_Status status, byte[][] received_radio)
83  {
84  int remaining_power = status.power;
85  int remaining_charge = status.charge;
86 
87  try
88  {
89  GridCell[][] neighbors = api.getVisibleNeighborhood();
90 
91  //What's our position?
92  GridCell self=null;
93  for(int i=0; i<neighbors.length; i++)
94  for(int j=0; j<neighbors[0].length; j++)
95  if(neighbors[i][j].contents==GridObject.SELF)
96  {
97  self=neighbors[i][j];
98  break;
99  }
100 
101  //Visible and reachable enemy? Attack!
102  remaining_power = searchAndDestroy(self,neighbors,api,remaining_power);
103 
104  //Do we still have power? Then we haven't attacked anything.
105  //Perhaps we couldn't find an enemy...
106  if(remaining_power > 3)
107  {
108  GridCell[][] world = api.getWorld(3);
109  remaining_power-=3;
110  searchAndDestroy(self,world,api,remaining_power);
111  }
112  }
113  catch(RoboSim.RoboSimExecutionException e)
114  {
115  System.err.println(e.getMessage());
116  }
117  }
118 }
119 
Robot_Specs my_specs
Definition: DemoBot.java:7
Definition: Robot.java:11
Robot_Specs createRobot(WorldAPI api, int skill_points, byte[] message)
Definition: DemoBot.java:9
static boolean isAdjacent(GridCell c1, GridCell c2)
Definition: DemoBot.java:40
void act(WorldAPI api, Robot_Status status, byte[][] received_radio)
Definition: DemoBot.java:82
static int searchAndDestroy(GridCell self, GridCell[][] neighbors, WorldAPI api, int remaining_power)
Definition: DemoBot.java:48