/// Create places and their interconnections by taking place names, exit /// data and descriptions from a text file. /// Return a map of place names to places. File format for each place: /// First line: place name (one word) /// Second line: pairs of exit direction and neighbor place name /// Remaining paragraph: place description, blank line terminated public static Dictionary<string, Place> createPlaces(string fileName) { StreamReader reader = FIO.OpenReader(fileName); // Map to return Dictionary<string, Place> places = new Dictionary<string, Place> (); // temporary Map to delay recording exits until all places exist Dictionary<string, string> exitStrings = new Dictionary<string, string> (); while (!reader.EndOfStream) { string name = reader.ReadLine (); string exitPairs = reader.ReadLine (); // You could also substitute your lab's ReadParagraph for the two // lines below if you want to format each paragraph line yourself. string description = TextUtil.LineWrap(reader); reader.ReadLine(); // assume empty line after description places [name] = new Place (description); exitStrings [name] = exitPairs; } reader.Close (); // need places before you can map exits // go back and use exitPairs to map exits: foreach (string name in places.Keys) { Place place = places [name]; string[] parts = TextUtil.SplitWhite(exitStrings[name]); for (int i = 0; i < parts.Length; i += 2) { place.setExit (parts [i], places [parts [i + 1]]); } } return places; }
/// Create the game and initialise its internal map. public Game() { places = Place.createPlaces("place_data.txt"); currentPlace = places["outside"]; quitter = new Quitter(); goer = new Goer(this); Dictionary<string, string> helpDetails = new Dictionary<string, string>(); helper = new Helper(helpDetails); helpDetails["help"] = helper.Help(); helpDetails["go"] = goer.Help(); helpDetails["quit"] = quitter.Help(); }
/// Define an exit from this place. /// Going to the exit in this direction /// leads to neighbor place. public void setExit(string direction, Place neighbor) { exits [direction] = neighbor; }
public void SetCurrentPlace(Place place) { currentPlace = place; }
/// Create the game and initialise its internal map. public Game() { places = Place.createPlaces("place_data.txt"); CurrentPlace = places["outside"]; commandMapper = new CommandMapper(this); }