Esempio n. 1
0
            private static bool BruteForceRecursion(Board work, ConstraintData data, int Index)
            {
                if (Index == 81)
                    return true;

                if (work[Index] > 0)
                    return BruteForceRecursion(work, data, Index + 1);

                var loc = new Location(Index);

                for (int i = 1; i < 10; i++)
                {
                    if (!data.DigitInRow[i, loc.Row] && !data.DigitInColumn[i, loc.Column] && !data.DigitInZone[i, loc.Zone])
                    {
                        work[loc] = i;
                        data.DigitInRow[i, loc.Row] = data.DigitInColumn[i, loc.Column] = data.DigitInZone[i, loc.Zone] = true;
                        if (BruteForceRecursion(work, data, Index + 1))
                            return true;
                        data.DigitInRow[i, loc.Row] = data.DigitInColumn[i, loc.Column] = data.DigitInZone[i, loc.Zone] = false;
                    }
                }

                work[Index] = 0;
                return false;
            }
Esempio n. 2
0
            private static bool RandomRecursion(Board work, ConstraintData data, List<int> Digits, int Index)
            {
                if (Index == 81)
                    return true;

                var loc = new Location(Index);

                if (work[loc] > 0)
                    return RandomRecursion(work, data, Digits, Index + 1);

                foreach (int test in Digits)
                {
                    if (!data.DigitInRow[test, loc.Row] && !data.DigitInColumn[test, loc.Column] && !data.DigitInZone[test, loc.Zone])
                    {
                        work[loc] = test;
                        data.DigitInRow[test, loc.Row] = data.DigitInColumn[test, loc.Column] = data.DigitInZone[test, loc.Zone] = true;
                        if (RandomRecursion(work, data, Digits, Index + 1))
                            return true;
                        data.DigitInRow[test, loc.Row] = data.DigitInColumn[test, loc.Column] = data.DigitInZone[test, loc.Zone] = false;
                    }
                }

                work[loc] = 0;
                return false;
            }
Esempio n. 3
0
            /// <summary>
            /// Returns every digit which may be placed in the calling <see cref="Board"/> instance at the specified <see cref="Location"/>
            /// </summary>
            /// <param name="Where">The <see cref="Location"/> to check</param>
            /// <returns><see cref="List{Int32}"/></returns>
            public List<int> Candidates(Location Where)
            {
                if (_parent[Where] > 0)
                    return new List<int>();

                bool[] present = new bool[10];
                foreach (var loc in Where.Blocking)
                    present[_parent[loc]] = true;

                List<int> result = new List<int>();
                for (int i = 1; i < 10; i++)
                    if (!present[i])
                        result.Add(i);

                return result;
            }
Esempio n. 4
0
            /// <summary>
            /// Attempts to cut 4 locations from the current board, mirrored about both horizontal and vertical axes.
            /// </summary>
            /// <param name="Stream">An existing <see cref="Random"/> number generator</param>
            /// <returns>If the result has a unique solution, then the new <see cref="Board"/>. Otherwise, the original</returns>
            public Board Quad(Random Stream)
            {
                var result = new Board(_parent);

                var Filled = _parent.Find.FilledLocations().ToList();

                if (Filled.Count > 0)
                {
                    Location loc = new Location(Filled[Stream.Next(Filled.Count)]);

                    result[loc] = 0;
                    result[loc.FlipHorizontal()] = 0;
                    result[loc.FlipVertical()] = 0;
                    result[loc.FlipHorizontal().FlipVertical()] = 0;

                    if (result.ExistsUniqueSolution())
                        return result;
                }
                return _parent;
            }
Esempio n. 5
0
 /// <summary>
 /// Determines whether [is same Zone] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameZone(Location CompareTo)
 {
     return Zone == CompareTo.Zone;
 }
Esempio n. 6
0
 /// <summary>
 /// Determines whether [is same row] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameRow(Location CompareTo)
 {
     return Row == CompareTo.Row;
 }
Esempio n. 7
0
 /// <summary>
 /// Determines whether [is same Column] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameColumn(Location CompareTo)
 {
     return Column == CompareTo.Column;
 }
Esempio n. 8
0
 /// <summary>
 /// Determines whether [the specified <see cref="Location"/> CompareTo] is either of the same row, column or zone as the calling instance.
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsBlockedBy(Location CompareTo)
 {
     return (IsSameRow(CompareTo) || IsSameColumn(CompareTo) || IsSameZone(CompareTo));
 }
Esempio n. 9
0
 /// <summary>
 /// Fills a cell in.
 /// </summary>
 /// <param name="Where">The <see cref="Location"/> of the cell to fill.</param>
 /// <param name="value">The value to place; 0 for clear, or 1-9.</param>
 public void PutCell(Location Where, int value)
 {
     this[Where] = value;
 }
Esempio n. 10
0
 /// <summary>
 /// Gets the value of a given cell.
 /// </summary>
 /// <param name="Where">The cell to check; may be provided as either an instance of <see cref="Location"/> or the integer index of the cell.</param>
 /// <returns></returns>
 public int GetCell(Location Where)
 {
     return this[Where];
 }
Esempio n. 11
0
 /// <summary>
 /// Overrides array indexing (suare brackets []) for accessing locations in the Grid.
 /// Essentially, it's another way to access GetCell and PutCell.
 /// 
 /// You may use
 ///   <see cref="Location"/> where = new <see cref="Location"/>(x,y);
 ///   <see cref="int"/> result = myGrid[where];
 ///   myGrid[where] = result;
 /// </summary>
 /// <value>
 /// The <see cref="int"/> representing the value of the cell (0 for empty, 1-9 for a value).
 /// </value>
 /// <param name="where">The <see cref="Location"/> to access.</param>
 /// <returns></returns>
 public int this[Location where]
 {
     get { return data[where]; }
     set { data[where] = value; }
 }
Esempio n. 12
0
 /// <summary>
 /// Determines whether [the specified <see cref="Location"/> CompareTo] is either of the same row, column or zone as the calling instance.
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsBlockedBy(Location CompareTo)
 {
     return(IsSameRow(CompareTo) || IsSameColumn(CompareTo) || IsSameZone(CompareTo));
 }
Esempio n. 13
0
 /// <summary>
 /// Determines whether [is same Zone] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameZone(Location CompareTo)
 {
     return(Zone == CompareTo.Zone);
 }
Esempio n. 14
0
 /// <summary>
 /// Determines whether [is same Column] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameColumn(Location CompareTo)
 {
     return(Column == CompareTo.Column);
 }
Esempio n. 15
0
 /// <summary>
 /// Determines whether [is same row] [the specified <see cref="Location"/> CompareTo].
 /// </summary>
 /// <param name="CompareTo">The <see cref="Location"/> to be compared.</param>
 /// <returns>True or False</returns>
 public bool IsSameRow(Location CompareTo)
 {
     return(Row == CompareTo.Row);
 }