Ejemplo n.º 1
0
        public static bool GetNearestValue(byte R, byte G, byte B, out MazeValue value, double maxDistance = -1)
        {
            Color[] colors = new Color[5] {
                Color.Black, Color.Blue, Color.White, Color.Red, Color.Lime
            };
            double minDistance  = double.MaxValue;
            Color  closestColor = new Color();

            foreach (Color color in colors)
            {
                double squareDistance = (color.R - R) * (color.R - R) + (color.G - G) * (color.G - G) + (color.B - B) * (color.B - B);

                if (squareDistance < minDistance)
                {
                    closestColor = color;
                    minDistance  = squareDistance;
                }
            }

            if (maxDistance > 0 && Math.Sqrt(minDistance) < maxDistance)
            {
                value = REVERSE_MAPPING[closestColor.ToArgb()];
                return(true);
            }

            value = MazeValue.Wall;  // Use the wall as a garbage value
            return(false);
        }
Ejemplo n.º 2
0
        private void BFS(ref MazeValue[,] maze, Point startingPoint, ref int[,] distance, ref Point[,] previous) //, out List<Point> nearbyStartingPoints)
        {
            Queue <Point> points = new Queue <Point>(maze.GetLength(0) * maze.GetLength(1));

            AddNeighbors(ref maze, ref points, ref distance, ref previous, startingPoint);
            while (points.Count > 0)
            {
                Point     currentPoint     = points.Dequeue();
                MazeValue currentMazeValue = maze[currentPoint.X, currentPoint.Y];

                switch (currentMazeValue)
                {
                case MazeValue.End:
                    return;

                case MazeValue.OpenSpace:
                    AddNeighbors(ref maze, ref points, ref distance, ref previous, currentPoint);
                    break;

                case MazeValue.Start:
                case MazeValue.Wall:
                    // Do Nothing
                    break;

                default:
                    throw new ArgumentException();
                }
            }
        }
        public void TestGenerateInvalidImagePath()
        {
            Random rngesus    = new Random();
            string outputFile = Path.Combine(FileSystemConstants.OUTPUT_FOLDER, "tmp_invalid_path.bmp");

            File.Create(outputFile).Close();
            int width = 10;

            MazeValue[,] maze = new MazeValue[width, width];
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    maze[i, j] = MazeValue.Wall;
                }
            }

            // Make sure the GenerateFile function returns false if file already exists
            Assert.IsFalse(MazeParser.GenerateFile(maze, outputFile));

            // Make sure the file has not changed (is empty)
            Assert.IsTrue(File.ReadAllBytes(outputFile).Length == 0);
            try
            {
                Bitmap image = new Bitmap(outputFile);
                Assert.Fail();
            }
            catch (ArgumentException)
            {
                // Pass
            }
        }
        public void TestGenerateEmptyImage()
        {
            Random rngesus = new Random();

            MazeValue[,] maze = new MazeValue[0, 0];
            string outputFile = Path.Combine(FileSystemConstants.OUTPUT_FOLDER, "tmp_empty.bmp");

            Assert.IsFalse(MazeParser.GenerateFile(maze, outputFile));
        }
Ejemplo n.º 5
0
        protected MazeValue[,] DeepCopy(MazeValue[,] values)
        {
            int width  = values.GetLength(0);
            int height = values.GetLength(1);

            MazeValue[,] newMaze = new MazeValue[width, height];

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    newMaze[i, j] = values[i, j];
                }
            }
            return(newMaze);
        }
Ejemplo n.º 6
0
        public static MazeValue[,] FuzzyParse(Bitmap img, bool pathInvalid = true)
        {
            MazeValue[,] maze = new MazeValue[img.Width, img.Height];
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    Color pixel = img.GetPixel(i, j);
                    if (!ColorMap.GetNearestValue(pixel.R, pixel.G, pixel.B, out maze[i, j], MAX_COLOR_DISTANCE) ||
                        (pathInvalid && maze[i, j] == MazeValue.Path))
                    {
                        throw new BadImageFormatException("The image has an unrecognized color");
                    }
                }
            }

            return(maze);
        }
Ejemplo n.º 7
0
        public static MazeValue[,] Parse(Bitmap img, bool pathInvalid = true)
        {
            MazeValue[,] maze = new MazeValue[img.Width, img.Height];
            for (int i = 0; i < img.Width; i++)
            {
                for (int j = 0; j < img.Height; j++)
                {
                    Color pixel = img.GetPixel(i, j);
                    if (!ColorMap.REVERSE_MAPPING.TryGetValue(pixel.ToArgb(), out maze[i, j]) ||
                        (pathInvalid && maze[i, j] == MazeValue.Path))
                    {
                        throw new BadImageFormatException("The image has an unrecognized color");
                    }
                }
            }

            return(maze);
        }
        private void CreateAndTestRandomlyGeneratedImage(string fileExtension, bool useFuzzy = false)
        {
            int    NUM_TESTS = 10;
            Random rngesus   = new Random();

            Array mazeValues = Enum.GetValues(typeof(MazeValue));

            for (int test_num = 0; test_num < NUM_TESTS; test_num++)
            {
                int    width      = rngesus.Next(75, 100);
                int    height     = rngesus.Next(75, 100);
                string outputFile = Path.Combine(FileSystemConstants.OUTPUT_FOLDER, "tmp_" +
                                                 test_num + fileExtension);

                // Create maze
                MazeValue[,] maze = new MazeValue[width, height];
                for (int i = 0; i < width; i++)
                {
                    for (int j = 0; j < height; j++)
                    {
                        maze[i, j] = (MazeValue)rngesus.Next(0, mazeValues.Length);
                    }
                }

                Assert.IsTrue(MazeParser.GenerateFile(maze, outputFile));

                // Use the fuzzy parser on jpeg images
                MazeValue[,] mazeFromFile;
                if (!useFuzzy)
                {
                    mazeFromFile = MazeParser.Parse(outputFile, false);
                    Assert.IsTrue(CheckMazeEquality(maze, mazeFromFile));
                }
                else
                {
                    // Note: Even the fuzzy parser can't deal with the jpeg resulting image due to lossy compression
                    //mazeFromFile = MazeParser.FuzzyParse(output_file, false);
                }
            }
        }