Пример #1
0
        public static MapGraph2 Parse(Map map)
        {
            var kb = new MapGraph2();
            kb.Actions = new List<Action>();
            kb.States = new List<State>();

            for (var i = 0; i < map.Tiles.Count; i++)
            {
            var row = map.Tiles[i];
            for (var j = 0; j < row.Count; j++)
            {
            var tile = row[j];

            // skip wall tiles
            if (tile.Equals(Tile.Wall)) continue;

            var state = kb.GetOrCreateState(j, i, tile, map);

            // adjacent tiles
            var n = kb.GetOrCreateState(j, i-1, map.TileAt(j, i-1), map);
            var e = kb.GetOrCreateState(j+1, i, map.TileAt(j+1, i), map);
            var s = kb.GetOrCreateState(j, i+1, map.TileAt(j, i+1), map);
            var w = kb.GetOrCreateState(j-1, i, map.TileAt(j-1, i), map);

            // actions between tile and adjacent
            // TODO why opposite N/S?
            kb.GetOrCreateAction(state, n, "N");
            kb.GetOrCreateAction(state, e, "E");
            kb.GetOrCreateAction(state, s, "S");
            kb.GetOrCreateAction(state, w, "W");
            }
            }

            return kb;
        }
Пример #2
0
 public static Map Parse(string mapString)
 {
     var map = new Map();
     foreach (var rowString in mapString.Split('\n'))
     {
     var row = new List<Tile>();
     foreach (var tileChar in rowString.ToCharArray())
     {
     switch (tileChar)
     {
         case '#': row.Add(Tile.Wall); break;
         case 'I': row.Add(Tile.Entry); break;
         case ' ': row.Add(Tile.Floor); break;
         case '$': row.Add(Tile.Treasure); break;
         default: throw new Exception(String.Format("Invalid tile character {0}", tileChar));
     }
     }
     map.Tiles.Add(row);
     }
     return map;
 }
Пример #3
0
        protected State GetOrCreateState(int px, int py, Tile type, Map map)
        {
            // check bounds
            if (type == Tile.Invalid) return null;
            if (px < 0  || py < 0 || px > map.Tiles[0].Count - 1 || py > map.Tiles.Count - 1) return null;

            // check if state already exists
            var state = this.States.SingleOrDefault(s => s.X == px && s.Y == py);
            if (state == null)
            {
            state = new State(px, py, type);
            this.States.Add(state);
            }

            return state;
        }