private void searchAndFill(Definition.Grid grid)
        {
            var currentGridSeatsCount = grid.Elements.SeatCount();

            if (currentGridSeatsCount <= 1)             //fill by OneInNine pattern
            {
                return;
            }

            Definition.Grid grid1InRow, grid2InRow, grid1InColumn, grid2InColumn;
            grid.GetOtherGrids(Definition.LineType.Row, out grid1InRow, out grid2InRow);
            grid.GetOtherGrids(Definition.LineType.Column, out grid1InColumn, out grid2InColumn);

            var currentGridValues      = grid.Elements.Values().ToArray();
            var currentGridSeatIndexes = grid.Elements.NotValued().Select(item => item.Index).ToArray();
            int currentGridIndex       = grid.Index;

            var allElementsInOtherGrid = grid1InRow.Elements
                                         .Concat(grid2InRow.Elements)
                                         .Concat(grid1InColumn.Elements)
                                         .Concat(grid2InColumn.Elements)
                                         .Valued()
                                         .ExceptWithoutDisdinct(item => item.Value, currentGridValues);

            foreach (var group in allElementsInOtherGrid
                     .GroupBy(item => item.Value)
                     .Where(item => item.Count() > 2))
            {
                var preFilledIndexes = group
                                       .SelectMany(item =>
                                                   pretendFillSeatIndex(currentGridIndex, currentGridSeatIndexes, item.GridIndex, item.Index));
                var exceptedSeatIndexes = currentGridSeatIndexes.Except(preFilledIndexes);
                if (exceptedSeatIndexes.Count() == 1)
                {
                    //means only one seat left
                    int exceptedSeatIndex = exceptedSeatIndexes.First();
                    var emptyElement      = grid.Elements[exceptedSeatIndex];
                    var value             = group.Key;
                    filling(emptyElement, value);
                    emptyElement.SetValue(value);
                }
            }
        }
Пример #2
0
        private bool fillAnyEmptyElement(Definition.Grid grid, Definition.Element emptyElement)
        {
            if (emptyElement.HasValue)
            {
                return(false);
            }

            //get two other grids in same row
            Definition.Grid otherGridInRow1, otherGridInRow2;
            grid.GetOtherGrids(Definition.LineType.Row, out otherGridInRow1, out otherGridInRow2);

            //get two other grids in same column
            Definition.Grid otherGridInColumn1, otherGridInColumn2;
            grid.GetOtherGrids(Definition.LineType.Column, out otherGridInColumn1, out otherGridInColumn2);

            int elementIndex    = emptyElement.Index;
            int rowLineIndex    = elementIndex / 3;
            int columnLineIndex = elementIndex % 3;

            var otherGridInRow1Values = otherGridInRow1
                                        .GetElementsInOtherGridLine(rowLineIndex, Definition.LineType.Row)
                                        .Values();
            var otherGridInRow2Values = otherGridInRow2
                                        .GetElementsInOtherGridLine(rowLineIndex, Definition.LineType.Row)
                                        .Values();
            var otherGridInColumn1Values = otherGridInColumn1
                                           .GetElementsInOtherGridLine(columnLineIndex, Definition.LineType.Column)
                                           .Values();
            var otherGridInColumn2Values = otherGridInColumn2
                                           .GetElementsInOtherGridLine(columnLineIndex, Definition.LineType.Column)
                                           .Values();

            var currentElementValues = grid.Elements
                                       .Values();

            var exceptResult = otherGridInRow1Values
                               .Concat(otherGridInRow2Values)
                               .Concat(otherGridInColumn1Values)
                               .Concat(otherGridInColumn2Values)
                               .Except(currentElementValues);

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

            int value = exactIntersectElementValues.SingleOrDefault(-1);

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