RoboSim Class Reference
Collaboration diagram for RoboSim:

Classes

class  RoboAPIImplementor
 
class  RoboSimExecutionException
 
class  RobotData
 
class  SimGridAllyDeterminant
 
class  SimGridCell
 

Public Member Functions

Robot.GridCell[][] getWorldGrid ()
 
String getOccupantPlayer (Robot.GridCell to_convert)
 
 RoboSim (String[] combatants, int initial_robots_per_combatant, int skill_points, int length, int width, int obstacles) throws RoboSimExecutionException
 
String executeSingleTimeStep () throws RoboSimExecutionException
 

Private Member Functions

SimGridCell[][] getSubGrid (int x_left, int y_up, int x_right, int y_down)
 

Static Private Member Functions

static Robot.GridCell[][] sanitizeGrid (SimGridCell[][] simgrid, String player)
 
static Robot.Robot_Specs checkSpecsValid (Robot.Robot_Specs proposed, String player, int skill_points) throws RoboSimExecutionException
 

Private Attributes

final int WALL_HEALTH = 10
 
final int WALL_DEFENSE = 10
 
SimGridCell[][] worldGrid
 
ArrayList< RobotDataturnOrder
 
int turnOrder_pos
 
SimulatorGUI gui
 

Detailed Description

Definition at line 8 of file RoboSim.java.

Constructor & Destructor Documentation

RoboSim.RoboSim ( String[]  combatants,
int  initial_robots_per_combatant,
int  skill_points,
int  length,
int  width,
int  obstacles 
) throws RoboSimExecutionException

Constructor for RoboSim:

Parameters
combatantsarray of String objects containing the names of the Robot classes for each combatant
initial_robots_per_combatanthow many robots each team starts out with
skill_pointsskill points per combatant
lengthlength of arena
widthwidth of arena
obstaclesnumber of obstacles on battlefield

Definition at line 185 of file RoboSim.java.

References checkSpecsValid(), Robot.createRobot(), RoboSim.RobotData.robot, turnOrder, turnOrder_pos, WALL_HEALTH, and worldGrid.

186  {
187  //Create grid
188  worldGrid = new SimGridCell[length][];
189  for(int i=0; i<length; i++)
190  {
191  worldGrid[i] = new SimGridCell[width];
192  for(int j=0; j<width; j++)
193  {
194  worldGrid[i][j] = new SimGridCell();
195  worldGrid[i][j].x_coord = i;
196  worldGrid[i][j].y_coord = j;
197  worldGrid[i][j].contents = Robot.GridObject.EMPTY;
198  }
199  }
200 
201  //Random number generator
202  generator = new Random();
203 
204  //Initialize array to hold turn order
205  turnOrder = new ArrayList<RobotData>(combatants.length*initial_robots_per_combatant);
206 
207  //Position in turnOrder
208  turnOrder_pos = 0;
209 
210  //Add robots for each combatant
211  for(String player : combatants)
212  {
213  Constructor gen_robot;
214  try
215  {
216  gen_robot = Class.forName(player).getConstructor();
217  }
218  catch(ClassNotFoundException e)
219  {
220  throw new RoboSimExecutionException("robot not found", player);
221  }
222  catch(NoSuchMethodException e)
223  {
224  throw new RoboSimExecutionException("Student's robot doesn't have a default constructor", player);
225  }
226  for(int i=0; i<initial_robots_per_combatant; i++)
227  {
228  int x_pos,y_pos;
229  do
230  {
231  x_pos = generator.nextInt(length);
232  y_pos = generator.nextInt(width);
233  } while(worldGrid[x_pos][y_pos].contents!=Robot.GridObject.EMPTY);
234 
235  worldGrid[x_pos][y_pos].contents = Robot.GridObject.SELF;
236  RobotData data = worldGrid[x_pos][y_pos].occupant_data = new RobotData();
237  data.assoc_cell = worldGrid[x_pos][y_pos];
238  try
239  {
240  data.robot = (Robot)(gen_robot.newInstance());
241  }
242  catch(Exception e)
243  {
244  throw new RoboSimExecutionException("something went wrong invoking studen'ts constructor", player);
245  }
246  data.player = player;
247  byte[] creation_message = new byte[64];
248  creation_message[1] = (byte)(turnOrder_pos % 256);
249  creation_message[0] = (byte)(turnOrder_pos / 256);
250  data.specs = checkSpecsValid(data.robot.createRobot(null, skill_points, creation_message), player, skill_points);
251  data.status = new Robot.Robot_Status();
252  data.status.charge = data.status.health = data.specs.power*10;
253  data.buffered_radio = new ArrayList<byte[]>();
254  turnOrder.add(data);
255  }
256  }
257 
258  //Add obstacles to battlefield
259  for(int i=0; i<obstacles; i++)
260  {
261  int x_pos, y_pos;
262  do
263  {
264  x_pos = generator.nextInt(length);
265  y_pos = generator.nextInt(width);
266  } while(worldGrid[x_pos][y_pos].contents!=Robot.GridObject.EMPTY);
267  worldGrid[x_pos][y_pos].contents = Robot.GridObject.WALL;
268  worldGrid[x_pos][y_pos].wallforthealth = WALL_HEALTH;
269  }
270  }
ArrayList< RobotData > turnOrder
Definition: RoboSim.java:94
SimGridCell[][] worldGrid
Definition: RoboSim.java:93
Definition: Robot.java:11
static Robot.Robot_Specs checkSpecsValid(Robot.Robot_Specs proposed, String player, int skill_points)
Definition: RoboSim.java:167
final int WALL_HEALTH
Definition: RoboSim.java:89
int turnOrder_pos
Definition: RoboSim.java:95

