public RectangleMaze(IMazeBuilder mazeBuilder, IMazeSchema mazeSchema, ILocation startLocation) { StartLocation = startLocation; Walls = new List <IWall>(); Paths = new List <IPath>(); ThreeSixtyView = new Dictionary <ILocation, Dictionary <Direction, IBuildingBlock> >(); string[] schemaLines = mazeSchema.GetSchema().Split(new[] { Environment.NewLine }, StringSplitOptions.None); IBuildingBlockIdentifier wallIdentifier = GetBuildingBlockIdentifier(mazeSchema, BuildingBlockType.Wall); IBuildingBlockIdentifier pathIdentifier = GetBuildingBlockIdentifier(mazeSchema, BuildingBlockType.Path); List <List <IBuildingBlock> > buildingBlocksByRows = CreateBuildingBlocks(mazeBuilder, schemaLines, wallIdentifier, pathIdentifier); PopulateThreeSixtyView(buildingBlocksByRows); foreach (List <IBuildingBlock> buildingBlocks in buildingBlocksByRows) { foreach (IBuildingBlock buildingBlock in buildingBlocks) { switch (buildingBlock.GetBuildingBlockType()) { case BuildingBlockType.Wall: Walls.Add(buildingBlock as IWall); break; case BuildingBlockType.Path: Paths.Add(buildingBlock as IPath); break; default: throw new NotImplementedException(); } } } }
public static void Main() { //Setup string schema = @" # # # # # # # # # # # # # . . . # . . . . . . # . . # . # . # # # # . # # # # . # . . . . # . # # . . . . # # # . # . . # # # # . # . # . # . # # . . # . # . # . # . # # # . # . # . # . # . # # . . . . . . . . # . # # # # # # # . # # # . # # . . . . . . # . . . # # # # # # # # # # # # #"; IMazeBuilder mazeBuilder = new RectangleMazeBuilder(); IBuildingBlockIdentifier wallIdentifier = mazeBuilder.CreateBuildingBlockIdentifier(BuildingBlockType.Wall, "#"); IBuildingBlockIdentifier pathIdentifier = mazeBuilder.CreateBuildingBlockIdentifier(BuildingBlockType.Path, "."); IMazeSchema mazeSchema = new MazeSchema ( new List <IBuildingBlockIdentifier>() { wallIdentifier, pathIdentifier }, schema ); ILatitude latitude = mazeBuilder.CreateLatitude(0); ILongitude longitude = mazeBuilder.CreateLongitude(2); ILocation startLocation = mazeBuilder.CreateLocation(latitude, longitude); IMaze maze = new RectangleMaze(mazeBuilder, mazeSchema, startLocation); IWalker walker = new WalkerWithRightHand(maze); //Execute do { walker.Walk(); } while (!walker.FoundExit()); //Finalize Console.WriteLine("Walker found the exit!"); }
private List <List <IBuildingBlock> > CreateBuildingBlocks(IMazeBuilder mazeBuilder, string[] schemaLines, IBuildingBlockIdentifier wallIdentifier, IBuildingBlockIdentifier pathIdentifier) { List <List <IBuildingBlock> > buildingBlocksByRows = new List <List <IBuildingBlock> >(); for (int schemaRowIndex = 0; schemaRowIndex <= schemaLines.Length - 1; schemaRowIndex++) { List <IBuildingBlock> buildingBlocksInRow = new List <IBuildingBlock>(); buildingBlocksByRows.Add(buildingBlocksInRow); string schemaRow = schemaLines[schemaRowIndex].Trim(); string[] schemaColumns = schemaRow.Split(); int latiduteIndex = -1; //To Keep track of space which is not BuildingBlock ILongitude longitude = mazeBuilder.CreateLongitude(schemaRowIndex); for (int schemaColumnIndex = 0; schemaColumnIndex <= schemaColumns.Length - 1; schemaColumnIndex++) { BuildingBlockType?buildingBlockType = null; if (schemaColumns[schemaColumnIndex].ToString() == wallIdentifier.GetIdentifier().ToString()) { buildingBlockType = BuildingBlockType.Wall; } else if (schemaColumns[schemaColumnIndex].ToString() == pathIdentifier.GetIdentifier().ToString()) { buildingBlockType = BuildingBlockType.Path; } if (buildingBlockType.HasValue) { latiduteIndex++; ILatitude latitude = mazeBuilder.CreateLatitude(latiduteIndex); ILocation location = mazeBuilder.CreateLocation(latitude, longitude); IBuildingBlock buildingBlock; switch (buildingBlockType) { case BuildingBlockType.Wall: buildingBlock = mazeBuilder.CreateWall(location); break; case BuildingBlockType.Path: buildingBlock = mazeBuilder.CreatePath(location); break; default: throw new NotImplementedException(); } buildingBlocksInRow.Add(buildingBlock); } } } return(buildingBlocksByRows); }