Ejemplo n.º 1
0
        private bool fillOnlyOneElement(Definition.GridLine gridLine)
        {
            //get empty element
            Definition.Element emptyElement = null;
            using (var e = gridLine.Elements
                           .Where(item => !item.HasValue)
                           .GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return(true);                               //not empty element
                }
                emptyElement = e.Current;
                if (e.MoveNext())
                {
                    return(false);                              //more than one empty element
                }
            }

            var lineType = gridLine.LineType;

            //get two other grids
            var currentGrid = gridLine.Grid;

            Definition.Grid otherGrid1, otherGrid2;
            currentGrid.GetOtherGrids(lineType, out otherGrid1, out otherGrid2);

            int currentLineIndex = gridLine.Index;

            var otherElementValues1 = otherGrid1.GetElementsInOtherGridLine(currentLineIndex, lineType)
                                      .Values();

            var otherElementValues2 = otherGrid2.GetElementsInOtherGridLine(currentLineIndex, lineType)
                                      .Values();

            var currentElementValues = currentGrid.Elements
                                       .Values();

            var exceptResult = otherElementValues1
                               .Concat(otherElementValues2)
                               .Except(currentElementValues);

            if (!exceptResult.Any())
            {
                return(false);                //no result
            }
            //get value which appeared both
            var bothOtherElementValues      = otherElementValues1.Intersect(otherElementValues2);
            var exactIntersectElementValues = bothOtherElementValues.Intersect(exceptResult);

            int value = exactIntersectElementValues.SingleOrDefault(-1);

            if (value > 0)
            {
                filling(emptyElement, value);
                emptyElement.SetValue(value);
                return(true);
            }
            return(false);
        }
 public GridLineUpdatedEventArgs(Definition.GridLine line)
     : base(line.Index, line)
 {
     this.line = line;
 }
Ejemplo n.º 3
0
 public GridLineObserver(Definition.GridLine line, SeatMode seatMode)
     : base(line, seatMode)
 {
     this.line = line;
 }
        private bool fillOnlyOneElement(Definition.GridLine gridLine)
        {
            //get empty element
            Definition.Element emptyElement = null;
            using (var e = gridLine.Elements
                           .Where(item => !item.HasValue)
                           .GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return(true);                               //not empty element
                }
                emptyElement = e.Current;
                if (e.MoveNext())
                {
                    return(false);                              //more than one empty element
                }
            }

            var lineType = gridLine.LineType;

            //get two other grids
            var currentGrid = gridLine.Grid;

            Definition.Grid otherGrid1, otherGrid2;
            currentGrid.GetOtherGrids(lineType, out otherGrid1, out otherGrid2);

            int currentLineIndex = gridLine.Index;

            var grid1Completed = otherGrid1
                                 .GetElementsInCurrentGridLine(currentLineIndex, lineType)
                                 .AllHasValue();
            var grid2Completed = otherGrid2
                                 .GetElementsInCurrentGridLine(currentLineIndex, lineType)
                                 .AllHasValue();

            //only when one other grid line is completed
            if (grid1Completed == grid2Completed)
            {
                return(false);
            }

            var targetGrid = grid1Completed ? otherGrid2 : otherGrid1;

            //if one of seats value appeared in target grid, fill to complete
            var seatValues = new LineEnumerable(this.sudoku, lineType)
                             //.ElementAt(currentLineIndex) //should get sudoku line index here
                             .ElementAt(getSudokuLineIndex(currentLineIndex, currentGrid.Index, lineType))
                             .Elements
                             .Values()
                             .SudokuExcept();
            var intersectResults = targetGrid.Elements.Values()
                                   .Except(currentGrid.Elements.Values())
                                   .Intersect(seatValues);

            int value = intersectResults.SingleOrDefault(-1);

            if (value > 0)
            {
                filling(emptyElement, value);
                emptyElement.SetValue(value);
                return(true);
            }
            return(false);
        }