Ejemplo n.º 1
0
 internal bool DoesNotContain(PencilMarks pencilMarks, char digit)
 {
     for (int columnNumber = 0; columnNumber < 9; columnNumber++)
     {
         if (pencilMarks.HasMark(columnNumber, RowNumber, digit))
         {
             return(false);
         }
     }
     return(true);
 }
Ejemplo n.º 2
0
        private void RemoveFromVerticallyAdjacentBoxes(Box box, int columnOffset, PencilMarks pencilMarks, char digit, List <string> results)
        {
            var columnNumber = box.Left + columnOffset;

            for (int rowNumber = 0; rowNumber < box.Top; rowNumber++)
            {
                RemoveMark(pencilMarks, digit, results, columnNumber, rowNumber);
            }

            for (int rowNumber = box.Top + 3; rowNumber < 9; rowNumber++)
            {
                RemoveMark(pencilMarks, digit, results, columnNumber, rowNumber);
            }
        }
Ejemplo n.º 3
0
        internal bool DoesNotContain(PencilMarks pencilMarks, char digit)
        {
            for (int column = 0; column < 3; column++)
            {
                for (int row = 0; row < 3; row++)
                {
                    if (pencilMarks.HasMark(column + Left, row + Top, digit))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
        public List <string> Reduce(Grid grid, PencilMarks pencilMarks)
        {
            var results       = new List <string>();
            var previousCount = -1;

            while (results.Count > previousCount)
            {
                previousCount = results.Count;
                foreach (var reducer in reducers)
                {
                    results.AddRange(reducer.Evaluate(grid, pencilMarks));
                }
            }

            return(results);
        }
Ejemplo n.º 5
0
        public List <string> Evaluate(Grid grid, PencilMarks pencilMarks)
        {
            var result = new List <string>();

            foreach (var box in grid.Boxes())
            {
                foreach (var digit in grid.AllowedDigits)
                {
                    if (box.DoesNotContain(digit))
                    {
                        var columns = new int[3];
                        var rows    = new int[3];

                        for (int x = 0; x < 3; x++)
                        {
                            for (int y = 0; y < 3; y++)
                            {
                                if (pencilMarks.HasMark(box.Left + x, box.Top + y, digit))
                                {
                                    columns[x]++;
                                    rows[y]++;
                                }
                            }
                        }

                        if (columns[0] > 0 && columns[1] == 0 && columns[2] == 0)
                        {
                            RemoveFromVerticallyAdjacentBoxes(box, 0, pencilMarks, digit, result);
                        }

                        if (columns[0] == 0 && columns[1] > 0 && columns[2] == 0)
                        {
                            RemoveFromVerticallyAdjacentBoxes(box, 1, pencilMarks, digit, result);
                        }

                        if (columns[0] == 0 && columns[1] == 0 && columns[2] > 0)
                        {
                            RemoveFromVerticallyAdjacentBoxes(box, 2, pencilMarks, digit, result);
                        }


                        if (rows[0] > 0 && rows[1] == 0 && rows[2] == 0)
                        {
                            RemoveFromHorizontallyAdjacentBoxes(box, 0, pencilMarks, digit, result);
                        }

                        if (rows[0] == 0 && rows[1] > 0 && rows[2] == 0)
                        {
                            RemoveFromHorizontallyAdjacentBoxes(box, 1, pencilMarks, digit, result);
                        }

                        if (rows[0] == 0 && rows[1] == 0 && rows[2] > 0)
                        {
                            RemoveFromHorizontallyAdjacentBoxes(box, 2, pencilMarks, digit, result);
                        }
                    }
                }
            }

            return(result);
        }