public void ConvertMazeToImage_SuperSimpleMazeRoundTrip_AllPixelColorsAndPositionsMatch()
        {
            const string mazeImageFilePath          = @"..\..\..\TestingMazes\SuperSimpleMaze.png";
            const bool   useEmbeddedColorManagement = true;
            var          originalImage = Image.FromFile(mazeImageFilePath, useEmbeddedColorManagement);

            var maze = MazeConverter.ConvertImageToMaze(originalImage);

            const string mazeImageFileSavePath = @"..\..\..\TestingMazes\SuperSimpleMazeTest2.png";
            var          mazeWriter            = new MazeWriterUtility(mazeImageFileSavePath, maze);

            mazeWriter.SaveMazeImage();

            var newImage = Image.FromFile(mazeImageFileSavePath, useEmbeddedColorManagement);

            var mazeHeight = originalImage.Height;
            var mazeWidth  = originalImage.Width;

            var originalBitmap = new Bitmap(originalImage);
            var newBitmap      = new Bitmap(newImage);

            for (var x = 0; x < mazeWidth; x++)
            {
                for (var y = 0; y < mazeHeight; y++)
                {
                    var originalPixelColor = originalBitmap.GetPixel(x, y);
                    var newPixelColor      = newBitmap.GetPixel(x, y);

                    Assert.AreEqual(originalPixelColor.ToArgb(), newPixelColor.ToArgb());
                }
            }
        }
Beispiel #2
0
        private void SolveMazeOnThread()
        {
            // If the maze image has already been solved, this will reset it
            var mazeImageBitmap = new Bitmap(_mazeImage);

            MazeImageBitmapSource = MazeRedrawnListener.BitmapToBitmapSource(mazeImageBitmap);

            var mazeRedrawnListener = new MazeRedrawnListener();

            try
            {
                _isBusySolving = true;

                // Subscribed to the event to watch the maze get solved
                if (WatchMazeGetSovled)
                {
                    mazeRedrawnListener.Subscribe(_selectedSolverLogic, this);

                    // Pretreatment logic is not very fun to watch
                    _selectedSolverLogic.PreTreatmentLogics.Clear();
                }

                _selectedSolverLogic.SolveMaze();
                _selectedSolverLogic.MarkSolutionPath();

                var mazeWriter = new MazeWriterUtility(MazeSolutionImageFilePath, _selectedSolverLogic.MazeToSolve);
                mazeWriter.SaveMazeImage();

                // This should dispaly the solved path on the maze highlighted in green
                mazeImageBitmap       = new Bitmap(mazeWriter.MazeImage);
                MazeImageBitmapSource = MazeRedrawnListener.BitmapToBitmapSource(mazeImageBitmap);
            }
            catch (Exception exceptionCaught)
            {
                throw exceptionCaught;
            }
            finally
            {
                _isBusySolving = false;
                mazeRedrawnListener.UnSubscribe(_selectedSolverLogic);

                const bool useEmbeddedColorManagement = true;
                _mazeImage   = Image.FromFile(_mazeImageFilePath, useEmbeddedColorManagement);
                _mazeToSolve = MazeConverter.ConvertImageToMaze(_mazeImage);
                InstantiateMazeSolutionLogicList();
            }
        }
        // This test was used for tracking the progress of some solver algorithms running in release mode
        public void ConvertTextOutputToSolutionPathOnMazeImage()
        {
            const string mazeImageFilePath         = @"..\..\..\SampleMazes\Maze2.png";
            const string mazeSolutionTextFilePath  = @"..\..\..\SampleMazesSolutions\SampleMaze2\WallHugging.txt";
            const string mazeImageSolutionFilePath = @"..\..\..\SampleMazesSolutions\SampleMaze2\Maze2SolutionProgress.png";

            var mazeReader = new MazeReaderUtility(mazeImageFilePath);
            var maze       = mazeReader.ReadInMazeImage();

            var fileLines = File.ReadAllLines(mazeSolutionTextFilePath);
            var count     = fileLines.Count();

            var mazeSolutionPath = new List <MazeSolutionElement>();

            for (var i = 0; i < count; i++)
            {
                var line      = fileLines[i];
                var splitLine = line.Split(' ', ',');
                var xPosition = Convert.ToInt32(splitLine[2]);
                var yPosition = Convert.ToInt32(splitLine[6]);

                var coordinate    = new CartesianCoordinate(xPosition, yPosition);
                var mazeGridpoint = maze.MazeGridpoints[coordinate];

                var solutionElement = new MazeSolutionElement
                {
                    MazeGridpoint = mazeGridpoint,
                };

                mazeSolutionPath.Add(solutionElement);

                fileLines[i] = null;
            }

            var mazeSolution = new MazeSolution(maze)
            {
                PathToSolveMaze = mazeSolutionPath
            };

            var mazeWriter = new MazeWriterUtility(mazeImageSolutionFilePath, mazeSolution.MazeToSolve);

            mazeSolution.MarkSolutionPath();
            mazeWriter.SaveMazeImage();
        }
