public void DrawMaze(Maze maze)
        {
            // We are going to draw maze again, even if it has been drawn.
            _currentMaze = null;

            // We read default styles...
            var mazeBackground = (Brush) Application.Current.Resources["MazeBackground"];
            var mazeWallColor = (Brush) Application.Current.Resources["MazeWallColor"];
            var mazeLineThickness = double.Parse(AppSettings.MazeLineThickness);

            // First of all, we clear the canvas.
            Canvas.Children.Clear();
            Canvas.Background = mazeBackground;

            // Cell dimensions are computed from canvas size.
            var cellWidth = Canvas.ActualWidth / maze.Width;
            var cellHeight = Canvas.ActualHeight / maze.Height;         

            // Useful methods to compute cell offsets.
            Func<uint, double> xOffset = x => x * cellWidth;
            Func<uint, double> yOffset = y => y * cellHeight;

            // Useful methods to draw cell walls.
            Action<Cell, WallPosition, double, double, double, double> drawWall = (c, w, x1, y1, x2, y2) => {
                if (!c.HasWall(w)) {
                    return;
                }
                var wall = new Line {
                    X1 = x1,
                    Y1 = y1,
                    X2 = x2,
                    Y2 = y2,
                    Stroke = mazeWallColor,
                    StrokeStartLineCap = PenLineCap.Round,
                    StrokeEndLineCap = PenLineCap.Round,
                    StrokeThickness = mazeLineThickness,
                };
                Canvas.Children.Add(wall);
            };
            Action<Cell> drawWalls = c => {
                var oX = xOffset(c.X);
                var oY = yOffset(c.Y);
                drawWall(c, WallPosition.West, oX, oY, oX, oY + cellHeight);
                drawWall(c, WallPosition.North, oX, oY, oX + cellWidth, oY);
            };

            // Now, for each cell, we draw a proper rectangle inside the canvas.
            // We do not draw a single rectangle, because each cell has missing walls.
            for (var y = 0; y <= maze.Height; ++y) {
                for (var x = 0; x <= maze.Width; ++x) {
                    var cell = maze.Cells[y, x];
                    drawWalls(cell);
                }
            }

            // At last, we mark the fact that the maze is ready.
            _currentMaze = maze;
        }
Exemple #2
0
 public Cell(Maze maze, uint x, uint y)
 {
     _maze = maze;
     X = x;
     Y = y;
     // Rightmost cells have no north walls.
     _hasNorthWall = (X != maze.Width);
     // Lowermost cells have no west walls.
     _hasWestWall = (Y != maze.Height);
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="maze">The maze from which we should escape.</param>
 public IEnumerable<SimEvent> TryToEscape(Maze maze)
 {
     var currentCell = maze.Entrance;
     _binder.PrintTrace("{0} is entering the maze, good luck to him!", _name);
     while (!ReferenceEquals(currentCell, maze.Exit)) {
         var nextCell = ChooseNextCell(currentCell);
         yield return MoveToCell(currentCell, nextCell);
         _binder.PrintTrace("{0} moved ({1}, {2})...", _name, nextCell.X, nextCell.Y);
         _binder.MoveEscaper(this, nextCell);
         Thread.Sleep(100);
         currentCell = nextCell;
     }
     _binder.PrintTrace("{0} exited the maze at {1}! Amazing :)", _name, _env.Now);
 }
 private void BtnPrintMaze_Click(object sender, RoutedEventArgs e)
 {
     var maze = new Maze(DefaultMazeSide, DefaultMazeSide);
     MazeCanvas.DrawMaze(maze);
 }