package mug13; import java.util.Vector; /** * The world for Multi-User Grinnell. Although it violates * some standards of object-oriented design, we use the world * as a central repository for all our objects. * * @author CSC152 2004F */ public class RealWorld implements World { //fields WorldBuilder worldOps; Broadcaster b; //constructors public RealWorld(WorldBuilder _worldOps) { worldOps = _worldOps; } //Methods /** * Indicate that some time has passed. * void elapse(); */ // Methods for "normal" UI to interact with. /** * Determine what time it is in the world. * public Time time(); /** * Get the location of someone in the world. */ public String getLocation(String name) { try { ThingPerson examined = worldOps.mapThingPerson(name); return examined.getWordAttribute("location"); } catch(Exception e) { String returned = new String("Send a valid Noun"); return returned; } } /** * Get the descriptionInvalidVerb of something (a noun). */ public String describe(String name) { try { Noun examined = worldOps.mapNoun(name); return examined.describe(); } catch(Exception e) { String returned = new String("Send a valid Noun"); return returned; } } /** * Find all the people in a room */ public String[] listPeople(String room) { try { Place spot = worldOps.mapPlace(room); Vector persons = spot.persons(); String[] stuff = new String[persons.size()]; persons.copyInto(stuff); return stuff; } catch(Exception e) { String[] returned = {"Send a valid Location"}; return returned; } } /** * find all the things in a room */ public String[] listContents(String room) { try { Place spot = worldOps.mapPlace(room); Vector things = spot.things(); String[] stuff = new String[things.size()]; things.copyInto(stuff); return stuff; } catch(Exception e) { String[] returned = {"Send a valid Location"}; return returned; } } /** * Record that an interaction has happened. Return a comment on the * results of the action. * * @exception * If the action is invalid or the object is invalid or ... */ public String perform(Action tim) throws InvalidVerbException, InvalidTargetException { String subject = tim.getSubject(); String location = this.getLocation(subject); String object = tim.getObject(); Verb verb = null; try { verb = worldOps.mapVerb(tim.getVerb()); } catch(InvalidVerbException e) { throw e; } try { worldOps.mapPlace(object); } catch(NotHereException g) { if (location != this.getLocation(object)) { throw new InvalidTargetException(); } } String[] result = verb.execute(subject, object); if (result[1] != null) b.broadcast(result[1], listContents(location)); return result[0]; } public void specifyBroadcaster(Broadcaster _b) { this.b = _b; } // specifyBroadcaster(Broadcaster) } // interface World