001 package aima.basic; 002 003 /* 004 * The base class of all agents . 005 * 006 */ 007 import java.util.ArrayList; 008 import java.util.List; 009 010 import aima.util.Calculator; 011 012 /** 013 * @author Ravi Mohan 014 * 015 */ 016 public class XYEnvironment extends Environment { 017 public static String LOCATION = "location"; 018 019 int width; 020 021 int height; 022 023 XYLocation defaultLocation; 024 025 public XYEnvironment() { 026 027 } 028 029 public XYEnvironment(int width, int height) { 030 031 this.width = width; 032 this.height = height; 033 this.defaultLocation = new XYLocation(1, 1); 034 } 035 036 public void addObject(EnvironmentObject o, XYLocation loc) { 037 038 super.addObject(o, LOCATION, loc); 039 } 040 041 public void addAgent(Agent a, XYLocation loc) { 042 super.addAgent(a, LOCATION, loc); 043 } 044 045 public void moveObjectToAbsoluteLocation(Agent a, XYLocation loc) { 046 047 a.setAttribute(LOCATION, loc); 048 049 } 050 051 public void moveObject(Agent a, String direction) { 052 XYLocation presentLocation = (XYLocation) a.getAttribute(LOCATION); 053 XYLocation locationToMoveTo = presentLocation.locationAt(direction); 054 055 if (!(isBlocked(locationToMoveTo))) { 056 moveObjectToAbsoluteLocation(a, locationToMoveTo); 057 } 058 059 } 060 061 public ArrayList<ObjectWithDynamicAttributes> getObjectsAt(XYLocation loc) { 062 063 ArrayList<ObjectWithDynamicAttributes> retval = new ArrayList<ObjectWithDynamicAttributes>(); 064 065 List<ObjectWithDynamicAttributes> all = getAllObjects(); 066 for (ObjectWithDynamicAttributes obj : all) { 067 XYLocation objLoc = (XYLocation) obj.getAttribute(LOCATION); 068 if (objLoc.equals(loc)) { 069 retval.add(obj); 070 } 071 072 } 073 return retval; 074 } 075 076 public ArrayList getObjectsNear(Agent agent, int radius) { 077 ArrayList<Object> retval = new ArrayList<Object>(); 078 079 XYLocation agentLocation = (XYLocation) agent.getAttribute(LOCATION); 080 081 List<ObjectWithDynamicAttributes> all = getAllObjects(); 082 for (ObjectWithDynamicAttributes a : all) { 083 if (!(a.equals(agent))) { 084 XYLocation otherAgentLocation = (XYLocation) a 085 .getAttribute(LOCATION); 086 if (withinRadius(radius, agentLocation, otherAgentLocation)) { 087 retval.add(a); 088 } 089 } 090 } 091 return retval; 092 } 093 094 private boolean withinRadius(int radius, XYLocation agentLocation, 095 XYLocation objectLocation) { 096 int dist = Calculator.calculateSquareOfDistanceBetweenLocations( 097 agentLocation, objectLocation); 098 int radiusSquared = radius * radius; 099 boolean withinRadius = (dist <= radiusSquared); 100 return withinRadius; 101 } 102 103 @Override 104 public void executeAction(Agent a, String Action) { 105 106 } 107 108 @Override 109 public Percept getPerceptSeenBy(Agent anAgent) { 110 return new Percept(); 111 } 112 113 public boolean isBlocked(XYLocation loc) { 114 boolean retval = false; 115 ArrayList objs = this.getObjectsAt(loc); 116 117 for (Object o : objs) { 118 if (o instanceof Wall) { 119 retval = true; 120 } 121 } 122 return retval; 123 } 124 125 public void makePerimeter() { 126 for (int i = 0; i < width; i++) { 127 XYLocation loc = new XYLocation(i, 0); 128 XYLocation loc2 = new XYLocation(i, height - 1); 129 addObject(new Wall(), loc); 130 addObject(new Wall(), loc2); 131 } 132 133 for (int i = 0; i < height; i++) { 134 XYLocation loc = new XYLocation(0, i); 135 XYLocation loc2 = new XYLocation(width - 1, i); 136 addObject(new Wall(), loc); 137 addObject(new Wall(), loc2); 138 } 139 140 } 141 142 }