Beispiel #4
0
        /// <summary>
        /// Solves an input maze by outputting an image with the solution traced in green
        /// and any spaces visited colored gray.
        ///
        /// Arguments Usage:
        /// [0] - Required - Path to the maze image (string)
        /// [1] - Required - Path to write the solution image (string)
        /// [2] - Optional - Specify to have the solution image be updated in real time (bool)
        /// [3] - Optional - Specify which solver logic to use (int)
        /// </summary>
        static void Main(string[] args)
        {
            var stopwatch          = System.Diagnostics.Stopwatch.StartNew();
            var watchMazeGetSolved = false;

            var pathToMazeImageFile = args[0];
            var mazeReader          = new MazeReaderUtility(pathToMazeImageFile);

            var mazeSolutionType = -1;

            if (args.Length > 3)
            {
                mazeSolutionType = Convert.ToInt32(args[3]);
            }

            var mazeToSolve  = mazeReader.ReadInMazeImage();
            var mazeSolution = InstantiateMazeSolution(mazeToSolve, mazeSolutionType);

            var pathToMazeSolutionImageFile = args[1];
            var mazeWriter = new MazeWriterUtility(pathToMazeSolutionImageFile, mazeSolution.MazeToSolve);

            if (args.Length > 2)
            {
                watchMazeGetSolved = bool.Parse(args[2]);
            }
            var mazeUpdatedListener = new MazeUpdatedListener();

            if (watchMazeGetSolved)
            {
                mazeUpdatedListener.Subscribe(mazeSolution, mazeWriter);
            }

            mazeSolution.SolveMaze();
            mazeSolution.MarkSolutionPath();

            mazeUpdatedListener.UnSubscribe(mazeSolution);
            mazeWriter.SaveMazeImage();

            stopwatch.Stop();
            var elapsedTimeInMilliseconds = stopwatch.ElapsedMilliseconds;

            Console.WriteLine("Elapsed Time in Seconds: " + elapsedTimeInMilliseconds / 1000d);
            Console.WriteLine("Number of Steps: " + mazeSolution.PathToSolveMaze.Count);
        }
        public void ConvertMazeToImage_SuperSimpleMaze_MazeGridpointsMatchHeightTimesWidth()
        {
            const string mazeImageFilePath          = @"..\..\..\TestingMazes\SuperSimpleMaze.png";
            const bool   useEmbeddedColorManagement = true;
            var          originalImage = Image.FromFile(mazeImageFilePath, useEmbeddedColorManagement);

            var maze = MazeConverter.ConvertImageToMaze(originalImage);

            const string mazeImageFileSavePath = @"..\..\..\TestingMazes\SuperSimpleMazeTest1.png";
            var          mazeWriter            = new MazeWriterUtility(mazeImageFileSavePath, maze);

            mazeWriter.SaveMazeImage();

            var newImage = Image.FromFile(mazeImageFileSavePath, useEmbeddedColorManagement);

            var mazeHeight = newImage.Height;
            var mazeWidth  = newImage.Width;

            Assert.AreEqual(maze.MazeGridpoints.Count, mazeHeight * mazeWidth);
        }
Beispiel #6
0
 public void Subscribe(MazeSolution mazeSolution, MazeWriterUtility mazeWriter)
 {
     _mazeWriter = mazeWriter;
     mazeSolution.MazeToSolve.MazeGridpointUpdated += OnMazeUpdated;
 }