Exemple #1
0
 public bool SaveMaze(string name, ImageFormat format)
 {
     if (MazeDestination != null || String.IsNullOrEmpty(name))
     {
         try {
             MazeDestination.Save(name, format);
             return(true);
         } catch (Exception e) {
             Console.WriteLine("Maze cannot be saved: " + e.StackTrace);
         }
     }
     return(false);
 }
Exemple #2
0
 public bool Solve(int x, int y)
 {
     if (!IsValid(x, y))
     {
         return(false);
     }
     while (IsValid(x, y) && mSource.GetPixel(x, y).ToArgb() != Color.Blue.ToArgb())
     {
         if (MoveAlongWall(ref x, ref y) && IsValid(x, y))
         {
             MazeDestination.SetPixel(x, y, Color.Green);
         }
         else if (MoveToNearestWall(ref x, ref y) && IsValid(x, y))
         {
             MazeDestination.SetPixel(x, y, Color.Green);
         }
         else
         {
             break;
         }
     }
     return(true);
 }
Exemple #3
0
        private void CheckMovingThroughTube(ref int x, ref int y, bool isLeft, bool isRight, bool isUp, bool isDown)
        {
            MazeDestination.SetPixel(x, y, Color.Green);

            if (isLeft || isRight)
            {
                // check if we are moving through tube

                while (IsInHorizontalTube(x, y))
                {
                    if (isLeft && !IsValidWall(x - 1, y))
                    {
                        x--;
                    }
                    else if (isRight && !IsValidWall(x + 1, y))
                    {
                        x++;
                    }

                    // if wall is eminent turn around and move back
                    else if (isLeft && IsValidWall(x - 1, y))
                    {
                        isLeft  = false;
                        isRight = true;
                        x++;
                    }
                    else if (isRight && IsValidWall(x + 1, y))
                    {
                        isLeft  = true;
                        isRight = false;
                        x--;
                    }
                }
            }
            if (isUp || isDown)
            {
                // check if we are moving through tube
                while (IsInVerticalTube(x, y))
                {
                    if (isUp && !IsValidWall(x, y - 1))
                    {
                        y--;
                    }
                    else if (isDown && !IsValidWall(x, y + 1))
                    {
                        y++;
                    }

                    // if wall is eminent turn around and move back
                    else if (isUp && IsValidWall(x, y - 1))
                    {
                        isUp   = false;
                        isDown = true;
                        y++;
                    }
                    else if (isDown && IsValidWall(x, y + 1))
                    {
                        isUp   = true;
                        isDown = false;
                        y--;
                    }
                }
            }

            // now out of the tube, make sure we position element along wall and it isn't another tube to go through
            if (isUp && !IsValidWall(x - 1, y) && !IsInHorizontalTube(x - 1, y))
            {
                x--;
            }
            if (isDown && !IsValidWall(x + 1, y) && !IsInHorizontalTube(x + 1, y))
            {
                x++;
            }
            if (isLeft && !IsValidWall(x, y + 1) && !IsInVerticalTube(x, y + 1))
            {
                y++;
            }
            if (isRight && !IsValidWall(x, y - 1) && !IsInVerticalTube(x, y - 1))
            {
                y--;
            }
        }