示例#1
0
 public Grid(SolvingGrid g)
 {
     grid          = g.grid;
     colorsPresent = g.colors;
     pathsOfColors = g.pathsOfColors.ToArray();
     RotateFlipPaths(pathsOfColors); // for some weird reason SolvingGrid flips x and y axis (and some other funky stuff, either way this fixes it)
     solved = false;
 }
示例#2
0
        /// <summary>
        /// Gets the potential directions of the current active cell, or the last cell on the last path in the given SolvingGrid.
        /// </summary>
        /// <param name="info">All the appropriate info in the form of a SolvingGrid.</param>
        /// <returns>Direction array with all options.</returns>
        private static Path.Direction[] GetMoveOptions(SolvingGrid info)
        {
            int  Y, X;
            Path currentPath = info.pathsOfColors.Last();

            Y = currentPath.lastPoint.Y;
            X = currentPath.lastPoint.X;
            List <Path.Direction> potentials = new List <Path.Direction>();

            int[,] grid = info.FlattenGrid();
            if (Y - 1 >= 0)
            {
                if (grid[Y - 1, X] == -1 || // direction is valid if new square is not visited or new square is the endpoint for the color of the current path
                    new Point(X, Y - 1) == info.endNodes[Array.IndexOf(info.colors, info.pathsOfColors.Last().color)])
                {
                    potentials.Add(Path.Direction.Up);
                }
            }
            if (Y + 1 < grid.GetLength(0))
            {
                if (grid[Y + 1, X] == -1 ||
                    new Point(X, Y + 1) == info.endNodes[Array.IndexOf(info.colors, info.pathsOfColors.Last().color)])
                {
                    potentials.Add(Path.Direction.Down);
                }
            }
            if (X - 1 >= 0)
            {
                if (grid[Y, X - 1] == -1 ||
                    new Point(X - 1, Y) == info.endNodes[Array.IndexOf(info.colors, info.pathsOfColors.Last().color)])
                {
                    potentials.Add(Path.Direction.Left);
                }
            }
            if (X + 1 < grid.GetLength(1))
            {
                if (grid[Y, X + 1] == -1 ||
                    new Point(X + 1, Y) == info.endNodes[Array.IndexOf(info.colors, info.pathsOfColors.Last().color)])
                {
                    potentials.Add(Path.Direction.Right);
                }
            }
            return(potentials.ToArray());
        }
示例#3
0
        public static bool IsItSolvable(int[,] grid, bool filterLameGrids)
        {
            SolvingGrid info = new SolvingGrid(grid);

            if (filterLameGrids) // filters grids that have any two same-color nodes adjacent to each other
            {
                for (int i = 0; i < info.colors.Length; i++)
                {
                    if ((Math.Abs(info.startNodes[i].X - info.endNodes[i].X) == 1 && info.startNodes[i].Y == info.endNodes[i].Y))
                    {
                        return(false);
                    }
                    if ((Math.Abs(info.startNodes[i].Y - info.endNodes[i].Y) == 1 && info.startNodes[i].X == info.endNodes[i].X))
                    {
                        return(false);
                    }
                }
            }

            return(Solve(info));
        }
示例#4
0
 /// <summary>
 /// Recursive algorithm to solve a level.
 /// </summary>
 /// <param name="info">All the appropriate info stored in a SolvingGrid.</param>
 /// <returns>Returns whether or not the level is solvable.</returns>
 private static bool Solve(SolvingGrid info)
 {
     Path.Direction[] potentials = GetMoveOptions(info);
     foreach (Path.Direction d in potentials)
     {
         info.AddDirectionToCurrentPath(d);
         if (info.state == SolvingGrid.SolveState.Success)
         {
             lastSolution = new Grid(info);
             return(true);
         }
         else if (info.state == SolvingGrid.SolveState.Solving)
         {
             if (Solve(info))
             {
                 return(true);
             }
         }
         info.RemoveLastAction();
     }
     return(false);
 }