Here is the call graph for this function:

Member Function Documentation

static Robot.Robot_Specs RoboSim.checkSpecsValid ( Robot.Robot_Specs  proposed,
String  player,
int  skill_points 
) throws RoboSimExecutionException
staticprivate

Definition at line 167 of file RoboSim.java.

Referenced by RoboSim.RoboAPIImplementor.finalizeBuilding(), and RoboSim().

168  {
169  if(proposed.attack + proposed.defense + proposed.power + proposed.charge != skill_points)
170  throw new RoboSimExecutionException("attempted to create invalid robot!",player);
171  return proposed;
172  }

Here is the caller graph for this function:

String RoboSim.executeSingleTimeStep ( ) throws RoboSimExecutionException

Executes one timestep of the simulation.

Returns
the winner, if any, or null

Definition at line 1031 of file RoboSim.java.

References RoboSim.RobotData.status, turnOrder, and turnOrder_pos.

1032  {
1033  for(turnOrder_pos=0; turnOrder_pos<turnOrder.size(); turnOrder_pos++)
1034  {
1035  //References to robot's data
1036  RobotData data = turnOrder.get(turnOrder_pos);
1037  WorldAPI student_api = new RoboAPIImplementor(data);
1038 
1039  //Charge robot an amount of charge equal to charge skill
1040  data.status.charge = Math.min(data.status.charge + data.specs.charge, data.specs.charge*10);
1041 
1042  /*We can spend up to status.power power this turn, but
1043  *no more than our current charge level*/
1044  data.status.power = Math.min(data.specs.power, data.status.charge);
1045 
1046  //Defense boost reset to zero at beginning of turn
1047  data.status.defense_boost = 0;
1048 
1049  //Clone status for student
1050  Robot.Robot_Status clonedStatus;
1051  try
1052  {
1053  clonedStatus = (Robot.Robot_Status)(data.status.clone());
1054  }
1055  catch(CloneNotSupportedException e)
1056  {
1057  throw new RuntimeException("error cloning in executeSingleTimeStep. This is not the student's fault.");
1058  }
1059 
1060  //Run student code
1061  data.robot.act(student_api,clonedStatus,data.buffered_radio.toArray(new byte[0][]));
1062  }
1063 
1064  String player = turnOrder.get(0).player;
1065  for(int i=1; i<turnOrder.size(); i++)
1066  if(!turnOrder.get(i).player.equals(player))
1067  return null;
1068  return player;
1069  }
ArrayList< RobotData > turnOrder
Definition: RoboSim.java:94
int turnOrder_pos
Definition: RoboSim.java:95
String RoboSim.getOccupantPlayer ( Robot.GridCell  to_convert)

