ScrambleSquaresSolver/Application/Solver.cs
/// <summary>
/// Returns the first complete grid that is found
/// </summary>
/// <param name="currentGrid"></param>
/// <param name="remainingTiles"></param>
/// <param name="iteration">The total count of the number of steps this has taken</param>
/// <returns></returns>
Returns the first complete grid that is found
Returns the first complete grid that is found
public static Grid SolveSync(Grid currentGrid, List<Tile> remainingTiles, ref int iteration)
        {
            var nextSpot = currentGrid.FindNextEmptySpot();
            if (nextSpot == null)
                return currentGrid;
            var legalTiles = remainingTiles.FindAll(t =>
                currentGrid.IsLegalSpot(nextSpot.Item1, nextSpot.Item2, t));
            if (!legalTiles.Any())
            {
                return null;
            }
            foreach (var tile in legalTiles)
            {
                Console.WriteLine($"Placing Tile {tile} in {nextSpot.Item1},{nextSpot.Item2}");
                iteration++;
                currentGrid[nextSpot.Item1, nextSpot.Item2].PlaceTile(tile);
                var result = SolveSync(currentGrid, remainingTiles.FindAll(t => t.Id != tile.Id), ref iteration);
                if (result != null)
                    return currentGrid;
                currentGrid[nextSpot.Item1, nextSpot.Item2].RemoveTile();
            }
            return null;
        }
public static Grid SolveSync ( Grid currentGrid , List < Tile > remainingTiles , ref int iteration ) { var nextSpot = currentGrid . FindNextEmptySpot ( ) ; if ( nextSpot == null ) return currentGrid ; var legalTiles = remainingTiles . FindAll ( t => currentGrid . IsLegalSpot ( nextSpot . Item1 , nextSpot . Item2 , t ) ) ; if ( ! legalTiles . Any ( ) ) { return null ; } foreach ( var tile in legalTiles ) { Console . WriteLine ( $" Placing Tile  { tile }  in  { nextSpot . Item1 } , { nextSpot . Item2 } " ) ; iteration ++ ; currentGrid [ nextSpot . Item1 , nextSpot . Item2 ] . PlaceTile ( tile ) ; var result = SolveSync ( currentGrid , remainingTiles . FindAll ( t => t . Id != tile . Id ) , ref iteration ) ; if ( result != null ) return currentGrid ; currentGrid [ nextSpot . Item1 , nextSpot . Item2 ] . RemoveTile ( ) ; } return null ; }
Returns the first complete grid that is found
Returns the first complete grid that is found
// Puzzle is solved //This approach fails, fall back until another legal combination can be found // Send all of the tiles that do not match the Id of the one that is placed to the next step // A solution has been found // There is no solution from this branch
None
currentGrid
Grid
None
remainingTiles
List<Tile>
None
int
ref
iteration
The total count of the number of steps this has taken
The total count of the number of steps this has taken