/** * Returns the attempted change in position by the agent for the given action. For instance, if the action is north, * it would result in an attempted position change of (0, +1). * @param actionName the action taken. * @return the attempted change in position for the given action. */ protected Location2 attemptedDelta(String actionName){ if(actionName.equals(GridGame.ACTION_NORTH)){ return new Location2(0, 1); } else if(actionName.equals(GridGame.ACTION_SOUTH)){ return new Location2(0, -1); } else if(actionName.equals(GridGame.ACTION_EAST)){ return new Location2(1, 0); } else if(actionName.equals(GridGame.ACTION_WEST)){ return new Location2(-1, 0); } else if(actionName.equals(GridGame.ACTION_NOOP)){ return new Location2(0, 0); } throw new RuntimeException("Error: Unknown action named '" + actionName + "' that GridGameStandardMechanics cannot handle"); }
/** * Returns the x-y position of an agent stored in a Location2 object. * @param s the state in which the agent exists * @param agentName the name of the agent. * @return a {@link GridGameStandardMechanics.Location2} object containing the agents position in the world. */ protected Location2 getLocation(OOState s, String agentName){ ObjectInstance a = s.object(agentName); Location2 loc = new Location2((Integer)a.get(GridGame.VAR_X), (Integer)a.get(GridGame.VAR_Y)); return loc; }
/** * Returns a new {@link Location2} object that is the subtraction of a provided object from this object (this - o). This objects values * are not affected by this operation. * @param o the other object whose values should be subtract. * @return a new {@link Location2} object that is the subtraction of a provided object from this object (this - o). */ public Location2 subtract(Location2 o){ return new Location2(x-o.x, y-o.y); }
/** * Returns a new {@link Location2} object that is the sum of this object and the provided object. This objects values * are not affected by this operation. * @param o the other object whose values should be added. * @return a new {@link Location2} object that is the sum of this object and the provided object. */ public Location2 add(Location2 o){ return new Location2(x+o.x, y+o.y); }