package mug12; import java.util.Hashtable; /** * 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 World { //Fields Hashtable people; Hashtable places; Hashtable verbs; Hashtable things; //Constructor public World() {} //Methods /** * Add Person to world */ public void addPerson(Person bob) throws NameTakenException { if (people.get(bob).equals(null)) { this.people.put(bob.getName(), bob); } else { throw new NameTakenException(); } } /** * Remove Person from world */ public void removePerson(Person bob) { this.people.remove(bob); } /** * Add Person to world */ public void addThing(Thing jim) throws NameTakenException { if (things.get(jim).equals(null)) { things.put(jim.getName(), jim); } else { throw new NameTakenException(); } } /** * Remove Person from world */ public void removeThing(Thing jim) { things.remove(jim); } /** * Determine what time it is in the world. * public Time time() { } /** * Indicate that some time has passed. * void elapse() { } */ /** * Sends a String to all persons in a room */ public void broadcast(String message, String[] sendTo) { //server.nameOfTheirThing(thierStuff) ask network group } /** * Find all the people in a room */ public String[] listPeople(String room) { try { Place spot = (Place) this.map(room); return spot.persons(); } catch(NotHereException e) { String[] returned = {"Send a valid Location"}; return returned; } } /** * find all the things in a room */ public String[] listContents(String room) { try { Place spot = (Place) this.map(room); return spot.things(); } catch(NotHereException e) { String[] returned = {"Send a valid Location"}; return returned; } } /** * Maps names to objects */ public Noun map(String name) throws NotHereException { Noun returned = (Noun) this.people.get(name); if (returned.equals(null)) { returned = (Noun) this.places.get(name); } if (returned.equals(null)) { returned = (Noun) this.things.get(name); } if (returned.equals(null)) { throw new NotHereException(); } return returned; } public Verb mapVerb(String verb) throws InvalidVerbException { Verb actionTaken = (Verb) this.verbs.get(verb); if(actionTaken != null) { return actionTaken; } else throw new InvalidVerbException(); } /** * 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 void perform(Action tim) throws InvalidVerbException, InvalidTargetException, NotHereException { String verb = tim.getVerb(); Person subject = (Person) this.map(tim.getSubject()); String[] person = {subject.getName()}; Object object = tim.getObject(); Place location = null; try { location = (Place) this.map(subject.getWordAttribute("location")); } catch(NotHereException e) { throw e; } if (verb == "say" || verb == "yell" || verb == "whisper") { object = tim.getObject(); } else { object = this.map(tim.getObject()); } String[] result = new String[3]; if (subject.getWordAttribute("location").equals( ((ThingPerson) object).getWordAttribute("location"))) { result = this.mapVerb(verb).execute(subject, ((ThingPerson) object)); this.broadcast(result[0], person); this.broadcast(result[1], location.persons()); } else { result[0] = "Your target is not present in the current location and time."; this.broadcast(result[0], person); throw new InvalidTargetException(); } } /*public String interact(String actor, String action, String noun) throws InvalidAction, InvalidTarget; public String interact(String actor, String action) throws InvalidAction;*/ } // interface World