SimulatorGUI needs to see who owns the robots in the cells This is a hack to allow this by downcasting the passed GridCell to SimGridCell and extracting the data.

Definition at line 104 of file RoboSim.java.

105  {
106  return ((SimGridCell)(to_convert)).occupant_data.player;
107  }
SimGridCell [][] RoboSim.getSubGrid ( int  x_left,
int  y_up,
int  x_right,
int  y_down 
)
private

Helper method to retrieve a subgrid of the world grid

Parameters
x_leftleft x coordinate (inclusive)
y_upsmaller y coordinate (inclusive)
x_rightright x coordinate (inclusive)
y_downlarger y coordinate (inclusive)
Returns
subgrid of the world grid (NOT copied or sanitized)

Definition at line 119 of file RoboSim.java.

References worldGrid.

Referenced by RoboSim.RoboAPIImplementor.getVisibleNeighborhood(), and RoboSim.RoboAPIImplementor.getWorld().

120  {
121  final int x_length = x_right - x_left + 1;
122  final int y_height = y_down - y_up + 1;
123 
124  SimGridCell[][] to_return = new SimGridCell[x_length][y_height];
125  for(int i=x_left; i<=x_right; i++)
126  for(int j=y_up; j<=y_down; j++)
127  to_return[i-x_left][j-y_up] = worldGrid[i][j];
128 
129  return to_return;
130  }
SimGridCell[][] worldGrid
Definition: RoboSim.java:93

Here is the caller graph for this function:

Robot.GridCell [][] RoboSim.getWorldGrid ( )

This is so SimulatorGUI can get a copy of world

Definition at line 99 of file RoboSim.java.

References worldGrid.

99 { return worldGrid; }
SimGridCell[][] worldGrid
Definition: RoboSim.java:93
static Robot.GridCell [][] RoboSim.sanitizeGrid ( SimGridCell  simgrid[][],
String  player 
)
staticprivate

Sanitizer to create a GridCell[][] 2D array to give to client.

Parameters
simgridSimGridCell 2D array with cells to sanitize
playerString containing player's name. Used to create sanitized grid as it would be seen from player's perspective.
Returns
sanitized grid or subgrid from player's perspective

Definition at line 139 of file RoboSim.java.

References Robot.GridCell.contents, RoboSim.SimGridCell.occupant_data, RoboSim.RobotData.player, and RoboSim.SimGridCell.wallforthealth.

Referenced by RoboSim.RoboAPIImplementor.getVisibleNeighborhood(), and RoboSim.RoboAPIImplementor.getWorld().

140  {
141  Robot.GridCell[][] to_return = new Robot.GridCell[simgrid.length][simgrid[0].length];
142  for(int i=0; i<simgrid.length; i++)
143  for(int j=0; j<simgrid[0].length; j++)
144  {
145  SimGridCell sanitized;
146  try
147  {
148  sanitized = (SimGridCell)(simgrid[i][j].clone());
149  }
150  catch(CloneNotSupportedException e)
151  {
152  throw new RuntimeException("Problem in sanitizeGrid() clone attempt. This is not the student's fault.");
153  }
154  if(sanitized.contents==Robot.GridObject.SELF)
155  if(sanitized.occupant_data.player.equals(player))
156  sanitized.contents=Robot.GridObject.ALLY;
157  else
158  sanitized.contents=Robot.GridObject.ENEMY;
159  sanitized.occupant_data=null;
160  sanitized.wallforthealth=0;
161  to_return[i][j] = sanitized;
162  }
163 
164  return to_return;
165  }
Definition: Robot.java:11

Here is the caller graph for this function:

Member Data Documentation

SimulatorGUI RoboSim.gui
private

Definition at line 96 of file RoboSim.java.

int RoboSim.turnOrder_pos
private
final int RoboSim.WALL_DEFENSE = 10
private

Definition at line 90 of file RoboSim.java.

final int RoboSim.WALL_HEALTH = 10
private

Definition at line 89 of file RoboSim.java.

Referenced by RoboSim.RoboAPIImplementor.finalizeBuilding(), and RoboSim().


The documentation for this class was generated from the following file: