public World(string file) { player = new Player(); List<Node> nodes = WorldParser.parseFile(file); worldName = nodes[0].name; rooms = WorldBuilder.build(player, nodes[0].children); Command look = s => { if (s.StartsWith("at ", StringComparison.OrdinalIgnoreCase)) player.commands["examine"](s.Substring(3).Trim()); else if (DirectionUtils.isDirection(s)) Console.WriteLine(player.room.getWall(s).look()); else Console.WriteLine(player.room.look()); }; Command move = s => { Direction d; try { d = DirectionUtils.parse(s); } catch { Console.WriteLine("I don't think that is a valid direction :P"); return; } Wall wall = player.room.getWall(d); if (wall == null || wall.door == null || !wall.door.visible) Console.WriteLine("You have nowhere to go."); else if (wall.door.locked) Console.WriteLine("This door is locked. Find a key."); else { player.room = wall.door.room; look(""); } }; Command take = s => { if (s == "") { Console.WriteLine("Take what?"); return; } Item i = player.room.getItem(s); if (i == null) { if (player.getItem(s) != null) Console.WriteLine("This item is already equipped."); else Console.WriteLine("You can't see such an item."); return; } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } if (!i.takeable) { Console.WriteLine("Sorry you can't take this."); return; } player.addItem(i); player.room.removeItem(i); Console.WriteLine(char.ToUpper(i.name[0]) + i.name.Substring(1) + " taken."); }; Command examine = s => { if (s == "") { Console.WriteLine("Examine what?"); return; } Item i = player.getItem(s); if (i == null) { i = player.room.getItem(s); if (i == null || !i.visible) { Console.WriteLine("You can't see such an item."); return; } } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } Command c = i.getCommand("examine"); if (c != null) c(""); Console.WriteLine(i.examine()); }; Command inventory = s => { if (player.inventory.Count == 0) { Console.WriteLine("Your inventory is empty."); return; } Console.WriteLine("Your inventory:"); foreach (Item i in player.inventory) Console.WriteLine(" - " + i.name); }; Command use = s => { string[] ps = s.Split(new string[] { " on " }, StringSplitOptions.RemoveEmptyEntries); if (ps.Length == 0) { Console.WriteLine("Use what?"); return; } Item i = player.getItem(ps[0].Trim()); if (i == null) { i = player.room.getItem(ps[0].Trim()); if (i == null || !i.visible) { Console.WriteLine("You can't see such an item."); return; } if (!i.useIfNotEquipped) { Console.WriteLine("This item is not equipped."); return; } } if (!i.visible) { Console.WriteLine("You can't see such an item."); return; } Command c = i.getCommand("use"); if (c == null) { Console.WriteLine("You can't use this item."); return; } string p = ""; for (int a = 1; a < ps.Length; a++) p += ps[a].Trim(); c(p); }; Command help = s => { foreach (string c in player.commands.Keys) { if (c == "?" || c == "help") continue; Console.WriteLine(c); } foreach (Item i in player.inventory) { foreach (string c in i.commands.Keys) { if (!player.commands.ContainsKey(c)) Console.WriteLine(c); } } }; player.commands.Add("look", look); player.commands.Add("move", move); player.commands.Add("go", move); player.commands.Add("take", take); player.commands.Add("examine", examine); player.commands.Add("inventory", inventory); player.commands.Add("use", use); player.commands.Add("help", help); player.commands.Add("?", help); }
public void execute(Player p, Item origin, Item target) { Item i; if (room.Equals("%origin%", StringComparison.OrdinalIgnoreCase)) { if (origin == null) throw new InvalidDataException("No origin specified."); i = origin; } else if (room.Equals("%target%", StringComparison.OrdinalIgnoreCase)) { if (target == null) throw new InvalidDataException("No target specified."); i = target; } else { i = p.getItem(item); if (i == null) i = findItem(p.room); if (i == null) return; } if (field.Equals("visible",StringComparison.OrdinalIgnoreCase)) i.visible = value; else if (field.Equals("takeable",StringComparison.OrdinalIgnoreCase)) i.takeable = value; if (field.Equals("locked", StringComparison.OrdinalIgnoreCase)) { if (i is Door) { ((Door)i).locked = value; string r = room; r = r.ToLower().Replace("%target%", p.room.name); Console.WriteLine(char.ToUpper(r[0]) + r.Substring(1) + "'s " + ((Door)i).parent.name + " door is " + (value ? "locked." : "unlocked.")); } else throw new InvalidDataException("Invalid target specified."); } }
private static Command createCommand(Player player, Item origin, string value, Dictionary<string, Instruction> variables) { value = value.Replace("[", "").Replace("]", "").Trim(); string[] cs = value.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries); if (cs.Length == 0) throw new InvalidDataException("No data found for this command."); List<Instruction> instructions = new List<Instruction>(); if (variables == null) variables = new Dictionary<string, Instruction>(); foreach (string s in cs) { string c = s.Trim(); if (c.StartsWith("print", StringComparison.OrdinalIgnoreCase)) instructions.Add(InstructionFactory.createPrint(c.Substring(c.IndexOf('-') + 1).Trim())); else if (c.StartsWith("remove", StringComparison.OrdinalIgnoreCase)) instructions.Add(InstructionFactory.createRemove()); else if (c.StartsWith("drop", StringComparison.OrdinalIgnoreCase)) instructions.Add(InstructionFactory.createDrop()); else if (c.StartsWith("take", StringComparison.OrdinalIgnoreCase)) instructions.Add(InstructionFactory.createTake()); else if (c.StartsWith("GameOver", StringComparison.OrdinalIgnoreCase)) instructions.Add(InstructionFactory.createGameOver(bool.Parse(c.ToLower().Replace("gameover","").Trim()))); else if (c.StartsWith("if", StringComparison.OrdinalIgnoreCase)) { int i = c.IndexOf("if"); int e = c.IndexOf("then"); string cond = c.Substring(i + 2, e - 1 - 2).Trim(); Instruction variable; object v; if (cond.Contains("is")) { string[] conds = cond.Split(new string[] { "is" }, StringSplitOptions.RemoveEmptyEntries); if (conds.Length != 2) throw new InvalidDataException("Invalid conditional statement."); variable = InstructionFactory.createVariable(conds[1].Trim()); v = conds[0].Trim(); } else { string[] conds = cond.Split(new string[] { "==" }, StringSplitOptions.RemoveEmptyEntries); if (conds.Length != 2) throw new InvalidDataException("Invalid conditional statement."); try { v = int.Parse(conds[1].Trim()); } catch { v = conds[1].Trim(); } variable = variables[conds[0].Trim()]; } try { instructions.Add(InstructionFactory.createConditional(variable, v, createCommand(player, origin, c.Substring(e + 4).Replace(" >", ":").Trim(), variables))); } catch (KeyNotFoundException ex) { throw new InvalidDataException("Invalid variable:" + cond[0]); } } else if (c.StartsWith("var ", StringComparison.OrdinalIgnoreCase)) { string[] var = c.Substring(4).Trim().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); variables.Add(var[0].Trim(), InstructionFactory.createVariable(int.Parse(var[1].Trim()))); } else if (c.Contains("++")) { c = c.Replace("++", "").Trim(); try { instructions.Add(InstructionFactory.incrementVariable(variables[c])); } catch (KeyNotFoundException e) { throw new InvalidDataException("Invalid variable:" + c); } } else if (c.Contains("--")) { c = c.Replace("--", "").Trim(); try { instructions.Add(InstructionFactory.decrementVariable(variables[c])); } catch (KeyNotFoundException e) { throw new InvalidDataException("Invalid variable:" + c); } } else if (c.Contains("=")) { string[] assignments = c.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); if (assignments.Length == 0) throw new InvalidDataException("No data found for this instruction."); if (assignments.Length == 1) throw new InvalidDataException("Invalid instruction:" + c); if (assignments.Length > 2) throw new InvalidDataException("Too many values:" + c); string[] item = assignments[0].Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries); if (item.Length == 1) { try { instructions.Add(InstructionFactory.modifyVariable(variables[item[0].Trim()], int.Parse(assignments[1].Trim()))); } catch (KeyNotFoundException e) { throw new InvalidDataException("Invalid variable:" + item[0]); } } else if (item.Length < 3) throw new InvalidDataException("Too little fields:" + assignments[0]); else if (item.Length > 3) throw new InvalidDataException("Too many field:" + assignments[0]); else instructions.Add(InstructionFactory.createAssignment(item[0].Trim() + '.' + item[1].Trim(), item[2].Trim(), bool.Parse(assignments[1].Trim()))); } else { throw new InvalidDataException("Invalid instruction:" + c); } } return str => { Item target; if (str.IndexOf(" door", StringComparison.OrdinalIgnoreCase) >= 0) { str = str.ToLower().Replace(" door", ""); Wall w = player.room.getWall(str); if (w == null) { Console.WriteLine("'" + str + "' is not a valid direction."); return; } if (w.door == null) { Console.WriteLine("There is no door here."); return; } target = w.door; } else { target = player.getItem(str); if (target == null) target = player.room.getItem(str); } try { foreach (Instruction i in instructions) i.execute(player, origin, target); } catch (Exception e) { Console.WriteLine(e.Message); } }; }